Jump to content

[SOLVED] MySQL Query Efficiency


bendurber

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.