Database indexing and Optimization

Source: Internet
Author: User

An index is a structure that sorts the values of one or more columns in a database table. You can use an index to quickly access specific information in a database table.

What is database index?

Database indexes are like directories in front of a book, which can speed up database queries.
For example, select * From Table1 where id = 44. If no index exists, you must traverse the entire table until the row with ID equal to 44 is found. If an index exists, it must be an index created on the column with ID ), find 44 in the index (that is, find the ID column) to find the location of this row, that is, find this row. It can be seen that the index is used for locating.
Indexes are divided into clustered indexes and non-clustered indexes. Clustered indexes are stored in the order of physical locations, rather than clustered indexes; clustering indexes can improve the speed of multi-row search, rather than clustering indexes can quickly retrieve individual rows.

Overview

The purpose of creating an index is to speed up the search or sorting of records in the table.
The cost for setting indexes for a table is: first, the storage space of the database is increased, second, it takes a lot of time to insert and modify data (because the index also needs to change ).

B-tree index-SQL Server Index Method

 

Why create an index?

Creating indexes can greatly improve the system performance.
First, you can create a unique index to ensure the uniqueness of each row of data in the database table.
Second, it can greatly speed up data retrieval, which is also the main reason for creating an index.
Third, it can accelerate the connection between tables, especially in achieving Data Reference integrity.
Fourth, when you use grouping and sorting clauses to retrieve data, you can also significantly reduce the time for grouping and sorting in queries.
Fifth, by using indexes, you can use the optimizer during the query process to improve system performance.
Some may ask: why not create an index for each column in the table because increasing Indexes has so many advantages? This is because adding Indexes has many disadvantages.
First, it takes time to create and maintain indexes. This time increases with the increase of data volume.
Second, indexes occupy physical space. In addition to data tables, each index occupies a certain amount of physical space. To create a clustered index, the required space is larger.
Third, when adding, deleting, and modifying data in the table, the index must also be dynamically maintained, which reduces the Data Maintenance speed.

Where to create an index

Indexes are created on certain columns in the database table. When creating an index, you should consider which columns can create an index and which Columns cannot create an index. In general, you should create an index on these columns:
You can speed up the search on columns that frequently need to be searched;
In a column that acts as a primary key, the uniqueness of the column and the data arrangement structure in the organization table are enforced;
These columns are usually used in connected columns. These columns are mainly foreign keys, which can speed up the connection. You can create indexes on columns that need to be searched by range frequently because the indexes have been sorted, the specified range is continuous;
Create an index on the columns that frequently need to be sorted. Because the index has been sorted, you can use the index sorting to speed up the sorting query time;
Create an index on the columns in the WHERE clause frequently to speed up condition judgment.
Similarly, indexes should not be created for some columns. In general, these columns that should not be indexed have the following features:
First, indexes should not be created for columns that are rarely used or referenced in queries. This is because, since these columns are rarely used, there is an index or no index, and the query speed cannot be improved. On the contrary, the addition of indexes reduces the system maintenance speed and space requirements.
Second, indexes should not be added to columns with only few data values. This is because these columns have very few values, such as gender columns in the personnel table. In the query results, the data rows in the result set account for a large proportion of the data rows in the table, that is, the proportion of data rows to be searched in the table is large. Adding indexes does not significantly accelerate the search speed.
Third, indexes should not be added for columns defined as text, image, and BIT data types. This is because the data volume of these columns is either large or small, which is not conducive to the use of indexes.
Fourth, when the modification performance is far greater than the retrieval performance, you should not create an index. This is because the modification performance and retrieval performance are inconsistent. When an index is added, the search performance is improved, but the modification performance is reduced. When the index is reduced, the modification performance is improved and the retrieval performance is reduced. Therefore, when modification operations are far more than search operations, you should not create an index.

Database Optimization

In addition to database indexes, performance optimization of databases (especially MySQL) is also a hot topic in Mass Data Processing today when lamp results are so popular. Next we will talk about several aspects of MySQL database Optimization Based on our own experience.
First, when designing a database, we should be able to fully utilize the performance improvement brought about by indexes. As for how to create an index, what kind of index to create, and on which fields to create an index, I have already explained it clearly. I will not repeat it here. In addition, the principle of designing a database is to perform as few database write operations (insert, update, delete, etc.) as possible. The simpler the query, the better. As follows:

Database Design

Second, configuring the cache is essential. configuring the cache can effectively reduce the number of database queries and reads, so as to relieve the pressure on the database server and achieve optimization. To a certain extent, this is a "Wei Jie Zhao" method. Configurable caches include index cache (key_buffer), sort cache (sort_buffer), query cache (query_buffer), and table descriptor cache (table_cache), for example:

Configure Cache

Third, table sharding is also a popular database optimization method. Two table sharding methods are available: horizontal table sharding and vertical table sharding. Vertical table sharding is helpful, but table sharding causes query burden. Therefore, when designing a database, consider the following methods:

Table sharding

Fourth, log analysis will accumulate a large number of log logs after the database has been running for a long period of time. In fact, the useful information contained here is still very large. By analyzing logs, you can find the bottleneck of system performance and further find the optimization solution.

Performance Analysis

All of the above are some experience in optimizing the performance of single-host mysql. However, with the explosive growth of information, the single-host database server can no longer meet our needs. As a result, many nodes and distributed database networks have emerged, the general structure is as follows:

Distributed Database Structure

The key to this distributed cluster technology is "synchronous replication "... Http://blog.redfox66.com/post/2011/08/16/mass-data-topic-7-index-and-optimize.aspx

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.