您现在的位置: 365建站网 > 365文章 > The sequence of columns in an clustered index

The sequence of columns in an clustered index

文章来源:365jz.com     点击数:166    更新时间:2009-09-18 10:32   参与评论
  Simplily say, If you have a CI(clustered index) which is composed by 4 columns, Which one of the 4 would you put into the first one in the CI? and Which would be put into the last? Or you shouldn't consider this question at all, just put them casually togather to form a CI?
  Actually I never thought about this question before, until I came to a problem, and now let me describe it to you. I have a table A, and the table have a CI on it. I had many queries about the table, and all the queries used the CI, and they worked well. One day for some reason I have to add an new column into the table, and also I have to add the column into the CI. The old CI have 5 columns, and I insert the new column between the 1st CI column and 2nd CI column(This insertion make the new column very important to the table, which I didn't understand at that time). After add the new column and rebuild the index, the old querys became slow. That's  not surprising because they didn't use the new CI. Let's see the example code of a simple query:
Select * from A
where col1 = 'something'
and col2 = 30
and col3 = 50
and col4 = 70
and col5 between 30 and 40
I changed the query to make it use the new CI:
Select * from A
where col1 = 'something'
and newCol between 1 and 4
and col2 = 30
and col3 = 50
and col4 = 70
and col5 between 30 and 40
When I run the new query, it's much slower than I had estimated. I checked the execution plan, the CI was used. But when I check the logic io, there was a huge logical read here.
So what's the problem? The between operation for the new column seems a little suspected, but as you could see from the code above, in old querys we implemented between to col5 too, and it worked well! I tried to change the new query like this:
 Select * from A
where col1 = 'something'
and newCol in (select value from aEnumTable)
and col2 = 30
and col3 = 50
and col4 = 70
and col5 between 30 and 40
And it is fast now, and the number of logical reads dramatically decreased to a very small value. So why can we put "between" operation on col5 but not on newCol? Now you got the anwser: Because they are not at the sequence in the CI. CI is builded as a B-Tree, and when the tree is builded, the columns which has smaller sequence number define the main order of all indexes.
 

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (166人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号