我有3个大表:tab1,tab2,tab3,tab4。我有一个SQL查询,如:
select * from tab1 a, tab2 b, tab3 c, tab4 d
where
a.c1 = b.c1
and b.c2 = c.c2
and c.c3 = d.c3
and a.c4 = c.c4
and a.c3 = @var3
and a.c4 = @var4
and b.date >= @d1 and b.date <=@d2
and b.c5 =@var5
group by d.c6, a.c4, c.c7
order by d.c6, a.c4, c.c7
创建索引为:对于where条件中的每一列,都会创建一个索引。例如,在上面的查询中使用tab1中有3列:c1,c3,c4。创建了3个索引: c1索引,c3索引,索引onc4。
所有其他表格相同。
问题:
1.如何为那些列创建索引出现在where条件中?单柱指数或综合指数?例如,对于tab1,一个索引包括每列一列的3列c1,c3,c4或3索引?
2.如果也应该为分组中的列创建索引?
3.如果条件在何处重要?例如,以下3对性能有何影响?
where
a.c1 = b.c1
and b.c2 = c.c2
and c.c3 = d.c3
and a.c4 = c.c4 ----duplicate join???
and a.c3 = @var3
and a.c4 = @var4
and b.date >= @d1 and b.date <=@d2
and b.c5 =@var5
或者
where
a.c3 = @var3
and a.c4 = @var4
and b.date >= @d1 and b.date <=@d2
and b.c5 =@var5
and a.c1 = b.c1
and b.c2 = c.c2
and c.c3 = d.c3
and a.c4 = c.c4 --duplicate join???
或
where
a.c3 = @var3
and a.c4 = @var4
and b.date >= @d1 and b.date <=@d2
and b.c5 =@var5
and a.c1 = b.c1
and b.c2 = c.c2
and c.c3 = d.c3
答案 0 :(得分:1)
桌子有多大?什么样的锁定方案表遵循?
通常,聚类索引在远程查询的情况下工作得更好,但如果表遵循DOL方案,那么它就没有用了。
如果不查看表格就很难分辨出应该创建哪些索引,但我最初会像
那样构建它们我也会以较小的部分打破查询: