2011-08-03 84 views
0

我尝试做如下选择:一个简单的索引连接两个表以年龄

select `table1`.`index2` 
    from `table1`, `table2` 
    where `table1`.`index1` = `table2`.`index1` 
     and `table1`.`index2` != `table2`.`index2` 

index1index2varchar(255)和编制索引。两个表都包含大约50k行。
这个查询花了10分钟,我杀了它,因为它太长了。
为什么需要这么长时间?

+0

都是四个'table1'.'index1','table2'.'index1','table1'.'index2'和'table2'.index2'索引?只是为了确保... – Jacob

+0

为什么不使用左连接或内连接? –

+0

@cularis,是的,他们被索引 – Dani

回答

0

此查询有最佳效果?使用左外连接或内连接有时可以改善查询。

select `table1`.`index2` 
from `table1` 
inner join table2 on `table1`.`index1` = `table2`.`index1` 
    and `table1`.`index2` != `table2`.`index2` 
+0

这已经是隐式语法的内部连接....你的查询和我的一样 – Dani

0

发现问题,与查询无关。
模样创造一个像

index(`index1`, `index2`) 

索引创建某种愚蠢的指数,但创造它像

index(`index1`), 
index(`index2`) 

固定我的问题。第一个做的是什么?