title: Optimization Django ORM
body: <p>I'm running my own smart house project, using django backend with MySql on raspberry Pi.I've got SensorsData table in DB with thousands records with data from sensors. In my REST API I'm using view which looks like this:</p>
  <pre><code>@api_view(['GET'])
  @permission_classes([IsAuthenticated])
  def list_of_sensors_data(request, format=None):
      &quot;&quot;&quot;
      Get list of all sensors data, only for authenticated users
      :param request: GET
      :return: list of all sensors data if ok http 200 response
      &quot;&quot;&quot;
      sensors_data = SensorsData.objects.all()
      serializer = SensorsDataSerializer(sensors_data, many=True)
      return Response(serializer.data, status=status.HTTP_200_OK)
  </code></pre>
  <p>I've run perfomance test with locust simulating 10 users trying to use my endpoints. After some time, Django keeps returning 504 Timeout using this particular endpoint. My quest is, <em>how can I optimize this queryset</em>? I need to make it faster.</p>
  <p>EDIT SensorsData model:</p>
  <pre><code>class SensorsData(models.Model):
      sensor = models.ForeignKey(Sensors, on_delete=models.CASCADE)
      delivery_time = models.DateTimeField(auto_now_add=True)
      sensor_data = models.CharField(max_length=20)
  
      class Meta:
          verbose_name = &quot;Sensor data&quot;
          verbose_name_plural = &quot;Sensors data&quot;
  
      def __str__(self):
          return f&quot;{self.sensor.id}: {self.sensor.name}&quot;
  </code></pre>
  <p>SensorsData Serializer:</p>
  <pre><code>class SensorsDataSerializer(serializers.ModelSerializer):
      sensor = serializers.SlugRelatedField(read_only=False, many=False, slug_field='name', queryset=Sensors.objects.all())
  
      class Meta:
          model = SensorsData
          fields = (&quot;sensor&quot;, &quot;delivery_time&quot;, &quot;sensor_data&quot;)
  </code></pre>