MySQL Forums
Forum List  »  Optimizer & Parser

Indexes won't work if using DISTINCT
Posted by: Andrey M
Date: December 18, 2007 08:06AM

I have two tables:

1.
product_id int(11)
product_name varchar(64)
product_update_date int(11)
product_parameters_md5 varchar(32)
part_id varchar(255)

Indexes:

product_parameters_md5 UNIQUE product_parameters_md5
index_product_id INDEX product_id
index_product_name INDEX product_name
index_product_update_date INDEX product_update_date
index_part_id INDEX part_id

2. id int(11) auto_increment
iproduct_id int(11) icategory_id int(11) imanufacturer_id int(11) idistributor_id int(11) product_name varchar(64)
quantity int(11) packaging varchar(255)
wholesale_price decimal(12,2) s_details varchar(255)
details text
sell_in_online_store int(1)
special_offer int(1) special_offer_details text
posted datetime
active int(1) premier int(11) part_alert int(1) cache_status int(1) product_parameters_md5 varchar(32)

Indexes:

PRIMARY PRIMARY id
idx_c INDEX icategory_id
idx_m INDEX imanufacturer_id
idx_date INDEX posted
idx_d INDEX idistributor_id
idx_md5 INDEX product_parameters_md5

I have a query joining these tables:

SELECT DISTINCT cdn.product_id, cdn.product_parameters_md5, cdn.product_name, cdn.part_id, cdn.product_update_date
FROM jos_dn_cache_inventory_partid_asc cdn, jos_dn_inventory dn
WHERE cdn.product_parameters_md5 = dn.product_parameters_md5
AND dn.quantity >0
AND dn.icategory_id =1
ORDER BY cdn.part_id
LIMIT 0 , 30

The problem here is the ORDER BY cdn.part_id without sorting or without Distinct the query works thousand times faster but I need to have both.

id|select_type|table|type|possible_keys|key|key_len|ref|rows|Extra
1|SIMPLE|cdn|ALL|product_parameters_md5|NULL|NULL|NULL|35018|Using temporary; Using filesort
1|SIMPLE|dn|ref|idx_c,idx_md5|idx_md5|34|cdn.product_parameters_md5| 1 |Using where; Distinct

If you look closer the first table has primary index product_parameters_md5 and it would usually make sense to add the part_id field to that index but the primary index cannot consist of more than one field.

Please advise how to solve this issue. Thanks!

Options: ReplyQuote


Subject
Views
Written By
Posted
Indexes won't work if using DISTINCT
3209
December 18, 2007 08:06AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.