然而,微软sql server在处理这类索引时,有个重要的缺陷,那就是把本该编译成索引seek的操作编成了索引扫描,这可能导致严重性能下降
举个例子来说明问题,假设某个表T有索引 ( cityid, sentdate, userid), 现在有个分页列表功能,要获得大于某个多列复合索引V0的若干个记录的查询,用最简单表意的方式写出来就是 V >= V0, 如果分解开来,就是:
cityid > @cityid0 or (cityid = @cityid0 and (sentdate > @sentdate0 or (sentdate = @sentdate0 and userid >= @userid0))),
当你写出上述查询时,你会期待sql server会自动的把上述识别为V >= V0类型的边界条件,并使用index seek操作来实施该查询。然而,微软的sql server (2005版)有一个重要缺陷(其他的sql server如何还不得知), 当它遇到这样sql时,sql server就会采用index scan来实施,结果是您建立好的索引根本就没有被使用,如果这个表的数据量很大,那所造成的性能下降是非常大的。
对于这个问题,我曾经提交给微软的有关人士,他们进一步要求我去一个正式的网站上去提交这个缺陷,我懒得去做。
不过,对这个缺陷,还是有个办法能够绕过去的,只要把上面给出的条件变变形,sql server还是能够变回到是用index seek, 而不是低性能的index scan. 具体请看我的英文原文吧(对不起了, 我一旦写了中文,就不想翻成英文,反过来也一样, 估计大家英文都还可以,实在不行的就看黑体部分吧, ):
The seek predicate of the form "x > bookmark_of_x" is needed in paging related query. The compiler has no difficulty to parse it correctly if x is a single column index, or two columns index, however, if x is a three columns index or more, then the compiler will have a hard time to recognize it. This failure will result in that the seek predicate ended up in residue predicate, which results in a much worse execution plan.
To illustrate the point, take a example,
Create table A( a int, b int, c int, d float, primary key (a, b, c))
now check the plan for the query:
select c, d from A where (a> 111 or a= 111 and
(b > 222 or b = 222 and c > 333))
you can see a table scan op is used, and the Where clause ended up in residue predicate.
However, if you rewrite the query in an equivalent form:
select c, d from A where a> 111 or a= 111 and b > 222 or a= 111 and b= 222 and c >333
Then the compiler can choose an index seek op, which is desired.
The problem is, the compiler should be able to recognize the first form of seek predicate on multiple columns index, it saves the user from having to pay extra time to figure out a get-around, not to mention the first form is a more efficient form of same expression.
上面的问题,可以说是部分的绕过去了,但是,也有绕不过的时候,接着看下面一段:
It looks like that sql server lacks a consept of vector bookmark, or vector comparison or whatever you like to call it.
The workaround is not a perfect workaround. If sql server were to understand the concept of vector bookmark, then the following two would be the same in execution plan and performance:
1. select top(n) * from A where vectorIndex >= @vectorIndex
2. select * from A where vectorIndex >= @vectorIndex and vectorIndex <=@vectorIndexEnd
-- @vectorIndexEnd corresponds to the last row of 1.
However, test has shown that, the second statement takes far more time than the first statement, and sql server actually only seek to the begining of the vector range and scan to the end of the whole Index, instead of stop at the end of the vector range.
Not only sql server compile badly when the vector bookmark has 3 columns, test has shown that even with as few as 2 columns, sql serer still can not correctly recognize this is actually a vector range, example:
3. select top (100) a, b, c, d from A where a> 60 or a= 60 and b > 20
4. select a, b, c, d from A where (a> 60 or a= 60 and b > 20) and
(a< 60 or a= 60 and b <= 21),
上面两个查询实质相同(表中的数据刚好如此),并且给出同业的结果集,但是,3比4的速度要快的多,如果去看execution plan也证明3确实应当比4快.
也就是说, 即使在索引vectorIndex只含两列的情况下, sql server也无法正确的理解范围表达式 @vectorIndex0 < vectorIndex < @vectorIndex1, 它能把前半部分正确的解读为seek, 但是, 后半部分无法正确解读, 导致, sql server会一直扫描到整个表的末尾, 而不是在@vectorIndex1处停下来.
以下测试代码, 有兴趣的人可以拿去自己玩:
复制代码 代码如下:
CREATE TABLE [dbo].[A](
[a] [int] NOT NULL,
[b] [int] NOT NULL,
[c] [int] NOT NULL,
[d] [float] NULL,
PRIMARY KEY CLUSTERED ([a] ASC, [b] ASC, [c] ASC)
)
declare @a int, @b int, @c int
set @a =1
while @a <= 100
begin
set @b = 1
begin tran
while @b <= 100
begin
set @c = 1
while @c <= 100
begin
INSERT INTO A (a, b, c, d)
VALUES (@a,@b,@c,@a+@b+@c)
set @c = @c + 1
end
set @b = @b + 1
end
commit
set @a = @a + 1
end
SET STATISTICS PROFILE ON
SET STATISTICS time ON
SET STATISTICS io ON
select top (10) a, b, c, d from A where (a> 60 or a= 60 and
(b > 20 or b = 20 and c >= 31))
select a, b, c, d from A where (a> 60 or a= 60 and
(b > 20 or b = 20 and c >= 31)) and (a< 60 or a= 60 and
(b < 20 or b = 20 and c <= 40))
select top (10) a, b, c, d from A where a> 60 or a= 60 and b > 20 or a= 60 and b= 20 and c >= 31
select a, b, c, d from A where (a> 60 or a= 60 and b > 20 or a= 60 and b= 20 and c >= 31) and
(a< 60 or a= 60 and b < 20 or a= 60 and b= 20 and c <= 40)
select top (100) a, b, c, d from A where a> 60 or a= 60 and b > 20
select a, b, c, d from A where (a> 60 or a= 60 and b > 20) and (a< 60 or a= 60 and b <= 21)
select top (100) a, b, c, d from A where a> 60 or a= 60 and b > 20
select a, b, c, d from A where (a> 60 or a= 60 and b > 20) and (a< 60 or a= 60 and b <= 21)
相关推荐:
SEO找出网站流量提升的终极策略,带你走向搜索引擎巅峰!,朝阳模板网站建设价格
互联网资源的无限潜力:如何利用数字世界为个人和企业创造价值,信阳网站建设正规公司
seo需要了解什么,seo需要学些什么内容 ,ovo ai
SEO站群:打造强大网络营销引擎,助力企业快速提升排名与流量,seo网站排名案例
seo诊断什么意思,seo诊断a5 ,约瑟夫ai
ChatGPT付款被拒?如何应对与解决常见支付问题,掉包ai
ChatGPT5.0为什么一直没出来?背后的技术与战略,元宵节ai趣赏月
SEO技巧提升网站流量:打造高效网站的关键策略,Ai测脸相
SEO优化工具优势:提升网站排名,带来流量和转化的秘密武器,ai圆形造字
ChatGPT198元永久会员,开启智慧之门,体验AI的极致服务!,老孙教ai
SEO无限:如何利用SEO技术实现网站流量爆发?,网站建设总监
SEO发明:引领数字时代营销革命的力量,速卖通外贸推广网站
SEO攻略:搜索引擎优化,快速提升网站排名,网站推广网站优化费用
ChatGPTCanvex打不开?详细分析及解决方法助你顺利使用,ai2.5d网格线去除
什么是seo公司口碑,seo品牌 ,ai大模型训练是什么意思
SEO优化流程:助力网站快速提升排名的关键策略,1745ai
SEO作用:提升网站流量与品牌曝光的秘密武器,美容网站联盟平台推广
360刷排名工具选哪家?揭秘2025年最强排名优化工具!,ai写作网站哪个好一点
SEO优化攻略:如何通过精准策略提升网站排名与流量,aI ow翻译
ChatGPT无法打开?这些解决办法让你重新畅享智能对话!,北大写作ai
AI免费免登录:轻松体验人工智能的魔力,无需繁琐注册,华为AI音箱2音质如何
什么是seo运营,什么是seo营销 ,ai绘画沙地
洗文章AI:让内容创作变得更智能、更高效
文章疑似AI生成怎么办?如何辨别并应对AI生成文章的挑战
AI提供的阅读书目对学生的专业知识有多大帮助,沃奇ai
AI缩写在线:让人工智能助力你行业前沿技术,ai uhrehara
优化网站的秘诀:提高网站速度与用户体验,助力业务腾飞,旅游网站建设步骤
SEO抓取:让您的网站从零到一的秘诀,佛山网站建设哪家效果好
AI撰写工具的无限可能,让内容创作更高效、更精彩!
2024年AI写文章生成器推荐:让创作轻松高效,提升写作水平
seo软件叫什么,seo软件视频教程 ,eps ai 缩略图
好用的AI写作工具,提升写作效率与创意的最佳选择
SEO做网站点击:提升网站流量的关键策略,国内ai写作论文
ChatGPT故障你从未听过的真相,究竟是什么让它偶尔“失灵”?,ai 图片 矢量
怎么用AI缩写文章,轻松提高效率的全新方法
seo软文用什么论坛,seo软文是什么意思 ,爱字幕的AI变脸不见了
WPS改写-轻松提升文档创作效率的秘密武器,推广网站的优势
AI搜索写文章是什么意思?人工智能赋能内容创作的未来,高德地图 ai
SEO监控:精准把握网站排名与优化成效的利器,湖南seo排名商家名单
ChatGPTO1免费:突破智能聊天的极限,体验AI无限可能,糯米ai唱歌
AI免费生成:释放创造力的秘密武器
SEO有点:揭秘优化之道,提升网站排名的秘诀,广州抖音seo厂家地址
什么是seo发外链,seo外链类型有哪些 ,小小苏ai
SEO难吗?揭秘SEO背后的真相,让你轻松上手!,大旺百度网站推广
SEO舆情:如何通过有效的舆情管理提升企业品牌形象,seo推广外包提高收录
GoogleGTP-智能时代的革命性突破,人工智能的新纪元,ai可以降论文ai率吗
AI一键生成文章,写作新境界
SEO优化排名原理解析:如何提高网站排名,实现精准流量获取,奥迪ai售价
seo菲律宾是做什么,菲律宾网址排名 ,openl ai
SEO优化是做什么的?让你的网站流量飙升的秘密武器,乳腺癌ai作用机理