Improve SQL Server speed to sort index fragments

Source: Internet
Author: User

Database Table A has 100,000 records, and the query speed is normal. However, after importing one thousand data records, the problem occurs. When the selected data is between the original 100,000 records, the speed is still quite fast; but when the selected data is between the one thousand data records, the speed becomes very slow.

Based on experience, this is an index fragmentation problem. Check the index fragmentation DBCC showcontig (table) and obtain the following results:

DBCC showcontig is scanning the 'A' table...
Table: 'A' (884198200); index ID: 1, Database ID: 13
A table-level scan has been performed.
-Number of scanned pages ..................................... 3127
-Scan the number of extended disk areas ......: 403
-Extended disk partition switch count ......................: 1615
-Average number of pages on each extended disk... 7.8
-Scan density [optimal value: actual value] ......: 24.20% [391: 1616]
-Logically scan fragments...
-Extended disk scan fragments...: 38.46%
-Average number of available bytes on each page ......: 2073.2
-Average page density (complete) ................: 74.39%
DBCC execution is complete. If DBCC outputs an error message, contact the system administrator.

From the above we can see that the scanning fragmentation of the logical scan fragmentation and the extended disk area is very large, and it is really necessary to process the index fragmentation.

There are two solutions: one is to use DBCC indexdefrag to sort out index fragmentation, and the other is to use DBCC dbreindex to reconstruct the index. Both have their own advantages and disadvantages. The original statement for calling Microsoft is as follows:
The DBCC indexdefrag command is an online operation, so the index is only available when the command is running. In addition, the operation can be interrupted without losing the completed work. The disadvantage of this method is that there is no clustered index removal/re-creation operation in reorganizing data.

Re-create a clustered index will re-organize the data. The result is that the data page is filled up. You can use the fillfactor option to configure the fill level. The disadvantage of this method is that the index is offline during the deletion/re-creation period and the operation belongs to the atomic level. If the index creation is interrupted, the index will not be re-created.

That is to say, to achieve good results, you still need to re-build the index, so you have to re-build the index.
DBCC dbreindex (table, index name, fill factor)
The first parameter can be the table name or table ID.
The second parameter, if it is '', affects all indexes of the table.
The third parameter is the fill factor, that is, the data fill degree on the index page. If it is 100, it means that every index page is filled up. In this case, select is the most efficient, but when you want to insert an index later, you have to move all the subsequent pages, which is very inefficient. If the value is 0, the previous fill factor value is used.

DBCC dbreindex (A, '', 100)
Retest the query speed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.