Note In Microsoft's SQL server system, you can use indexes to improve the query performance of the database. However, the performance improvement depends on the implementation of the database. This article will show you how to index and effectively improve the database performance.
Using indexes in relational databases can improve database performance, which is obvious. The more indexes you use, the faster you get data from the database system. However, the more indexes you use, the more time it takes to insert new data into the database system. In this article, you will learn about the various types of indexes supported by Microsoft's SQL Server database. Here you will learn how to implement indexes using different methods, through these different implementation methods, you get far more losses in terms of the read performance of the database than in terms of the overall performance of the database.
Index Definition Index is a database tool. By using indexes, you do not need to scan all data records in the database to improve the system's data acquisition performance. Using indexes can change the way data is organized so that all data is organized according to a similar structure, so that data retrieval and access can be easily realized. The index is created by column, so that the database can find the corresponding data based on the value in the index column.
Index type Microsoft SQL Server supports two types of indexes: clustered index and nonclustered index. Clustered indexes store data in physical order in data tables. Because there is only one physical order in the table, each table can have only one clustered index. Clustered index is a very effective index when you look for data within a certain range, because the data is stored in a physical order.
Nonclustered indexes do not affect the following physical storage, but they are composed of data row pointers. If a clustered index already exists, the index pointer in nonclustered will contain the clustered index location reference. These indexes are faster than data, and the scanning speed of these indexes is much faster than that of the actual data table.
How to Implement Index The database can automatically create some indexes. For example, Microsoft's SQL Server System enforces unique constraints by automatically creating unique indexes, so that duplicate data is not inserted in the database. You can also use the create index statement or SQL Server Enterprise Manager to create other indexes. SQL Server Enterprise Manager also has an index creation template to guide you in creating indexes.
Better performance Although the index can bring performance advantages, it will also bring a certain price. Although the SQL server system allows you to create up to 256 nonclustered indexes in each data table, we recommend that you do not use so many indexes. Because indexes need to use more storage space on both internal and physical disk drives. During the execution of the insert statement, the system performance may be degraded to some extent, because data needs to be inserted in the order of indexes, instead of directly inserting data at the first available location, the more indexes there are, the more time it takes to insert or update the declaration.
When using the SQL server system to create an index, we recommend that you follow the creation rules below:
Correct Data Type Selection Using certain data types in indexes can improve the efficiency of the database system, such as int, bigint, smallint, and tinyint, because they all occupy the same size of space and can be easily compared. The efficiency of other data types such as char and varchar is very low, because these data types are not suitable for performing mathematical operations, and the time for performing comparative operations is longer than the data type mentioned above.
Make sure that the index value is used correctly during use. When performing the query operation, the columns may only be part of clustered. Pay special attention to how to use the data. When using these data columns as parameters to call functions, these functions may invalidate the existing sorting advantages. For example, if you use a date value as an index, you may need to convert the date value to a string to achieve a comparison operation. In this way, the date index value cannot be used during the Query Process.
When creating multi-column indexes, pay attention to the column sequence. The database will sort records based on the index values of the first column, and then sort the records based on the values of the second column until the last index is sorted. If the unique data value of a column is small, the column should be the first index. This ensures that the data can be further sorted by index.
Restrict the number of columns in the clustered Index The more columns are used in the clustered index, the more reference positions the nontered index contains in the nonclustered index, and the more data to store. This will increase the size of the data table containing the index and the search time based on the index.
Avoid frequent updates to clustered index data Columns Since the nonclustered index depends on the clustered index, if the data columns that constitute the clustered index are frequently updated, the row locators stored in nonclustered will also be updated frequently. For all queries related to these columns, if a record is locked, this may cause an increase in performance costs.
Separate operations (if possible) For a table, if you need to perform frequent insert and update operations and a large number of read operations, try to separate the table if possible. All insert and update operations can be performed in a non-indexed table, and then copied to another table. In this table, a large number of indexes can be used to optimize the ability to read data.
Appropriate index Reconstruction The nonclustered index contains the pointer of the clustered index, so that the nonclustered index will belong to the clustered index. When the clustered index is rebuilt, the original index is discarded first, and then the create index is used to create the index. Alternatively, the drop_existing clause is used as part of the index reconstruction when the create index statement is used. Dropping and creating a nonclustered index may result in multiple reconstruction times, instead of rebuilding a nonclustered index once, as with the drop_existing clause.
Use fill factor wisely data is stored on pages with a fixed size of continuous memory. With the addition of new record rows, the data memory page will gradually be filled up, and the system must split the data page, through which part of the data will be transferred to the next new page. Such splitting will increase the burden on the system and lead to fragmented storage data. Fill factor can maintain the gap between data. Generally, when creating an index, the fill factor of the index has been set. This reduces the number of page splits caused by data insertion. Because the size of the space is maintained only when the index is created, the size of the space is not maintained when data is increased or updated. Therefore, to make full use of the fill factor, you must rebuild the index periodically. The gap caused by the fill factor will lead to a reduction in read performance, because as the database expands, more and more disk access tasks need to read data. Therefore, when the number of reads exceeds the number of writes, it is important to consider whether to use the fill factor or the default method. Management Decision by effectively using indexes, You can implement good query functions in Microsoft SQL Server, however, the efficiency of using indexes depends on several different implementation decisions. In terms of the Performance balance of indexes, it is necessary to make correct database management decisions in terms of good performance and dilemmas. Under specific circumstances, some suggestions provided in this article will help you make the right decisions.
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