Home Database Mysql Tutorial 重建索引提高SQL Server性能

重建索引提高SQL Server性能

Jun 07, 2016 pm 03:15 PM
server sql performance improve index reconstruction

大多数SQL Server表需要 索引 来 提高 数据的访问速度,如果没有 索引 ,SQL Server 要进行表格扫描读取表中的每一个记录才能找到索要的数据。 索引 可以分为簇 索引 和非簇 索引 ,簇 索引 通过重排表中的数据来 提高 数据的访问速度,而非簇 索引 则通过维

大多数SQL Server表需要索引提高数据的访问速度,如果没有索引,SQL Server 要进行表格扫描读取表中的每一个记录才能找到索要的数据。索引可以分为簇索引和非簇索引,簇索引通过重排表中的数据来提高数据的访问速度,而非簇索引则通过维护表中的数据指针来提高数据的索引

 

1. 索引的体系结构

为什么要不断的维护表的索引?首先,简单介绍一下索引的体系结构。SQL Server在硬盘中用8KB页面在数据库文件内存放数据。缺省情况下这些页面及其包含的数据是无组织的。为了使混乱变为有序,就要生成索引。生成索引后,就有了索引页和数据页,数据页保存用户写入的数据信息。索引页存放用于检索列的数据值清单(关键字)和索引表中该值所在纪录的地址指针。索引分为簇索引和非簇索引,簇索引实质上是将表中的数据排序,就好像是字典的索引目录。非簇索引不对数据排序,它只保存了数据的指针地址。向一个带簇索引的表中插入数据,当数据页达到100%时,由于页面没有空间插入新的的纪录,这时就会发生分页,SQL Server 将大约一半的数据从满页中移到空页中,从而生成两个半的满页。这样就有大量的数据空间。簇索引是双向链表,在每一页的头部保存了前一页、后一页地址以及分页后数据移动的地址,由于新页可能在数据库文件中的任何地方,因此页面的链接不一定指向磁盘的下一个物理页,链接可能指向了另一个区域,这就形成了分块,从而减慢了系统的速度。对于带簇索引和非簇索引的表来说,非簇索引的关键字是指向簇索引的,而不是指向数据页的本身。

 

为了克服数据分块带来的负面影响,需要重构表的索引,这是非常费时的,因此只能在需要时进行。可以通过DBCC SHOWCONTIG来确定是否需要重构表的索引

 

2. DBCC SHOWCONTIG用法

下面举例来说明DBCC SHOWCONTIG和DBCC REDBINDEX的使用方法。以应用程序中的Employee数据表作为例子,在 SQL Server的Query analyzer输入命令:

use database_name

declare @table_id int

set @table_id=object_id('Employee')

dbcc showcontig(@table_id)

 

输出结果:

DBCC SHOWCONTIG scanning 'Employee' table...

Table: 'Employee' (1195151303); index ID: 1, database ID: 53

TABLE level scan performed.

- Pages Scanned................................: 179

- Extents Scanned..............................: 24

- Extent Switches..............................: 24

- Avg. Pages per Extent........................: 7.5

- Scan Density [Best Count:Actual Count].......: 92.00% [23:25]

- Logical Scan Fragmentation ..................: 0.56%

- Extent Scan Fragmentation ...................: 12.50%

- Avg. Bytes Free per Page.....................: 552.3

- Avg. Page Density (full).....................: 93.18%

DBCC execution completed. If DBCC printed error messages, contact your system administrator.

通过分析这些结果可以知道该表的索引是否需要重构。如下描述了每一行的意义:

信息                                           描述

Pages Scanned                    表或索引中的长页数

Extents Scanned                 表或索引中的长区页数

Extent Switches                  DBCC遍历页时从一个区域到另一个区域的次数

Avg. Pages per Extent         相关区域中的页数

Scan Density[Best Count:Actual Count]       

Best Count是连续链接时的理想区域改变数,Actual Count是实际区域改变数,Scan Density为100%表示没有分块。

Logical Scan Fragmentation   扫描索引页中失序页的百分比

Extent Scan Fragmentation    不实际相邻和包含链路中所有链接页的区域数

Avg. Bytes Free per Page       扫描页面中平均自由字节数

Avg. Page Density (full)         平均页密度,表示页有多满

 

 从上面命令的执行结果可以看的出来,Best count为23 而Actual Count为25这表明orders表有分块需要重构表索引。下面通过DBCC DBREINDEX来重构表的簇索引

 

3. DBCC DBREINDEX 用法

重建指定数据库中表的一个或多个索引

 

语法

DBCC DBREINDEX

    (    [ 'database.owner.table_name'   

            [ , index_name

                [ , fillfactor ]

            ]

        ]

    )    

 

