The guidelines to be followed when designing indexes include: Database Rules, query rules, and column rules. Detailed descriptions of each criterion are listed below. Among them, 1-3 belongs to the database criterion; 2-8 belongs to the query criterion; and 9-15 belongs to the column criterion.
1.If a table has a large number of indexes, the performance of INSERT, UPDATE, and DELETE statements will be affected, because all indexes must be adjusted properly when the data in the table is changed. Avoid making too many indexes on frequently updated tables and keep the indexes narrow. That is to say, as few columns as possible.
2.Using Multiple indexes can improve the performance of queries with few updates and a large amount of data. A large number of indexes can improve the performance of queries without modifying data (such as SELECT statements), because the query optimizer has more indexes to choose from, so as to determine the fastest access method.
3.Indexing small tables may not produce optimization results, because the query optimizer takes longer time to traverse the index used for data search than to perform a simple table scan. Therefore, the index of a small table may never be used, but it must be maintained when the data in the table is changed.
4.When a view contains a combination of aggregation, table join, or aggregation and join, the view index can significantly improve the performance. To enable the query optimizer to use a view, you do not have to explicitly reference this view in the query.
5.Create a non-clustered index for all columns that are frequently used in queries for predicates and join conditions. Avoid adding unnecessary columns. Adding too many index columns may negatively affect disk space and index maintenance performance.
6.Covering indexes can improve query performance, because all data that meets the query requirements exists in the index itself. That is to say, you only need to index the page, instead of the data page or clustered index of the table to retrieve the required data. Therefore, the overall disk I/O is reduced. For example, for a query of column a and column B in a table (which creates a composite index for column a, column B, and column c, the specified data can be retrieved only from the index.
7.Write batch insert and update operations into a single statement, instead of using multiple SQL statements to operate the same row. Only one statement can be used to maintain the optimized index.
8.Evaluate the query type and How to Use columns in the query. For example, columns used in the exact match Query type are suitable for non-clustered indexes or clustered indexes.
9.For clustered indexes, keep the index key length shorter. In addition, creating a clustered index for a unique or non-empty column can benefit the clustered index.
10.You cannot specify columns of the ntext, text, image, varchar (max), nvarchar (max), and varbinary (max) data types as index key columns. However, columns of the varchar (max), nvarchar (max), varbinary (max), and xml data types can be used as non-clustered indexes.
11.Columns of the xml data type can only be used as key columns in the XML index.
12.Check column uniqueness. Unique indexes in the same column combination instead of non-unique indexes provide additional information about the query optimizer that makes the index more useful.
13.Check the data distribution in the column. In general, creating an index for a column that contains few unique values or performing a join operation on such a column will lead to long-running queries. This is a basic problem of data and query. This problem cannot be solved if it is not identified. For example, if the physical phone book is sorted alphabetically by the last name, and all people in the city have the last name Smith or Jones, a person cannot be quickly found.
14.If the index contains multiple columns, the column sequence should be considered. The WHERE clause used for equal to (=), greater than (>), less than (<), or BETWEEN search conditions, or the columns involved in the join should be placed at the beginning. Other columns should be sorted based on their non-repeated levels, that is, from the least repeated columns to the most repeated columns.
For example, if you define an index as LastName or FirstName, this index is useful when the search condition is WHERE LastName = 'Smith 'or WHERE LastName = Smith AND FirstName LIKE 'J %. However, the query optimizer does not use this index for searches based on FirstName (WHERE FirstName = 'Jane.
15.Consider indexing the computed columns.