Summary SQL Index

Source: Internet
Author: User
Index

Indexes in databases are similar to those in books. In a book, you can use indexes to quickly find the required information without reading the entire book. In the database, the index allows the database program to find the required data without scanning the entire table. The index in the book is a word list, which indicates the page number of each word. The index in the database is a list of values contained in a table, indicating the storage location of the rows containing values in the table. You can create an index for a single column in a table or an index for a group of columns. The index uses the B tree structure. An index contains an entry that contains one or more columns (search keywords) from each row in the table ). Tree B is ordered by the Search Keyword and can be searched efficiently on any subentry set of the search keyword. For exampleA,B,CThe column index can beAAndA,BAndA,B,CFor efficient search.

Most books contain a total index of words, names, locations, and so on. A database contains an index for the selected type or data column. This is like creating an index for the name and place name in a book. When creating a database and optimizing its performance, you should create an index for the columns used for data query.

When Microsoft SQL Server 2000PubsIn the example database,EmployeeTable inEmp_idThe column has an index. The following shows how the index stores eachEmp_idAnd point to the data rows that contain values in the table.

When SQL Server executes a statementEmployeeBased on the specifiedEmp_idWhen the value is used to search for data, it can recognizeEmp_idAnd use this index to find the required data. If the index does not exist, it searches for the specifiedEmp_idValue.

SQL Server automatically creates indexes for certain types of constraints (such as primary key and unique constraints. You can create an index that does not depend on the constraints to further customize the table definition.

However, there is a price for the benefits of indexes. Indexed tables occupy more space in the database. In addition, to maintain indexes, it takes longer to insert, update, or delete data commands. When designing and creating indexes, ensure that the performance is improved at a higher cost in terms of storage space and processing resources.

Design indexes

When Microsoft SQL Server 2000 performs a query, the query optimizer evaluates the cost of available data retrieval methods and selects the most effective method. SQL Server can scan tables. If an index exists, an index is used. When SQL Server performs a table scan, it searches for rows from the first row of the table and extracts rows that meet the query conditions. When SQL server uses an index, It queries the storage location of the required rows and extracts only the required rows.

When you consider whether to create an index for a column, you should consider whether the column to be indexed is used and how it is used in the query. Indexes are helpful for the following queries:

  • Search for rows that match the specified search keyword value (exact match query ). Exact match comparison refers to the use of the where statement to specify a column entry with a given value. For example:

    WHERE emp_id = 'VPA30890F'
  • Search for rows whose search keyword value is range value (range query ). A range query is a query of any entries whose values are between two values. For example:
    WHERE job_lvl BETWEEN 9 and 12

 

 

Or

WHERE job_lvl >= 9 and job_lvl <= 12
  • In the tableT1Search by join predicates and tablesT2A row in (nested index loop join ).
  • When no explicit sorting operation is performed, the sorted query output is generated, especially the sorted dynamic cursor.
  • Without explicit sorting, the route is scanned in an ordered order to allow sequential operations, such as merge joins and stream aggregation.
  • The performance is superior to the performance of table scanning. The performance improvement is due to the reduction of the total number of columns and data to be scanned (this query has indexes covered for use ).
  • Search for duplicate new search keyword values in insert and update operations to implement the primary key and unique constraints.
  • Search for rows that match the two tables with the foreign key constraint defined.

    When you use like to compare a query, if the mode starts with a specific string such as "ABC %", using an index will improve efficiency. If the mode starts with a wildcard such as "% XYZ, then the index does not work.

    In many queries, indexes can bring many benefits. For example, in addition to covering queries, indexes also enable range queries. SQL Server can use multiple indexes for a table in the same query, and can combine multiple indexes (using the join algorithm) so that the search keyword can overwrite a query. In addition, SQL Server automatically determines which indexes are used for query, and ensures that all indexes of the table are maintained when the table is changed.

    Other index Design Criteria

    Other considerations for designing indexes include:

    • 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. On the other hand, for queries that do not need to modify data (select Statement), a large number of indexes will help improve performance, because SQL Server has more indexes to choose from, in order to determine the best way to access data as quickly as possible.
    • Overwrite queries can improve performance. Overwrite query means that all specified columns in the query are included in the same index. For exampleA,BAndCColumn created Composite Index Is retrieved from the table.AAndBColumn queries are considered as overwrite queries. Creating an index that overwrites a query can improve the performance, because all the data in the query is included in the index itself. When retrieving data, you only need to reference the index page of the table and do not need to reference the data page, therefore, the total I/O volume is reduced. Although adding columns to the index to overwrite the query can improve the performance, maintaining more columns in the index will incur the update and storage costs.
    • Indexing small tables may not produce optimization results, because it takes longer time for SQL Server to traverse the index to search for data than a simple table scan.
    • You should use the SQL event probe and index Optimization Wizard to help analyze and query and determine the index to be created. It is very complicated to select the correct index for the database and its workload. A balance must be achieved between the query speed and the update cost. Narrow indexes (the index of only a few columns in the search keyword) require less disk space and maintenance costs. On the other hand, wide indexes can cover more queries. There are no simple rules for determining the correct index set. Experienced database administrators are often able to design good index sets. However, this task is complex, time-consuming, and error-prone even for less complex databases and workloads. You can use the index optimization Wizard to automate this task. For more information, see index Optimization Wizard.

     

     

    Index features

    After determining that an index is suitable for a query, you can customize the index type that best suits the specific situation. Index features include:

    • Clustering or non-Clustering
    • Unique or not
    • Single or multiple columns
    • The column order in the index is ascending or descending.
    • Overwrite or Not Overwrite

     

     

    You can also customize the initial storage features of the index, optimize maintenance by setting the fill factor, and customize the location of the index using files and file groups to optimize performance.

    Use clustered Index

    The clustered index determines the physical sequence of data in the table. The clustered index is similar to the phone book, which sorts data by last name. Because clustered indexes specify the physical storage sequence of data in a table, a table can only contain one clustered index. However, this index can contain multiple columns (composite indexes), just as the phone book is organized by the last name and name.

    Clustered indexes are particularly effective for columns that frequently search for range values. When a clustered index is used to locate the row that contains the first value, you can ensure that the row that contains the subsequent index value is physically adjacent. For example, if an application executes a query that often retrieves records within a certain date range, the clustered index can be used to quickly locate the rows containing the start date, and then retrieve all adjacent rows in the table, until the end date is reached. This helps improve the performance of such queries. Similarly, if a column is frequently used to sort the data retrieved from a table, the table can be clustered (physically sorted) on the column ), this avoids sorting this column each time it is queried, thus saving costs.

    When the index value is unique, it is efficient to use clustered indexes to find specific rows. For example, use the unique employee ID columnEmp_idThe quickest way to find a specific employee isEmp_idCreate a clustered index or primary key constraint on the column.

    DescriptionIf no clustered index is created for the table and no non-clustered index is specified when the primary key constraint is created, the primary key constraint automatically creates a clustered index.

     

    You can alsoLname(Last Name) column andFnameCreate a clustered index on the (name) column, because employee records are often grouped and queried by name rather than by employee ID.

     

  • 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.