我很难比较两个简单的查询计划。
第一个查询是:
EXPLAIN
SELECT prod.id
FROM product prod, product_by_client pbc
WHERE id_cat=656889
AND pbc.id_pr=prod.id
AND pbc.has_pr=1
AND pbc.is_ready=1
AND pbc.id_prod_original=0
AND prod.is_discontinued=0
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE pbc ref ...has_pr has_pr 4 const 711972 Using where
1 SIMPLE prod eq_ref id,... id 4 id_pr 1 Using where
强制使用另一个密钥后的第二个是:
EXPLAIN
SELECT prod.id
FROM product prod USE index (id_cat), product_by_client pbc
WHERE id_cat=656889
AND pbc.id_pr=prod.id
AND pbc.has_pr=1
AND pbc.is_ready=1
AND pbc.id_prod_original=0
AND prod.is_discontinued=0
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE prod ref id_cat id_cat 4 const 138 Using where
1 SIMPLE pbc ref id_pr id_pr 4 id 25963 Using where
我的问题是为什么第二个比第一个更快?
138 * 25693 = 3545634,远远超过711972
感谢。
修改
这是product_by_client表上的索引:
Keyname Type Unique Packed Column Cardinality Collation
id_pr BTREE Yes No id_pr 0 A
id_client 1950268 A
id_mapped_to BTREE No No id_mapped_to 19309 A
is_duplicate BTREE No No id_prod_original 34215 A
is_in_sf BTREE No No is_in_sf 2 A
is_ready BTREE No No is_ready 2 A
has_pr BTREE No No has_pr 2 A
is_changed BTREE No No is_cat_changed 2 A
is_data_changed BTREE No No is_data_changed 2 A
以及根据这些查询的表产品的一些索引:
Keyname Type Unique Packed Column Cardinality Collation
id BTREE Yes No id 948019 A
id_fref BTREE Yes No id_fref 948019 A
number BTREE No No number 948019 A
id_cat BTREE No No id_cat 32690 A
is_disc BTREE No No is_discontinued 2 A