2011-01-06 136 views
0

我有两个表:table1有4lacs(400,000)记录,其他有100个记录。Mysql查询优化

我想下面的查询:

SELECT DISTINCT t1.id as id 
FROM table1 t1 
JOIN table2 t2 ON t2.f_id = t1.id 
       AND t1.action = 0; 

我使用MyISAM引擎。 t1.id和t2.f_id是相同的和索引的。

此查询需要30秒。有什么办法可以让它更快。

回答

0

t1.action,t1.id上设置一个索引,在t2.f_id上设置另一个索引。

4

一定要对table1.idtable1.actiontable2.f_id,并假设没有其它指标住在那些导致性能问题或者表,这应该是接近最优的索引:

SELECT 
    DISTINCT t1.id AS id 
FROM 
    table1 AS t1 
    JOIN table2 AS t2 ON t2.f_id = t1.id 
WHERE 
    t1.action = 0 
0

第一步将在0123'上加上idaction的索引。

在查询中引入distinct也会减慢速度。