使用MySQL 5.6我必须专门为这个子选择创建一个索引:
UPDATE site
SET site.shipping_address_id = (
SELECT id
FROM platform_address
WHERE owner_id = site.numeric_id AND owner_type = 'site' AND deleted = 0
ORDER BY modified_time
LIMIT 1
),
site.postal_address_id = (
SELECT id
FROM platform_address
WHERE owner_id = site.numeric_id AND owner_type = 'site' AND deleted = 0
ORDER BY modified_time
LIMIT 1
);
我最初的想法是:
create index tmp_platform_address_for_site on platform_address (owner_id, owner_type, deleted, modified_time, id);
因为它是前3列的精确过滤器。我的理解是,查询规划器足够聪明,可以在此之后使用modified_time
,然后因为id
在索引中,所以根本不必打到表。
但是,该计划如下:
1 PRIMARY site index PRIMARY,ID_UNIQUE,numeric_id_UNIQUE,fk_site_company1,i_default_price_list_id,site_ModifiedTime PRIMARY 108 11 Using temporary
3 DEPENDENT SUBQUERY platform_address ref tmp_platform_address_for_site tmp_platform_address_for_site 8 site.numeric_id,const,const 1 Using where; Using index; Using filesort
2 DEPENDENT SUBQUERY platform_address ref tmp_platform_address_for_site tmp_platform_address_for_site 8 site.numeric_id,const,const 1 Using where; Using index; Using filesort
我错过了什么吗?