0

我有这个简单的 JOIN:

SELECT my_table1.*,  
       TIME_FORMAT(time1, '%H:%i') AS time1,
       TIME_FORMAT(time2, '%H:%i') AS time2,
       TIME_FORMAT(time3, '%H:%i') AS time3,
       my_table2.*, 
       TIME_FORMAT(time2_saved, '%H:%i') AS time2_saved,
       TIME_FORMAT(time3_saved, '%H:%i') AS time3_saved,
       my_table3.device_id, my_table3.device_token,
       my_table3.device_language, my_table3.app_edition 
  FROM my_table1, my_table2, my_table3
 WHERE my_table1.flight_id = mytable_2.flight_id 
   AND my_table2.device_id = my_table3.device_id AND my_table3.app_edition = '1'

正如 MySQL 报告的那样,这似乎效率低下。索引显然不是此查询的最佳选择。但是,所有表都有索引,但我缺少一些东西。

如果我在查询之前运行 EXPLAIN,MySQL 将返回以下内容:

1
SIMPLE
my_table2
ALL
flight_id,device_id
NULL
NULL
NULL
356

1
SIMPLE
my_table3
eq_ref
device_id
device_id
128
my_databasename.my_table2.device_id
1
Using where

1
SIMPLE
my_table1
ref
flight_id
flight_id
99
my_databasename.my_table2.flight_id
1
Using where

(如果您更喜欢看到干净的桌子,请在此处查看实际屏幕截图:http: //oi48.tinypic.com/20kov3d.jpg

如您所见,第一行显示“ALL”类型,这意味着它使用全表扫描。我在这里失踪了吗?

4

1 回答 1

1

首先,你应该学习正确的join语法,不要,from子句中使用陈旧的东西。

我认为问题是table3。索引对于查询不是最优的。较好的指标是table3(app_edition , device_id)。您可以用这个重新尝试查询。

查询应写为:

SELECT my_table1.*,  
       TIME_FORMAT(time1, '%H:%i') AS time1,
       TIME_FORMAT(time2, '%H:%i') AS time2,
       TIME_FORMAT(time3, '%H:%i') AS time3,
       my_table2.*, 
       TIME_FORMAT(time2_saved, '%H:%i') AS time2_saved,
       TIME_FORMAT(time3_saved, '%H:%i') AS time3_saved,
       my_table3.device_id, my_table3.device_token,
       my_table3.device_language, my_table3.app_edition 
  FROM my_table1 join
       my_table2
       on my_table1.flight_id = mytable_2.flight_id join
       my_table3
       on my_table2.device_id = my_table3.device_id AND my_table3.app_edition = '1'

但是,这可能不会影响优化策略。

于 2013-03-30T16:12:31.533 回答