我有3个表t1,t2,t3,每个表有35K记录。
select t1.col1,t2.col2,t3.col3
from table1 t1,table2 t2,table3 t3
where t1.col1 = t2.col1
and t1.col1 = 100
and t3.col3 = t2.col3
and t3.col4 = 101
and t1.col2 = 102;
返回结果需要更多时间(15秒)。我有适当的索引。
重写它的最佳方式是什么?
答案 0 :(得分:2)
最好在您面前放置Explain Extended来运行查询。这将使您了解它正在使用或未使用的索引。如果需要帮助解析结果,请在问题中包含输出。
答案 1 :(得分:0)
如果您有基于t1.Col1或t1.Col2的索引,请使用THAT作为WHERE子句的第一部分。然后,通过使用“STRAIGHT_JOIN”子句,它告诉MySQL完全按照我在此处列出的那样进行操作。是的,这是旧的ANSI查询语法,它仍然完全有效(正如您最初的那样),但应该快速响应。 where子句的前两个将立即限制数据集,而其余的实际完成与其他表的连接...
select STRAIGHT_JOIN
t1.Col1,
t2.Col2,
t3.Col3
from
table1 t1,
table2 t2,
table3 t3
where
t1.Col1 = 100
and t1.Col2 = 102
and t1.col1 = t2.col1
and t2.col3 = t3.col3
and t3.Col4 = 101