I’m running queries on an old mysql database and many queries are taking far longer than they should. for example this one, ad_vehicle has 60000 rows, id_ad_link has 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;
duration is 2.323 seconds, fetch is still going 12 minutes after hitting execute (using mysql workbench, not sure if that makes any difference though). running in a vm with 3 cores of my amd quad core @ 3.4ghz with 1gb memory
I don’t think there are any indexes/primary keys however would that make such a big difference?
first few rows of 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
first few rows of 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
UPDATE:
here’s the result of
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', ''
Try creating an index on the foreign key:
Without this index, each row of
ad_vehiclewill cause a full tablescan onid_ad_link.With the index, each row of
ad_vehiclewill result in a few index pages being accessed (probably in-memory) and as few pages read to fetch the joined rows, because the index stores the page(s) to find the rows.Minimizing disk I/O is critical to performance, because it’s at least 1000 times slower than memory operations.
Indexes make a huge difference, especially on columns used for joins (like foreign keys)