Jump to content

MYSQL Join Optimization


a1amattyj

Recommended Posts

Hello,

 

I currently have 2 tables, a deals table and a handset table. Now, only the handset table has a reference to a manufacturer. I wish to list the top 20 DEALS for a given manufacturer. So the easiest way to do this would be to use the handset table; each deal is linked to a handset, and hence, has the manufacturer ID required to produce results. Tables below:

 

'Deals' Table - 2.3m rows

 

|Column|Type|Null|Default

|deal|text|No|

|handset|int(15)|No|

|tarrif|int(15)|No|

........

 

'Handsets' Table - 5000 rows

 

|Column|Type|Null|Default

|deal|text|No|

|handset|int(15)|No|

|handet_numeric|int(15)|No|

|tarrif|int(15)|No|

...

|popularity|int(15)|No|

|manufacturer_id|int(15)|No|

 

Here is the query I have come up with which isn't giving me fantastic results, for manufacturer ID 3, top 20.

 

	SELECT distinct *  FROM `deals` LEFT JOIN `handsets`
	ON deals.handset = handsets.handet_numeric
	WHERE `handsets`.`manufacturer_id` = '3'
	ORDER BY `deals`.`popularity` DESC
	LIMIT 20

 

This query takes around 53 seconds too! Not very well optimised.. Wondered if anyone could let me know a way to optimise it?

 

I've added an index for handsets.handet_numeric.

 

Thanks

Link to comment
Share on other sites

Thanks for the help, I've implemented all the changes you have given but the speed is still an issue:

 

Showing rows 0 - 19 ( 20 total, Query took 4.4153 sec) [popularity: 84 - 80]

 

SELECT DISTINCT *

FROM deals d

JOIN handsets h ON ( d.handset = h.handet_numeric )

WHERE h.manufacturer_id =  '9'

ORDER BY d.popularity DESC

LIMIT 20

 

Thanks!

 

Give this a go:

 

select * from deals d join handsets h on(d.handset = h.handet_numeric) where h.manufacturer_id = 3 order by d.popularity desc limit 20;

 

you would want to index:

 

deals.handset

handset.handet_numeric

 

you may even want a doube index on:

handset.handet_numeric, handset.manufacturer_id

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.