Join query taking 11 mins to run on 300,000 rows table

The following should help with the execution time:

  • remove the ORDER BY if it isn't strictly necessary
  • replace the join of dr table with WHERE EXISTS (SELECT 1 FROM tms_door_record_raw As dr WHERE c.card_no = dr.card_no AND dr.record_time BETWEEN '2016-11-01' AND '2016-11-02')
  • GROUP BY, may now not be needed
  • widen the index on tms_door_record_raw to include both card_no and record_time

Test this and see if progress is being made. Further steps may be necessary but hopefully this is in the right direction.