多列复合索引的使用绕过微软sqlserver的一个缺陷

作者:小涵 | 来源:互联网 | 2018-07-15 16:35

阅读: 4292

多列复合索引是指由多个字段组成的索引。这种情况相当常用的,并且,在查询中,用多列复合索引来指定搜索范围边界也相当常用

多列复合索引是指由多个字段组成的索引。这种情况相当常用的,并且,在查询中,用多列复合索引来指定搜索范围边界也相当常用

然而,微软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)

吐了个 "CAO" !

吐个槽吧,看都看了

php复合索引,多列复合索引的使用绕过微软sqlserver的一个缺陷相关推荐

  1. mysql按照列构建索引_列存储索引增强功能–在线和离线(重新)构建

    mysql按照列构建索引 In this article, we will explore ' Clustered columnstore online index build and rebuild ...

  2. 行存储索引改换成列存储索引_列存储索引增强功能–数据压缩,估计和节省

    行存储索引改换成列存储索引 Data compression is required to reduce database storage size as well as improving perf ...

  3. SQL Server 2016新特性:列存储索引新特性

    SQL Server 2016新特性:列存储索引新特性 行存储表可以有一个可更新的列存储索引,之前非聚集的列存储索引是只读的. 非聚集的列存储索引支持筛选条件. 在内存优化表中可以有一个列存储索引,可 ...

  4. SQL Server 2014聚集列存储索引

    转发请注明引用和原文博客(http://www.cnblogs.com/wenBlog) 简介 之前已经写过两篇介绍列存储索引的文章,但是只有非聚集列存储索引,今天再来简单介绍一下聚集的列存储索引,也 ...

  5. pandas索引复合索引dataframe数据、索引dataframe中指定行和指定列交叉格子的数据内容(getting a specific value)、使用元组tuple表达复合索引的指定行

    pandas索引复合索引dataframe数据.索引dataframe中指定行和指定列交叉格子的数据内容(getting a specific value).使用元组tuple表达复合索引的指定行 目 ...

  6. pandas索引复合索引dataframe数据、索引其中一个水平(level)的特定数据列(index a column of a level)

    pandas索引复合索引dataframe数据.索引其中一个水平(level)的特定数据列(index a column of a level) 目录

  7. Pandas中xs()函数索引复合索引数据的不同切面数据(索引复合索引中需要的数据):索引列复合索引中的一个切面、索引行复合索引中的一个切面

    Pandas中xs()函数索引复合索引数据的不同切面数据(索引复合索引中需要的数据):索引列复合索引中的一个切面.索引行复合索引中的一个切面 目录

  8. PostgreSQL 数据库多列复合索引的字段顺序选择原理

    标签 PostgreSQL , 多列索引 , 复合索引 , 驱动列 , 顺序 , 等值查询 , 范围扫描 , 离散值 , 连续值 , 单列索引 , bitmap index scan 背景 当需要创建 ...

  9. 复合非聚集索引里列的顺序的重要性

    当我谈论索引时,大家经常会问我在复合非聚集索引里,列的顺序是否重要?简单来说:"看情况".我们来具体看下为啥"看情况"-- 单例查找(Singleton Loo ...

最新文章

  1. 源码分析-Activity的启动流程
  2. python从list列表中选出一个数和其对应的坐标
  3. 《LeetCode力扣练习》第14题 C语言版 (做出来就行,别问我效率。。。。)
  4. SpringCloud 微服务 (十) 消息队列MQ 基础
  5. Storm 03_Storm 架构设计
  6. titanium开发教程-04-10移动rows
  7. Oracle sqlldr 在DOS窗口导入多列数据到数据库表
  8. html4 form日期,bootstrap4日期时间选择器插件
  9. tomcat配置项目的接种方式
  10. CamTwist 3.4.3最新版(macOS 虚拟摄像头)
  11. 层次分析法 - MATLAB代码详解
  12. 【UOJ 710】魔塔 OL(贪心)(四毛子分块)
  13. 基础知识(三),OSI七层协议、数据传输过程、数据的封装与解封装、IP抓包分析、交换机、路由器、ARP协议、TRUNK中继、VLAN、DHCP中继、ICMP协议、三层交换机
  14. 死锁预防之银行家算法
  15. [树莓派] 轻松制作一个遥控小车(C++,Socket)
  16. 前端导出表格,万级数据,带样式(留自用)
  17. 集丰照明|一帖说清6种防眩射灯应用在家装修上
  18. 【图论】关于邻接表建图
  19. 数字滚动效果(number-flip)
  20. 电商横幅BANNER素材PSD分层模板|多品类,都能借鉴!

热门文章

  1. 《人工智能及其应用》1-6章
  2. 6-3 二叉搜索树中的最近公共祖先 (25 分)
  3. sublime连接Linux进行vim编辑
  4. 如何正确认识大数据分析
  5. 物联网卡封卡原因有哪些
  6. 寻找链表中值域最小的节点并移到链表的最前面
  7. 美赛整理之遗传算法优化BP神经网络的齿轮故障诊断问题
  8. react 打包体积过大_解决 webpack 打包文件体积过大
  9. 小沙的长路(图论+数学)
  10. 2021年北京理工大学ACM CLUB清明节组队训练赛