参数

'database.owner.table_name'

是要重建其指定的索引的表名。数据库、所有者和表名必须符合标识符的规则。有关更多信息,请参见使用标识符。如果提供 database 或 owner 部分,则必须使用单引号 (') 将整个 database.owner.table_name 括起来。如果只指定 table_name,则不需要单引号。

 

index_name

是要重建索引名。索引名必须符合标识符的规则。如果未指定 index_name 或指定为 ' ',就要对表的所有索引进行重建

 

fillfactor

是创建索引时每个索引页上要用于存储数据的空间百分比。fillfactor 替换起始填充因子以作为索引或任何其它重建的非聚集索引(因为已重建聚集索引)的新默认值。如果 fillfactor 为 0,DBCC DBREINDEX 在创建索引时将使用指定的起始 fillfactor。

 

同样在Query Analyzer中输入命令:

dbcc dbreindex('database_name.dbo.Employee','',90)

 

然后再用DBCC SHOWCONTIG查看重构索引后的结果:

DBCC SHOWCONTIG scanning 'Employee' table...

Table: 'Employee' (1195151303); index ID: 1, database ID: 53

TABLE level scan performed.

- Pages Scanned................................: 178

- Extents Scanned..............................: 23

- Extent Switches..............................: 22

- Avg. Pages per Extent........................: 7.7

- Scan Density [Best Count:Actual Count].......: 100.00% [23:23]

- Logical Scan Fragmentation ..................: 0.00%

- Extent Scan Fragmentation ...................: 0.00%

- Avg. Bytes Free per Page.....................: 509.5

- Avg. Page Density (full).....................: 93.70%

DBCC execution completed. If DBCC printed error messages, contact your system administrator.

通过结果我们可以看到Scan Denity为100%。

原文链接:《如何提高SQL SERVER的性能

http://www.csdn.com.cn/database/1142.htm

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Windows 10 vs. Windows 11 performance comparison: Which one is better? Windows 10 vs. Windows 11 performance comparison: Which one is better? Mar 28, 2024 am 09:00 AM

Windows 10 vs. Windows 11 performance comparison: Which one is better? With the continuous development and advancement of technology, operating systems are constantly updated and upgraded. As one of the world's largest operating system developers, Microsoft's Windows series of operating systems have always attracted much attention from users. In 2021, Microsoft released the Windows 11 operating system, which triggered widespread discussion and attention. So, what is the difference in performance between Windows 10 and Windows 11? Which

The local running performance of the Embedding service exceeds that of OpenAI Text-Embedding-Ada-002, which is so convenient! The local running performance of the Embedding service exceeds that of OpenAI Text-Embedding-Ada-002, which is so convenient! Apr 15, 2024 am 09:01 AM

Ollama is a super practical tool that allows you to easily run open source models such as Llama2, Mistral, and Gemma locally. In this article, I will introduce how to use Ollama to vectorize text. If you have not installed Ollama locally, you can read this article. In this article we will use the nomic-embed-text[2] model. It is a text encoder that outperforms OpenAI text-embedding-ada-002 and text-embedding-3-small on short context and long context tasks. Start the nomic-embed-text service when you have successfully installed o

Performance comparison of different Java frameworks Performance comparison of different Java frameworks Jun 05, 2024 pm 07:14 PM

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

What impact do C++ functions have on program performance? What impact do C++ functions have on program performance? Apr 12, 2024 am 09:39 AM

The impact of functions on C++ program performance includes function call overhead, local variable and object allocation overhead: Function call overhead: including stack frame allocation, parameter transfer and control transfer, which has a significant impact on small functions. Local variable and object allocation overhead: A large number of local variable or object creation and destruction can cause stack overflow and performance degradation.

How to optimize the performance of multi-threaded programs in C++? How to optimize the performance of multi-threaded programs in C++? Jun 05, 2024 pm 02:04 PM

Effective techniques for optimizing C++ multi-threaded performance include limiting the number of threads to avoid resource contention. Use lightweight mutex locks to reduce contention. Optimize the scope of the lock and minimize the waiting time. Use lock-free data structures to improve concurrency. Avoid busy waiting and notify threads of resource availability through events.

What are the performance considerations for C++ static functions? What are the performance considerations for C++ static functions? Apr 16, 2024 am 10:51 AM

Static function performance considerations are as follows: Code size: Static functions are usually smaller because they do not contain member variables. Memory occupation: does not belong to any specific object and does not occupy object memory. Calling overhead: lower, no need to call through object pointer or reference. Multi-thread-safe: Generally thread-safe because there is no dependence on class instances.

See all articles