索引要加入的查询的结果

时间:2014-10-31 20:57:26

标签: mysql database join optimization

我需要知道是否可以索引子查询的结果以便与另一个结果连接。 我有查询:

Select A.*, B.* from (select * from table1 where condition1 = 1) A join (select * from table2 where condition2 = 0) A on A.c1 = B.c1 and A.c2 = B.c2

我假设我需要索引子查询的结果以提高响应的速度:

select * from table1 where condition1 = 1 (Id like to index the fields c1, c2)

select * from table2 where condition2 = 0 (Id like to index the fields c1, c2)
这可能吗?我想通过索引子查询来做到这一点。 感谢

2 个答案:

答案 0 :(得分:0)

在子查询上使用索引没有用处。计算索引需要扫描子查询的整个结果,所以没有任何好处。

但是,如果子查询的结果很少更改,则可以添加另一个存储中间结果的表,并为该表添加索引。如果table1table2中的数据已更新,请确保同时更新其他表格。使用触发器可以实现自动更新。

答案 1 :(得分:0)

您可以使用内部查询创建临时表并将其编入索引,然后在临时表上使用外部查询。

Create index on temp table

When to Add Indexes to temp tables