T Sql 简明教程
T-SQL - Indexes
Indexes 是数据库搜索引擎用于加快数据检索的特殊查找表。简而言之, index 是指向表中数据的指针。数据库中的索引与书末尾的索引非常相似。
Indexes are special lookup tables that the database search engine can use to speed up data retrieval. Simply put, an index is a pointer to data in a table. An index in a database is very similar to an index at the end of a book.
例如,如果你想要引用一本书中讨论某一特定主题的所有页面,你首先会参考索引,它按字母顺序列出所有主题,然后被引用到一个或多个特定的页码。
For example, if you want to reference all the pages in a book that discuss a certain topic, you first refer to the index, which lists all topics alphabetically and are then referred to one or more specific page numbers.
索引有助于加快 SELECT 查询和 WHERE 子句的速度,但会减慢数据输入速度,以及 UPDATE 和 INSERT 语句。可以在不影响数据的情况下创建或删除索引。
An index helps speed up SELECT queries and WHERE clauses, but it slows down data input, with UPDATE and INSERT statements. Indexes can be created or dropped with no effect on the data.
创建索引涉及使用 CREATE INDEX 语句,该语句允许你命名索引、指定表和要索引的列(一个或多个)以及指示索引是按升序还是降序。
Creating an index involves the CREATE INDEX statement, which allows you to name the index, to specify the table and which column or columns to index, and to indicate whether the index is in ascending or descending order.
索引也可以是唯一的,类似于 UNIQUE 约束,其中索引禁止在列或有索引列组合中重复条目。
Indexes can also be unique, similar to the UNIQUE constraint, in that the index prevents duplicate entries in the column or combination of columns on which there’s an index.
CREATE INDEX Command
以下是 CREATE INDEX 的基本语法。
Following is the basic syntax of CREATE INDEX.
Single-Column Indexes
单列索引是基于仅一个表列创建的。以下是其基本语法。
A single-column index is one that is created based on only one table column. Following is the basic syntax.
Unique Indexes
唯一索引不仅用于性能,还用于数据完整性。唯一索引不允许将任何重复值插入表中。以下是其基本语法。
Unique indexes are used not only for performance, but also for data integrity. A unique index does not allow any duplicate values to be inserted into the table. Following is the basic syntax.
Composite Indexes
复合索引是对表中的两个或多个列进行的索引。以下是其基本语法。
A composite index is an index on two or more columns of a table. Following is the basic syntax.
Example
CREATE INDEX compositeindex
on customers (NAME, ID)
无论创建单列索引还是复合索引,都要考虑在查询的 WHERE 子句中可能非常频繁地用作筛选条件的列。
Whether to create a single-column index or a composite index, take into consideration the column(s) that you may use very frequently in a query’s WHERE clause as filter conditions.
如果只使用了一列,则应该选择单列索引。如果在 WHERE 子句中经常将两列或更多列用作筛选条件,那么复合索引将是最佳选择。
Should there be only one column used, a single-column index should be the choice. Should there be two or more columns that are frequently used in the WHERE clause as filters, the composite index would be the best choice.
DROP INDEX Command
可以使用 MS SQL SERVER DROP 命令删除索引。删除索引时应小心,因为这可能会降低或提升性能。
An index can be dropped using MS SQL SERVER DROP command. Care should be taken when dropping an index because performance may be slowed or improved.
When to Avoid Indexes?
尽管索引旨在提高数据库性能,但在某些情况下应避免使用它们。以下指导原则指出了在什么时候应重新考虑使用索引−
Although indexes are intended to enhance the performance of databases, there are times when they should be avoided. The following guidelines indicate when the use of an index should be reconsidered −
-
Indexes should not be used on small tables.
-
Tables that have frequent, large batch update or insert operations should not be indexed.
-
Indexes should not be used on columns that contain a high number of NULL values.
-
Columns that are frequently manipulated should not be indexed.