Index 3 - index design best practices and failure index

9.7 Best Practices index design

We must first understand the environment of the system, what system, OLTP, OLAP. Read more, or write more. Run queries on the database than a small database to run a lot of queries require less index.

Consider a clustered index is provided for each table - the entire table becomes sequential

   Using data aggregation index table is clustered index key sequence is present in the stack store instead. The advantage of using the clustered index is such that the order of the data clustered index keys stored, and such that after insertion of the element remains in this order.

Each table should have a primary key, so each table should have one or more indexes, which allow an index to be clustered index. Clustered index itself does not make one more index on the table, but to better organize the structure of the table.

    When you select the clustered index key, clustered index key should be unique, short and try not need to change.

Table 2 set the primary key

    While SQL Server is not mandatory to set the primary key. But not a primary key table no matter under OLTP or OLAP environment is a dangerous thing, because there is no primary key can not guarantee that every trip is unique. Then you can not know whether the same two rows of data exist in the table, especially when you do not have enough information to analyze this point.

    Although SQL Server is not mandatory to set a primary key, but the primary key is a key relational database theory. If no primary key constraint, associated with a unique index or connection operations it may produce unexpected performance problems.

 

3 Use the fill factor to reduce page split

Consider using a fill factor to reduce page split

Data added to the table has reached the maximum page can be accommodated. So then insert the data will cause a page split. Fill factor can be used when rebuilding the index, if the database is greater than the read-write, then, 75 is provided, substantially equal if the read, set the fill factor of 90-95 fill factor.

4 table primary key, foreign key must have an index

The foreign key is set to the leftmost column of the clustered index is the data in the table are aggregated and organized in accordance with the value of this column, which is required for the query. For example, you the consumer with a credit card and this behavior is associated with the strongest card, not your credit card and shopping centers as well as banks deal with this consumption. Then a credit card number as a clustered index of consumer records in the leftmost column of the table, so that all consumer information will be the same card there is a continuous page.

Of course, you still need a few additional changes in the credit card number of columns and column combined to ensure the uniqueness of the clustered index keys.

Principle 5 indexed

A frequently appear in the Where clause field, especially in the field of large tables should be indexed;

Table B often connected to the other tables in the connection field should be indexed;

C index should be in the field of high selectivity;

D index should be built on a small field, for large text fields and even fields long, not indexed;

E build composite index needs to be carefully analyzed; try to consider instead of a single field index:

Whether the F composite index several fields often while AND to appear on the Where clause of a query whether a single field with little or no if yes, you can build composite index;?? Otherwise consider single-field index;

G frequent table data manipulation, do not create too much of an index;

H delete unused indexes, to avoid negative impact on the implementation plan;

I unnecessary index, such as gender field on fewer different value field;

J for the column to avoid indexing frequently accessed;

The column index (primary key / foreign key) K for coupling;

 

9.8 Failure Index

1 query predicate does not use the main index of the border, in other words select *, it may lead to taking the index.

2, small tables in the database or need to select most of the data, do not take the index

3 character field when numbers do not add quotation marks where conditions inside.

4,! = Or <> (not equal to), not in, not exist lead to take the index,

5, the index lists function calculation or operation, resulting in the index do not go

6, implicit conversions lead to take the index. SELECT * FROM T WHERE Y = 5, SELECT * FROM T WHERE TO_NUMBER (Y) = 5, this time it was possible to use less than the index.

7. Establish a composite index, but the query predicate is not used in the first column of the composite index, a composite index references in the index column other than the first position alone.

8, like '% liu' forward Percent

9. conditions with or, even if conditional band index, a query can not use the index to be used union

 

Failure index, the full value of my favorite matches, the best left-prefix rule, do not do anything in the index column, the column on the range of conditions index failure, reduce the use of select *, use is not equal (! = Or <>) can not use the index, use is null or is not null can not use an index, like the beginning has been the wildcard (% abc) leads to failure of the index. or less

Published 37 original articles · won praise 0 · Views 2416

Guess you like

Origin blog.csdn.net/syjhct/article/details/86696670