bendurber Posted May 22, 2008 Share Posted May 22, 2008 Hi, I need to optimise a query due to the size of the tables. To set the background I have three tables tbl_company(30k records), tbl_contact(50k records) and tbl_search_results(1M+ records). A company may have many contacts and the table is joined by the field comp_id. I have a list of companies in the search table and want to get all of the contacts associated to those companies. The query below works fine for when the number of records in the search is small but it takes a lot longer when we increase it to 1000, 10000 and all of the companies (30000). SELECT * FROM tbl_search_results r INNER JOIN tbl_contact c ON r.result_link = c.comp_id /*(note, the company id is not the primary key)*/ WHERE search_id='590'; Any ideas as to how I can improve the efficiency would be most appreciated. Thanks Ben Quote Link to comment https://forums.phpfreaks.com/topic/106753-solved-mysql-query-efficiency/ Share on other sites More sharing options...
arianhojat Posted May 22, 2008 Share Posted May 22, 2008 Can you set the company id in the tbl_contact table to be Index key (not Primary, just set to Index)? I assume result_link is Primary or Index-ed field' are these MyISAM databases or InnoDB? maybe set to MyISAM too if dont need foreign keys, etc. Quote Link to comment https://forums.phpfreaks.com/topic/106753-solved-mysql-query-efficiency/#findComment-547353 Share on other sites More sharing options...
bendurber Posted May 22, 2008 Author Share Posted May 22, 2008 I can and will change the company id field to an Indexed field. The database is already set to MyISAM. Is the query that I am using the best way or is there a more efficient way? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/106753-solved-mysql-query-efficiency/#findComment-547374 Share on other sites More sharing options...
fenway Posted May 22, 2008 Share Posted May 22, 2008 Post the EXPLAIN output to make certain that you've done that correctly. Quote Link to comment https://forums.phpfreaks.com/topic/106753-solved-mysql-query-efficiency/#findComment-547399 Share on other sites More sharing options...
bendurber Posted May 22, 2008 Author Share Posted May 22, 2008 id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE r ref search_id,result_link search_id 5 const 42672 Using where 1 SIMPLE c ref comp_id comp_id 5 db_hr.r.result_link 2 Using where Once I ran this query it took around 2.5 seconds and that is to align all of the companies with contacts. Given the previous results its excellent and I suspect I cant really do much more to improve it? Thanks for you help Quote Link to comment https://forums.phpfreaks.com/topic/106753-solved-mysql-query-efficiency/#findComment-547462 Share on other sites More sharing options...
fenway Posted May 23, 2008 Share Posted May 23, 2008 That's about as good as you can get... unless you've picked the wrong sized column types to index. Quote Link to comment https://forums.phpfreaks.com/topic/106753-solved-mysql-query-efficiency/#findComment-548153 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.