位置: 编程技术 - 正文

多列复合索引的使用 绕过微软sql server的一个缺陷(复合索引顺序)

编辑:rootadmin
然而,微软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 (版)有一个重要缺陷(其他的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> or a= and (b > or b = and c > )) 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> or a= and b > or a= and b= and c > 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 () a, b, c, d from A where a> or a= and b > 4. select a, b, c, d from A where (a> or a= and b > ) and (a< or a= and b <= ), 上面两个查询实质相同(表中的数据刚好如此),并且给出同业的结果集,但是,3比4的速度要快的多,如果去看execution plan也证明3确实应当比4快. 也就是说, 即使在索引vectorIndex只含两列的情况下, sql server也无法正确的理解范围表达式 @vectorIndex0 < vectorIndex < @vectorIndex1, 它能把前半部分正确的解读为seek, 但是, 后半部分无法正确解读, 导致, sql server会一直扫描到整个表的末尾, 而不是在@vectorIndex1处停下来. 以下测试代码, 有兴趣的人可以拿去自己玩:

推荐整理分享多列复合索引的使用 绕过微软sql server的一个缺陷(复合索引顺序),希望有所帮助,仅作参考,欢迎阅读内容。

多列复合索引的使用 绕过微软sql server的一个缺陷(复合索引顺序)

文章相关热门搜索词:多列索引和联合索引,多列复合索引的作用,复合索引顺序,单列索引和复合索引,多个列上创建复合索引,单列索引和复合索引,多列复合索引的作用,复合索引最多几个字段,内容如对您有帮助,希望把文章链接给更多的朋友!

sqlserver索引的原理及索引建立的注意事项小结 聚集索引,数据实际上是按顺序存储的,数据页就在索引页上。就好像参考手册将所有主题按顺序编排一样。一旦找到了所要搜索的数据,就完成了这

SQL Server数据库入门学习总结 一图胜十言:SQLServer数据库总结一个大概的总结经过一段时间的学习,也对数据库有了一些认识。数据库基本是由表,关系,操作组成;对于初学者首

sqlserver 索引的一些总结 1.1.1摘要如果说要对数据库进行优化,我们主要可以通过以下五种方法,对数据库系统进行优化。1.计算机硬件调优2.应用程序调优3.数据库索引优化4.SQL

标签: 复合索引顺序

本文链接地址:https://www.jiuchutong.com/biancheng/348160.html 转载请保留说明!

上一篇:SQL语句分组获取记录的第一条数据的方法(sql实现分组查询的短语)

下一篇:sqlserver索引的原理及索引建立的注意事项小结(sqlserver 索引)

  • 查付款记录需要什么?
  • 增值税发票开票软件金税盘
  • 消费税的计算方法有哪三种
  • 应收账款贷方余额怎么调平
  • 母公司合并子公司会计处理
  • 结转本月销售材料实际成本分录
  • 注销公司详细步骤
  • 股东分红需要开股东会决定吗?
  • 新会计准则 预付房租怎么做账
  • 贴现率与现值系数的关系
  • 基本户购买支票需要什么
  • 辅导期一般纳税人什么意思
  • 建筑安装项目要求有哪些
  • 仓储货架工厂仓库货架
  • 不同的银行存款会收手续费吗
  • 人力资源外包公司排名
  • 营改增之后的增值税
  • 技术服务费可以计入研发费用吗
  • 个人可以开技术服务费发票税率多少
  • 普通发票离线限额为0
  • 2020国家生育津贴多少钱
  • 企业残疾人保障金
  • 电子税务局社保申报截止日期每月
  • 购买用于产品设计拍摄的道具怎么做账?
  • 税务非正常户罚款多少
  • 工会经费的开支必须取得发票么
  • 工商营业年报怎么做
  • 盈余公积的来源及主要用途包括哪些
  • 外贸出口企业退税计算
  • 固定资产无偿转让程序是什么
  • 最新制作视频的软件
  • 公司支付的赔偿金要扣税吗
  • 税控盘抵减
  • Windows 11 CO-21H2 22000.194 正式版官方下载地址(附esd微软三语直链下载x64+arm64)
  • 收到招标费用会计分录
  • 签合同要注意些什么
  • 调频连续波雷达
  • 建设项目罚款支出的会计处理
  • 月末结转本年利润吗
  • 账本登错的账务如何处理
  • 毕业设计基于web难还是JAVA
  • ptech模型
  • 干货!​如何打造我们自己的“ChatGPT”?| 大佬思辨
  • 企业提取盈余公积的比例
  • 织梦百科
  • 净资产小于注册资本
  • 零申报年报
  • 公司不交工会经费有什么后果?工会经费交费时自愿的么?
  • 汽车加油增值税专用发票
  • 小企业购入债券作为长期债券投资,应将相关税费计入
  • 应收账款是指的资产吗
  • 企业小汽车折旧年限
  • 分公司交纳的管理费用
  • 暂估入库冲回与退回区别
  • 冲销以前年度主营业务成本
  • 货款和发票金额不一致
  • 从银行提取现金分录
  • 应付职工社会保险费计入什么科目
  • 出口货物开票怎么处理
  • 财政专用存款账户
  • mysql数据库高可用方案
  • 服务器找不到存储盘
  • mac网易云怎么换皮肤
  • 如何搭建一台内网服务器
  • solaris ip配置
  • vmwarevmx进程是干嘛的
  • win8系统设置
  • ubuntu 14.4
  • ubuntu 18.04怎么用
  • svcproc.exe - svcproc是什么进程 有什么作用
  • u盘做pe后还能存储吗
  • jquery动态生成div
  • python的threadpool
  • 安卓开源好处
  • unity shader 外发光
  • js 设计模式
  • 利用jQuery实现一个时间无法显示
  • 电子税务局怎么添加银行账户信息
  • 行政单位,比如什么单位
  • 税务2021年认真落实各项工作
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设