I have two tables, one of which matches ids to ratings and one of which has the ids with the actual data in about 15 columns. In the data table the id column has an index and in the ratings table the id is the primary key and the rating is indexed. I want to select data but only from the rows which have a positive rating. to do so I am using the query
SELECT * FROM data_table
INNER JOIN rating_table ON data_table.id = rating_table.id
WHERE rating > 0
but for some reason this is taking about 0.35 seconds which seems very long to me. There are about 90,000 rows in the data table and about 5,000 rows in the rating table and this needs to be taking much less than even a tenth of a second... How can I either index differently or query differently to speed this task up?
edit: After profiling it gave me the following. Note that I think it was cached so the query came back much faster than before but even so this might be useful to someone
0.000012 starting
0.000053 checking query cache for query
0.000014 Opening tables
0.000006 System lock
0.000027 Table lock
0.000044 init
0.000018 optimizing
0.000060 statistics
0.000016 preparing
0.000004 executing
0.004916 Sending data
0.000007 end
0.000003 query end
0.002271 freeing items
0.000009 storing result in query cache
0.000002 logging slow query
0.000004 cleaning up
so I see a lot of the time was spent in sending data... how can I speed this part up?
data_table.id
andrating_table.id
of the same data type? 2) Besides indexing bothid
fields, have you indexedrating
, too? 3) Have you profiled the statement to find out if the duration isn't caused by other things (like sending the data back to the client)?