为什么这个简单的左连接需要永远执行?

时间:2012-12-12 03:23:46

标签: mysql query-optimization left-join

我正在对旧的mysql数据库运行查询,并且许多查询花费的时间远远超过应有的时间。例如,ad_vehicle有60000行,id_ad_link有25000

SELECT * FROM autotalk_identicar_old.ad_vehicle 
left join autotalk_identicar_old.id_ad_link on autotalk_identicar_old.ad_vehicle.vehiclekey=autotalk_identicar_old.id_ad_link.rbvehiclekey;

持续时间是2.323秒,在执行命令后12分钟仍然需要获取(使用mysql工作台,不确定这是否有任何区别)。运行在带有3核myd四核@ 3.4ghz和1GB内存

的虚拟机中

我认为没有任何索引/主键会产生如此大的差异吗?

ad_vehicle的前几行

A145 00AA, PV, ALFA, 145, 2000, 3HBm, 1.6, , , , , P, , 5, 1596, , , 32995,  , , 3HBm 1.6p
A145 00AB, PV, ALFA, 145, 2000, 3HBm, 1.7, , , , , P, , 5, 1712, , , 41995,  , , 3HBm 1.7p
A145 01AA, PV, ALFA, 145, 2001, 3HBm, 1.6, , , , , P, , 5, 1596, , , 32995,  , , 3HBm 1.6p
A145 01AB, PV, ALFA, 145, 2001, 3HBm, 1.7, , , , , P, , 5, 1712, , , 41995,  , , 3HBm 1.7p
A145 02AA, PV, ALFA, 145, 2002, 3HBm, 1.6, , , , , P, , 5, 1596, , , 32995,  , , 3HBm 1.6p
A145 02AB, PV, ALFA, 145, 2002, 3HBm, 1.7, , , , , P, , 5, 1712, , , 41995,  , , 3HBm 1.7p
A145 95AA, PV, ALFA, 145, 1995, 3HBm, 1.6, , L, , , P, , 4, 1596, , , 32995,  , , 3HBm 1.6p L
A145 95AB, PV, ALFA, 145, 1995, 3HBm, 1.7, ELEGANTE, L, , , P, , 4, 1712, , , 41995,  , , 3HBm 1.7p ELEGANTE L
A145 96AA, PV, ALFA, 145, 1996, 3HBm, 1.6, , L, , , P, , 4, 1596, , , 32995,  , , 3HBm 1.6p L
A145 96AB, PV, ALFA, 145, 1996, 3HBm, 1.7, ELEGANTE, L, , , P, , 4, 1712, , , 41995,  , , 3HBm 1.7p ELEGANTE L
A145 97AA, PV, ALFA, 145, 1997, 3HBm, 1.6, , L, , , P, , 4, 1596, , , 32995,  , , 3HBm 1.6p L
A145 97AB, PV, ALFA, 145, 1997, 3HBm, 1.7, ELEGANTE, L, , , P, , 4, 1712, , , 41995,  , , 3HBm 1.7p ELEGANTE L
A145 98AA, PV, ALFA, 145, 1998, 3HBm, 1.6, , L, , , P, , 4, 1596, , , 32995,  , , 3HBm 1.6p L
A145 98AB, PV, ALFA, 145, 1998, 3HBm, 1.7, ELEGANTE, L, , , P, , 4, 1712, , , 41995,  , , 3HBm 1.7p ELEGANTE L
A145 98AC, PV, ALFA, 145, 1998, 4SDm, 2.5, , , , , P, , 5, 2492, , , 65998,  , , 4SDm 2.5p
A145 99AA, PV, ALFA, 145, 1999, 3HBm, 1.7, ELEGANTE, L, , , P, , 4, 1712, , , 41995,  , , 3HBm 1.7p ELEGANTE L
A145 99AB, PV, ALFA, 145, 1999, 3HBm, 1.6, , L, , , P, , 4, 1596, , , 32995,  , , 3HBm 1.6p L
A146 00AA, PV, ALFA, 146, 2000, 5HBm, 1.6, , TS, , , P, , 5, 1596, , , 37995,  , , 5HBm 1.6p TS
A146 01AA, PV, ALFA, 146, 2001, 5HBm, 1.6, , TS, , , P, , 5, 1596, , , 38995,  , , 5HBm 1.6p TS

id_ad_link的前几行

4   10  DHJT 94AA   1994    REDUNDANT   HIJET
12  971 A33  95AA   1995    REDUNDANT   ALFA33
13  971 A33  95AB   1995    REDUNDANT   ALFA33
14  971 A33  95AC   1995    REDUNDANT   ALFA33
61  973 A146 95AB   1995    REDUNDANT   146
60  973 A146 95AA   1995    REDUNDANT   146
59  973 A145 02AB   2002    REDUNDANT   145
58  973 A145 02AA   2002    REDUNDANT   145
57  973 A145 01AB   2001    REDUNDANT   145
56  973 A145 01AA   2001    REDUNDANT   145
55  973 A145 00AB   2000    REDUNDANT   145
54  973 A145 99AB   1999    REDUNDANT   145
53  973 A145 99AA   1999    REDUNDANT   145
52  973 A145 98AC   1998    REDUNDANT   145
45  973 A145 95AB   1995    REDUNDANT   145
44  973 A145 95AA   1995    REDUNDANT   145
70  973 A146 98AB   1998    REDUNDANT   146
71  973 A146 98AC   1998    REDUNDANT   146
72  973 A146 99AA   1999    REDUNDANT   146
73  973 A146 00AA   2000    REDUNDANT   146

更新:

这是

的结果
explain SELECT * FROM autotalk_identicar_old.ad_vehicle 
left join autotalk_identicar_old.id_ad_link on autotalk_identicar_old.ad_vehicle.vehiclekey=autotalk_identicar_old.id_ad_link.rbvehiclekey;

id, select_type, table,       type,    possible_keys, key,  key_len, ref, rows,   Extra
'1', 'SIMPLE', 'ad_vehicle', 'ALL',        NULL,     NULL,   NULL,  NULL, '60433', ''
'1', 'SIMPLE', 'id_ad_link', 'ALL',        NULL,     NULL,   NULL,  NULL, '25571', ''

1 个答案:

答案 0 :(得分:6)

尝试在外键上创建索引:

create index id_ad_link_rbvehiclekey_index on id_ad_link(rbvehiclekey);

如果没有此索引,ad_vehicle的每一行都会导致id_ad_link上的完整表格扫描 使用索引,ad_vehicle的每一行将导致访问一些索引页(可能在内存中),并且读取的页数很少,以获取连接的行,因为索引存储了寻找行的页面。

最小化磁盘I / O对性能至关重要,因为它至少比内存操作慢1000倍。

索引产生巨大差异,特别是在用于连接的列(如外键)