I have created an API to get all the data from by database. There is a "deployment" table and a related table "sensors" which has a foreign key referencing the deployment, a one-many relationship. The serializer creates a nested JSON. Currently if I request all records it takes roughly 45 seconds to return the data (17,000 JSON lines).
How do I profile my Django application to determine what is the bottleneck?
Any suggestions on what can be improved to speed this up? Or is this is as good as it's going to get?
models.py
class deployment(models.Model):
ADD_DATE = models.DateTimeField() #creation of record in db
#...30 more fields....
class sensor(models.Model):
DEPLOYMENT = models.ForeignKey(deployment, related_name='sensors', on_delete=models.CASCADE)
ADD_DATE = models.DateTimeField() #creation of record in db
SENSOR = models.ForeignKey(sensor_types, to_field="VALUE", max_length=50, on_delete=models.PROTECT)
#...5 more foreign key fields...
views.py
class GetCrtMetadata(generics.ListAPIView): #Read only
serializer_class = CurrentDeploymentSerializer
queryset=deployment.objects.all().prefetch_related("sensors")
filter_backends = [DjangoFilterBackend]
filter_fields = [field.name for field in deployment._meta.fields]
deployment app serializers.py
class CurrentDeploymentSerializer(serializers.ModelSerializer):
#Returns deployment with sensors
sensors = SensorSerializer(many=True)
class Meta:
model = deployment
fields = [field.name for field in deployment._meta.fields]
fields.extend(['sensors'])
read_only_fields = fields
sensor app serializers.py
class SensorSerializer(serializers.ModelSerializer):
class Meta:
model = sensor
fields = [field.name for field in sensor._meta.fields]