AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 13111
Accepted
GSerg
GSerg
Asked: 2012-02-16 08:41:18 +0800 CST2012-02-16 08:41:18 +0800 CST 2012-02-16 08:41:18 +0800 CST

过滤索引:为什么要包含过滤字段?

  • 772

这让我发疯。

考虑一个删除了不相关列的简单表:

create table boxes (
  row_id int not null identity(1,1) primary key,
  location varchar(15) null,
  dismantled bit not null default 0,
  rank int not null default 0
  /* irrelevant columns, text and numbers */
)

对于某个重要的查询,我想要一个过滤索引:

create nonclustered index anindex ON boxes (rank)
where (dismantled=0 and location is null)

这将创建一个索引,其中包含大约 150k 中的大约 150 条记录。这很好。

现在我们查询表:

select top (1) row_id
from boxes
where dismantled = 0 and location is null
order by rank;

执行计划很奇怪。索引扫描anindex伴随着书签查找,使用 seekedrow_id来确认location是null. 选择通过查找的记录。

为什么在地球上?location是null根据所用索引的定义,不是吗?

现在,如果我做了一件愚蠢的事情并包含location在过滤索引中......

create nonclustered index anindex ON boxes (location, rank)
where (dismantled=0 and location is null)

然后突然间执行计划很棒,索引搜索anindex没有查找。

这只是一个例子,每次我想使用过滤索引时都会遇到这个问题。每次我最终被迫将过滤索引的无用字段包含到索引字段列表中时,只有这样服务器才能正确使用索引;否则,它要么扫描它,要么完全忽略它。

是什么赋予了?包含无用字段是推荐的做法吗?

sql-server-2008 index
  • 1 1 个回答
  • 161 Views

1 个回答

  • Voted
  1. Best Answer
    Martin Smith
    2012-02-16T10:21:30+08:002012-02-16T10:21:30+08:00

    这之前已在 Connect 和Closed 上作为“Won't Fix”提出。

    我们将关闭它,因为它似乎是一个狭窄的场景,我们看不到在可预见的未来对此做任何事情。

    这在我看来非常令人失望。也许对该项目进行投票和评论。

    编辑: BOL 有一个在过滤索引中使用的示例,它不执行键查找,但替换为确实会导致查找,因此看起来好像有时它被避免了......WHERE EndDate IS NOT NULLIS NOT NULLIS NULL

    • 3

相关问题

  • 我在索引上放了多少“填充”?

  • 是否有开发人员遵循数据库更改的“最佳实践”类型流程?

  • 从 SQL Server 2008 降级到 2005

  • RDBMS 上的“索引”是什么意思?[关闭]

  • 如何在 MySQL 中创建条件索引?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何查看 Oracle 中的数据库列表?

    • 8 个回答
  • Marko Smith

    mysql innodb_buffer_pool_size 应该有多大?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    从 .frm 和 .ibd 文件恢复表?

    • 10 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    如何选择每组的第一行?

    • 6 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST
  • Martin Hope
    bernd_k 什么时候应该使用唯一约束而不是唯一索引? 2011-01-05 02:32:27 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve