sql 查询数据库索引重建

Poor indexing is one of the top performance killers, and we will focus on them in this article.

索引编制不良是导致性能下降的主要原因之一,在本文中我们将重点介绍它们。

什么是索引? (What are indexes?)

An index is used to speed up data search and SQL query performance. The database indexes reduce the number of data pages that have to be read in order to find the specific record.

索引用于加快数据搜索和SQL查询的性能。 数据库索引减少了为找到特定记录而必须读取的数据页数。

The biggest challenge with indexing is to determine the right ones for each table.

索引的最大挑战是为每个表确定正确的索引。

We will start with explaining clustered and nonclustered indexes.

我们将从解释聚簇索引和非聚簇索引开始。

A table without a clustered index is called a heap, due to its unordered structure. Data in a heap table isn’t sorted, usually the records are added one after another, as they are inserted into the table. They can also be rearranged by the database engine, but again, without a specific order. When you insert a lot of rows into a heap table, the new records are written on data pages without a specific order. Finding a record in a heap table can be compared to finding a specific leaf in a heap of leaves. It is inefficient and requires time.

没有聚簇索引的表由于其无序结构而称为堆。 堆表中的数据不进行排序,通常是在将记录插入表中时,将记录一个接一个地添加。 它们也可以由数据库引擎重新排列,但同样,也无需特定顺序。 当您在堆表中插入很多行时,新记录将以特定顺序写入数据页。 在堆表中查找记录可以与在叶子堆中查找特定叶子进行比较。 它效率低下,需要时间。

A heap can have one or several nonclustered indexes, or no indexes at all.

堆可以具有一个或几个非聚集索引,或者根本没有索引。

A nonclustered index is made only of index pages that contain row locators (pointers) for records in data pages. It doesn’t contain data pages, like clustered indexes.

非聚集索引仅由索引页面组成,这些索引页面包含数据页面中记录的行定位器(指针)。 它不包含数据页,如聚簇索引。

A clustered index organizes table data, so data is queried quicker and more efficiently. A clustered index consists of both index and data pages, while a heap table has no index pages; it consists only of data pages. In other words, it is not just an index, i.e. a pointer to the data row that contains the key value, but it also contains table data. The data in the clustered table is sorted using the values of the columns the clustered index is made of.

聚集索引组织表数据,因此可以更快,更有效地查询数据。 聚集索引由索引和数据页组成,而堆表没有索引页。 它仅包含数据页。 换句话说,它不仅是索引,即指向包含键值的数据行的指针,而且还包含表数据。 聚簇表中的数据使用构成聚簇索引的列的值进行排序。

Finding a record from a table with a proper clustered index is quick and easy like finding a name in an alphabetically ordered list. A general recommendation for all SQL tables is to have a proper clustered index

从具有适当聚集索引的表中查找记录非常容易,就像在按字母顺序排列的列表中查找名称一样。 对于所有SQL表的一般建议是拥有适当的聚集索引

While there can be only one clustered index on a table, a table can have up to 999 nonclustered indexes

虽然表上只能有一个聚集索引,但一个表最多可以有999个非聚集索引

Indexes can be created using T-SQL.

可以使用T-SQL创建索引。


CREATE TABLE [Person].[Address]([AddressID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,[AddressLine1] [nvarchar](60) NOT NULL,[AddressLine2] [nvarchar](60) NULL,[City] [nvarchar](30) NOT NULL,
CONSTRAINT [PK_Address_AddressID] PRIMARY KEY CLUSTERED
([AddressID] ASC
) ON [PRIMARY]

When you execute the select statement on a clustered table where an ascending clustered index is created, the results will be ordered ascending by the clustered key column. In this example, it’s the AddressID column.

在创建升序聚簇索引的聚簇表上执行select语句时,结果将按聚簇键列升序排列。 在此示例中,它是AddressID列。

The same table, but with a descending clustered index will return the results sorted by the AddressID column descending. To create a descending clustered index, just replace ASC with DESC in code above, so the constraint syntax becomes.

相同的表,但聚集索引为降序,将返回按AddressID列降序排序的结果。 要创建降序聚集索引,只需在上面的代码中将ASC替换为DESC,约束语法就会变成。


CONSTRAINT [PK_Address_AddressID] PRIMARY KEY CLUSTERED
([AddressID] DESC
)

The select statement on this table returns the AddressID column sorted descending.

此表上的select语句返回降序排列的AddressID列。

T-SQL code to create a table with a nonclustered index:

使用T-SQL代码创建具有非聚集索引的表:


CREATE TABLE [Person].[Address4]([AddressID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,[AddressLine1] [nvarchar](60) NOT NULL,[AddressLine2] [nvarchar](60) NULL,[City] [nvarchar](30) NOT NULL)CREATE NONCLUSTERED INDEX [IX_Address_StateProvinceID4] ON [Person].[Address4]
([AddressID] ASC
) ON [PRIMARY]

When you execute the select statement on a heap table with the same columns and data, the results returned will be unordered.

在具有相同列和数据的堆表上执行select语句时,返回的结果将是无序的。

Besides using T-SQL code to create an index, you can use SQL Server Management Studio. To create an index on an existing table:

除了使用T-SQL代码创建索引外,您还可以使用SQL Server Management Studio。 要在现有表上创建索引:

  1. Tables node in 对象资源管理器中展开“ Object Explorer表”节点
  2. Expand the table where you want to create the index and right-click it 展开要在其中创建索引的表,然后右键单击它
  3. Indexes索引
  4. New Index新索引
  5. Select Clustered Index or Non-Clustered Index option

    选择“ 聚集索引”或“ 非聚集索引”选项

  6. Clustered Index option, the following dialog is shown. The index name is generated automatically, but it’s not very descriptive, so it’s recommended to change it and add the clustered index column names, e.g. ClusteredIndex_AddressID聚簇索引”选项,则会显示以下对话框。 索引名称是自动生成的,但是描述性不强,因此建议更改它并添加聚簇索引列名称,例如ClusteredIndex_AddressID
  7. Click Add

    点击添加

  8. Select the column(s) you want to use as a clustered index

    选择要用作聚簇索引的列

  9. OK. The selected column(s) will be listed in the 确定 。 所选列将列在“ Index key columns list索引键列”列表中
  10. Unique check box唯一”复选框
  11. Use other tabs to set index options, storage options, and extended properties使用其他选项卡设置索引选项,存储选项和扩展属性

When the index in created successfully, it will be listed in the Indexes node for the specific table

成功创建索引后,它将在特定表的“ 索引”节点中列出

The steps are similar for creating a nonclustered index

创建非聚集索引的步骤类似

Another option to create a clustered index on the existing table using SQL Server management Studio is to:

使用SQL Server Management Studio在现有表上创建聚簇索引的另一种方法是:

  1. Design设计”。
  2. Indexes/Keys索引/键
  3. Click Add. By default, the identity column is added in the ascending order

    点击添加 。 默认情况下,标识列按升序添加

  4. To select another column, click the ellipsis button in the Columns row and select another column and sorting order

    要选择另一列,请单击“列”行中的省略号按钮,然后选择另一列和排序顺序

  5. Create As Clustered column select 作为群集创建”列中,为群集索引选择“ Yes for a clustered index. Leave ”。 保留“ No to create a nonclustered index否”以创建非聚集索引
  6. Again, it’s recommended to change the automatically created name in the Identity – (Name) row by a more descriptive one
  7. 同样,建议将“ 身份”((名称))行中自动创建的名称更改为更具描述性的名称
  8. Close关闭
  9. Save in the SQL Server management Studio menu to save the index保存 Studio菜单,保存指数

堆或群集SQL表? (Heap or clustered SQL table?)

When searching for a specific record in a heap table, SQL Server has to go through all table rows. This can be acceptable on a table with a small number of records.

在堆表中搜索特定记录时,SQL Server必须遍历所有表行。 这在具有少量记录的表上是可以接受的。

As rows in a heap table are unordered, they are identified by a row identifier (RID) which consists of the file number, page number, and slot number of the row.

由于堆表中的行是无序的,因此它们由行标识符(RID)标识,该标识符由该行的文件号,页号和插槽号组成。

It’s not recommended to use a heap table if you’re going to store a large number of records in the table. SQL query execution on a table with millions of records without a clustered index requires a lot of time. Also, if you need to get a sorted results list, it’s easier to define an ascending or descending clustered index, as shown in the examples above, than to sort the unsorted results set retrieved from a heap table. The same goes for grouping, filtering by a value range.

如果要在表中存储大量记录,则不建议使用堆表。 对具有数百万条记录且没有聚集索引的表执行SQL查询需要大量时间。 另外,如果需要获取排序结果列表,则如上面示例所示,定义升序或降序聚集索引比对从堆表中检索的未排序结果集进行排序更容易。 分组,按值范围过滤也是如此。

In this article, we showed how to create clustered and nonclustered indexes using T-SQL and SQL Server Management Studio options, and pointed out the main differences between a clustered and heap table. In the next part of this article, we will explain what is considered to be bad indexing practice and give recommendations for creating indexes.

在本文中,我们展示了如何使用T-SQL和SQL Server Management Studio选项创建群集索引和非群集索引,并指出了群集表和堆表之间的主要区别。 在本文的下一部分中,我们将解释什么是不良的索引编制实践,并提供有关创建索引的建议。

翻译自: https://www.sqlshack.com/sql-query-performance-killers-understanding-poor-database-indexing/

sql 查询数据库索引重建

sql 查询数据库索引重建_SQL查询性能的杀手– –了解不良的数据库索引相关推荐

  1. mysql 查询总数时条件_SQL查询数据库中符合条件的记录的总数

    1. select count(*) from table; //统计元组个数 2. select count(列名) from table; //统计一列中值的个数 3. select count( ...

  2. python查询数据库语句大全_sql:查询语句大全

    一.mysql 查看数据库:SHOW DATABASES; 创建数据库:CREATE DATABASE db_name; 使用数据库:USE db_name; 删除数据库:DROP DATABASE ...

  3. sql 按照天环比_SQL 查询同比,环比

    一.要求 1.表结构如下: ID DepartName(部门) Sales(销售量) SalesDate(销售日期) 1 营销一部 300 2006-7-1 2 营销二部 500 2006-7-1 3 ...

  4. mysql查询字段大于小于_sql查询大于字段的所有数据,或小于字段的所有数据

    展开全部 例如查询时间字段62616964757a686964616fe58685e5aeb931333431353962在2008-01-01(含)至2008-10-31(含)之间的记录可以这么写: ...

  5. mysql查询第二大的_sql查询最大的见多了,查询第二的呢???

    问题: 数据库中人表有三个属性,用户(编号,姓名,身高),查询出该身高排名第二的高度. 建表语句 create tableusers ( idint identity(1,1) primary key ...

  6. mysql查询的长度限制_SQL查询的长度的实际限制(特别是MySQL)

    阅读你的查询让我想玩RPG. 这绝对不会太长.只要格式很好,我会说实际的限制是大约100行.之后,你最好不要将子查询分解成意见,以防止双眼过渡. 我已经处理了一些1000行的查询,这很难调试. 顺便提 ...

  7. mysql查询结果横向显示出来_SQL查询结果横向显示

    第一张表S,车类型表 第二张表C,姓名表 第三张表SC,两个表的关系还有价格 我把他们加在一个视图里面.视图代码 SELECT     dbo.s.id AS s_id, dbo.c.id AS c_ ...

  8. mysql查询月销售数量_sql 查询每月的销售金额

    sql数据分月统计,表中只有每天的数据,现在要求求一年中每个月的统计数据(一条sql) SELECT MONTH (那个日期的字段  ), SUM(需要统计的字段,比如销售额什么的 ) FROM 表 ...

  9. mysql查看索引创建进度_SQL Server查看索引重建、重组索引进度

    相信很多SQL Server DBA或开发人员在重建或重组大表索引时,都会相当郁闷,不知道索引重建的进度,这个对于DBA完全是一个黑盒子,对于系统负载非常大的系统或维护窗口较短的系统,你会遇到一些挑战 ...

最新文章

  1. html中内联的form,bootstrap3.0教程之表单(form)使用详解
  2. Python扩展库numpy中where()函数的三种用法
  3. ZeroMemory(百度百科 ZeroMemory)
  4. Hyperledger Fabric服务器配置及修改Docker容器卷宗存储根目录/位置
  5. 映日荷花别样红是什么季节,映日荷花别样红的上一句是什么?全诗赏析
  6. 【zabbix解决value too small or too large】
  7. linux 软件查询,linux安装常用软件和查询基本信息
  8. Android保存照片到相册
  9. 线材检测项目(基于QT)
  10. LCD液晶拼接屏优势凸显受市场欢迎
  11. Uber天使投资人杰森卡拉卡尼斯投资…
  12. 【转载】MAC OS X常用快捷键
  13. 数据库之Mysql索引、事务与存储引擎
  14. Programming Ruby 读书笔记(六)
  15. 【渝粤题库】陕西师范大学201511先秦历史散文研究 作业(专升本)
  16. 梦幻西游手游最多人的服务器,梦幻西游手游哪个区人多及区服选择分析
  17. CAS算法与ABA问题
  18. 国内最全的Android市场,最全Android软件商店
  19. webRTC(十一):webrtc 实时共享桌面
  20. 2019百度云智峰会重磅发布:全自研昆仑云服务器、智能来电小秘书

热门文章

  1. centos7安装telnet服务
  2. WIN10 64位 JDK的安装
  3. hadoop-0.20.2完全分布式集群
  4. 【Unity入门】场景、游戏物体和组件的概念
  5. sql中全文检索的具体细节
  6. http,tcp的长连接和短连接
  7. 一个家庭女人太强势,这个家庭会怎样?
  8. 为什么有的人开车舍不得开空调?车载空调耗油吗?
  9. 为什么今年好多人开始买基金了,是疫情影响的吗?
  10. 【无一时】的意思和解释