1

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?

6
  • 2
    Three questions: 1) Are data_table.id and rating_table.id of the same data type? 2) Besides indexing both id fields, have you indexed rating, 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)?
    – Bjoern
    Jun 28, 2012 at 6:26
  • Create Index. It will greatly improve your query response time.
    – Jonas T
    Jun 28, 2012 at 6:27
  • yes I said in the question that rating already has an index and the ids in both tables are of the same data type. I am running this in php myadmin for now so the time is actually spent in mysql. Also what do you mean by query plan?
    – hackartist
    Jun 28, 2012 at 6:29
  • MySQL only has primitive standard support, but see dev.mysql.com/doc/refman/5.6/en/execution-plan-information.html. A query plan is how the particular RA operations are performed: table scan? hash join? [which] index used? (It also contains other information like size estimates -- a wrong size/cardinality estimate can lead to poor plan selection.)
    – user166390
    Jun 28, 2012 at 6:34
  • select only those columns which are required instead of * and what in rating? is it a column? use data_table.rating > 0 Can you provide some sample data on sqlfiddle.com Jun 28, 2012 at 6:36

1 Answer 1

3

No exact solution to your problem, but some advice without seeing the actual table structure: Profile your Query properly.

MySQL has an inbuild profiler, which allows you to see very detailed for what part of the query how much time has been spend.

In your case, follow these steps:

(1) Execute your query.

(2) Find out the query id for profiling:

SHOW PROFILES;

It will return you something like this:

Query_ID |  Duration | Query
---------+-----------+-----------------------
   2     | 0.0006200 | SHOW STATUS
   3     | 0.3600000 | (your query here)
  ...    | ...       | ...

Now you know the query id is (3).

(3) Profile the query.

SHOW PROFILE FOR QUERY 3;   // example 

This will return you the details, which might look like this:

Status                          | Duration
--------------------------------+-------------------
starting                        | 0.000010
checking query cache for query  | 0.000078
Opening tables                  | 0.000051
System lock                     | 0.000003
Table lock                      | 0.000008
init                            | 0.000036
optimizing                      | 0.000020
statistics                      | 0.000013
preparing                       | 0.000015
Creating tmp table              | 0.000028
executing                       | 0.000602
Copying to tmp table            | 0.000176
Sorting result                  | 0.000043
Sending data                    | 0.080032
end                             | 0.000004
removing tmp table              | 0.000024
end                             | 0.000006
query end                       | 0.000003
freeing items                   | 0.000148
removing tmp table              | 0.000019
closing tables                  | 0.000005
logging slow query              | 0.000003
cleaning up                     | 0.000004

In this example, most of the time was actually spend sending the data from the server back to the client.

If you've done this, maybe update your question to display the results. And maybe add the data structure, too.

3
  • ok I just posted the results but I think it was cached because this time it came back much faster. It still yet may be useful. Thank you for telling me about this I just had never used the profiler before.
    – hackartist
    Jun 28, 2012 at 6:47
  • Please reset the query cache before executing the query, otherwise we'll only see it received the data from the cache: RESET QUERY CACHE;
    – Bjoern
    Jun 28, 2012 at 6:49
  • 2
    @hackartist, keep in mind that Sending data is actually very deceiving. It is NOT ONLY the time spent sending the data to the client. It's the time from reading the first row until finish sending the last row to the client. In other words, the time spent sending the data to the client can be less than 1% of that value.
    – Adi
    Jun 28, 2012 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.