Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up this MySQL Statement

I'm trying to get the count of surveys that need to be taken which is stored in seotc, and the count of surveys completed, stored seotcresults_v2. The seotc table holds nearly 100k records, and the seotcresults_v2 table hold about half that. How can I speed this query up?

SELECT 
  DISTINCT seotcresults_v2.Clock, 
  COUNT(seotc.Id) AS Surveys, 
  COUNT(seotcresults_v2.Id) AS Complete 
FROM seotc 
JOIN seotcresults_v2 ON seotcresults_v2.Clock = seotc.Clock
WHERE seotcresults_v2.CampusID = 40
AND seotcresults_v2.Term = 201011
ORDER BY seotc.Clock

UPDATE:

Thanks for all the responses. The table Structure (minimally) is as such:

seotc: | Id | Clock | CampusID | Term |

seotcresults_v2: | Id | Clock | CampusID | Term | Q1 | Q2 | ...etc

Id is the auto-incremented index in each table for the surveys and survey results

Where 'Clock' is an Id for an instructor and can be found multiple times in the seotc and seotcresults_v2 table because they have multiple classes and multiple surveys completed for each class for multiple terms. I'm essentially trying to determine the response rate based on the number of surveys for an instructor at a given campus in a given term versus the number of results posted given those same parameters. Does that help?

I will attempt to run the EXPLAIN as well shortly.

like image 823
d2burke Avatar asked Oct 14 '22 19:10

d2burke


1 Answers

First things to check: are the WHERE-clause columns indexed?

Then: do you need the ORDER BY, which is a sort, and expensive.

Finally, are the .clock columns indexed?

like image 113
Eight-Bit Guru Avatar answered Oct 18 '22 01:10

Eight-Bit Guru