nirvana Posted December 6, 2006 Share Posted December 6, 2006 Hi All,I am new in this area and have a limited knowledge.Please look at the following two queries which retreive different data points from the same tables. I am puzzled by the time taken to get the data. I am using MySQL database and PHP web interface. The problem started when my graph plotting with JpGraph took a long time and didn't return any graph. It was working fine before and suddenly I didn't get any graph output if the data I queried is more than 700 points on the first query. The query is called 50 times and the graphs are superpositioned together. So to get one graph, 8080 rows need to be retrieved.Before even though it took a while, at least I get the output graph.When I investigated it, I found this:SELECT measured_para_1, measured_para_5FROM tested_device join dwp_data join dwp_data_container where tested_device.device_num = dwp_data.device_num AND dwp_data.test_number = dwp_data_container.test_number AND device_ID = 7660 AND TestGroup = 2 AND parameter = 'spec' AND port = 1 AND attn = 0 AND measured_para_2 = 39 AND temp = 25This query took 10.23sec to retrieve 159 rowsThis second query is called 9 times, altogether 40509 rows need to be retrieved to get one graph which includes 9 superpositioned graphs. It is strange that this query still works and gives output graph although the number of rows are much more than the first one.SELECT measured_para_2, measured_para_3 FROM tested_device join dwp_data join dwp_data_container where tested_device.device_num = dwp_data.device_num AND dwp_data.test_number = dwp_data_container.test_number AND device_ID = 7660 AND parameter = 'xtalk' AND TestGroup = 2 AND measured_para_1 = 1 AND port = 5 AND attn = 0 AND temp = 25This query took 3.16sec to retrieve 1000 rowsDo you guys know why? Is it because there are so many data in the database and that's why taking so long?Is there a way to speed this up? Please let me know if more information is needed.thanks in advance,Nirvana Quote Link to comment Share on other sites More sharing options...
artacus Posted December 6, 2006 Share Posted December 6, 2006 Always use ON with JOIN.Otherwise the number of records it has to go thru is multiplied by the number of records in that table.[code]SELECT measured_para_2, measured_para_3FROM tested_device AS devJOIN dwp_data AS data ON dev.device_num = data.device_numJOIN dwp_data_container AS dat_con ON data.test_number = dat_con.test_numberWHERE dev.device_ID = 7660 AND parameter = 'xtalk' AND TestGroup = 2AND measured_para_1 = 1 AND port = 5 AND attn = 0 AND temp = 25[/code] Quote Link to comment Share on other sites More sharing options...
artacus Posted December 6, 2006 Share Posted December 6, 2006 Oh, and to qualify my always us ON statement. If you do need to join every record in one table to every record in another, you can use "JOIN ON 1=1" Quote Link to comment Share on other sites More sharing options...
fenway Posted December 9, 2006 Share Posted December 9, 2006 If it's still slow, post the EXPLAIN. Quote Link to comment Share on other sites More sharing options...
nirvana Posted January 7, 2007 Author Share Posted January 7, 2007 Hi,Thanks for the help and sorry for late response as I was away.I did try the code with join on but it's still slow, the similar timing as before.Can you please explain?thanks in advance,[quote author=fenway link=topic=117558.msg481537#msg481537 date=1165632697]If it's still slow, post the EXPLAIN.[/quote] Quote Link to comment Share on other sites More sharing options...
fenway Posted January 7, 2007 Share Posted January 7, 2007 Run the SELECT query with "EXPLAIN" in front of it, and post the output. Quote Link to comment Share on other sites More sharing options...
nirvana Posted January 8, 2007 Author Share Posted January 8, 2007 HiThe following is the output:I copy and paste straight out from the database and not suer the table alignment will be shifted.thanks for your help.regards,+----+-------------+--------------------+--------+---------------+---------+---------+----------------------------------------------+----------+-------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+--------------------+--------+---------------+---------+---------+----------------------------------------------+----------+-------------+| 1 | SIMPLE | dwp_data_container | ALL | [NULL] | [NULL] | [NULL] | [NULL] | 24610672 | Using where || 1 | SIMPLE | dwp_data | eq_ref | PRIMARY | PRIMARY | 4 | manufacturing.dwp_data_container.test_number | 1 | Using where || 1 | SIMPLE | tested_device | eq_ref | PRIMARY | PRIMARY | 4 | manufacturing.dwp_data.device_num | 1 | Using where |+----+-------------+--------------------+--------+---------------+---------+---------+----------------------------------------------+----------+-------------+[quote author=fenway link=topic=117558.msg498957#msg498957 date=1168179140]Run the SELECT query with "EXPLAIN" in front of it, and post the output.[/quote] Quote Link to comment Share on other sites More sharing options...
fenway Posted January 9, 2007 Share Posted January 9, 2007 You're clearly missing appropriate indexes on "dwp_data_container"... 24610672 rows examined?!?? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.