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 / 问题 / 29873
Accepted
Scott
Scott
Asked: 2012-12-06 07:58:39 +0800 CST2012-12-06 07:58:39 +0800 CST 2012-12-06 07:58:39 +0800 CST

SQL Server 索引与统计

  • 772

我应该使用它们之间CREATE INDEX以及CREATE STATISTICS何时使用它们之间有什么区别?

sql-server index
  • 1 1 个回答
  • 12422 Views

1 个回答

  • Voted
  1. Best Answer
    Thomas Stringer
    2012-12-06T08:04:45+08:002012-12-06T08:04:45+08:00

    索引存储实际数据(数据页或索引页取决于我们所讨论的索引类型),而统计存储数据分布。因此,CREATE INDEX将由 DDL 创建索引(聚集、非聚集等),CREATE STATISTICS并由 DDL 创建表内列的统计信息。

    我建议您阅读关系数据的这些方面。下面是一些初学者的介绍性文章。这些是非常广泛的主题,因此有关它们的信息可以非常广泛和深入。在下面阅读它们的一般概念,并在它们出现时提出更具体的问题。

    关于表和索引组织
    的 BOL 参考 关于聚集索引结构
    的 BOL 参考 关于非聚集索引结构的 BOL 参考
    SQL Server Central 关于索引简介的
    BOL 统计参考

    这是一个工作示例,可以查看这两个部分的实际效果(评论解释):

    use testdb;
    go
    
    create table MyTable1
    (
        id int identity(1, 1) not null,
        my_int_col int not null
    );
    go
    
    insert into MyTable1(my_int_col)
    values(1);
    go 100
    
    -- this statement will create a clustered index
    -- on MyTable1.  The index key is the id field
    -- but due to the nature of a clustered index
    -- it will contain all of the table data
    create clustered index MyTable1_CI
    on MyTable1(id);
    go
    
    
    -- by default, SQL Server will create a statistics
    -- on this index.  Here is proof.  We see a stat created
    -- with the name of the index, and the consisting stat 
    -- column of the index key column
    select
        s.name as stats_name,
        c.name as column_name
    from sys.stats s
    inner join sys.stats_columns sc
    on s.object_id = sc.object_id
    and s.stats_id = sc.stats_id
    inner join sys.columns c
    on sc.object_id = c.object_id
    and sc.column_id = c.column_id
    where s.object_id = object_id('MyTable1');
    
    
    -- here is a standalone statistics on a single column
    create statistics MyTable1_MyIntCol
    on MyTable1(my_int_col);
    go
    
    -- now look at the statistics that exist on the table.
    -- we have the additional statistics that's not necessarily
    -- corresponding to an index
    select
        s.name as stats_name,
        c.name as column_name
    from sys.stats s
    inner join sys.stats_columns sc
    on s.object_id = sc.object_id
    and s.stats_id = sc.stats_id
    inner join sys.columns c
    on sc.object_id = c.object_id
    and sc.column_id = c.column_id
    where s.object_id = object_id('MyTable1');
    
    
    -- what is a stat look like?  run DBCC SHOW_STATISTICS
    -- to get a better idea of what is stored
    dbcc show_statistics('MyTable1', 'MyTable1_CI');
    go
    

    以下是统计测试样本的样子:

    在此处输入图像描述

    请注意,统计数据包含数据分布。它们帮助 SQL Server 确定最佳计划。一个很好的例子是,想象你要生活一个重物。如果你知道那个重量是多少,因为上面有重量标记,你就会确定最好的举重方式和肌肉。这就是 SQL Server 对统计信息所做的事情。

    -- create a nonclustered index
    -- with the key column as my_int_col
    create index IX_MyTable1_MyIntCol
    on MyTable1(my_int_col);
    go
    
    -- let's look at this index
    select
        object_name(object_id) as object_name,
        name as index_name,
        index_id,
        type_desc,
        is_unique,
        fill_factor
    from sys.indexes
    where name = 'IX_MyTable1_MyIntCol';
    
    -- now let's see some physical aspects
    -- of this particular index
    -- (I retrieved index_id from the above query)
    select *
    from sys.dm_db_index_physical_stats
    (
        db_id('TestDB'),
        object_id('MyTable1'),
        4,
        null,
        'detailed'
    );
    

    从上面的例子我们可以看出,索引实际上包含了数据(根据索引的类型,叶子页面会有所不同)。

    这篇文章只展示了SQL Server 这两大方面的非常非常简短的概述。这两个都可能占用章节和书籍。阅读一些参考资料,然后您将有更好的把握。

    • 20

相关问题

  • 死锁的主要原因是什么,可以预防吗?

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

  • 如何确定是否需要或需要索引

  • 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