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 B开发者_运维百科Y 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.
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?
I don't think this query is even going to give you correct results.
Add an index to campusID and Term, and possibly clock. Then try this query:
SELECT 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
GROUP by seotcresults_v2.Clock
ORDER BY seotcresults_v2.Clock
精彩评论