Mainstream database indexes and their examples

Article directory

 

content

Article directory

foreword

index

Overview

concept

Advantages and disadvantages of using indexes in the database:

Index classification

normal index

unique index

primary key index

Full-text index

Spatial index

Other categories

Basic Principles of Index Setup

create index

Indexing with the CREATE INDEX statement

Create an index when creating a table

Create an index when modifying a table

Create Index Considerations

Index performance analysis

Check if the profile is enabled

set open profile

View the time-consuming details of SQL execution

drop index

Use the drop index statement to drop an index

Use the alter table statement to delete an index


foreword

Hello everyone, I am the color of ice three points.

Personal homepage: blog of ice three colors

This article talks about the knowledge of indexes, the concept of indexes, the classification of indexes, the basic principles of setting, three methods and precautions for creating indexes, performance analysis of indexes, and knowledge points such as deleting indexes. Indexing is difficult, very important.

Friends passing by, please like and follow before walking. Welcome to the comment area to communicate. It is never too late to start working hard, so it is better to start with this article!

Let's grow together! refill


index

Overview

concept

1. The index is a special data structure composed of one or more columns in the database table. The index can be used to quickly query the specific record information in the database table (the index is similar to the directory in the dictionary, which can be sorted by pinyin, strokes, etc.) , radicals, etc. to quickly find the required word. Multi-column index is similar to first according to one condition, this condition is the same, then continue to judge the second condition, that is, the second column index and so on)

2. Indexes affect data performance, and efficient indexes can improve query speed and performance. MySQL's default query is to perform a full table scan based on the search conditions (from the first to the last is called a full table scan), and the records that meet the matching conditions are added to the result set. If it involves multi-table joins, complex query conditions, and a large amount of data, the amount of data executed without index scan will be very large and very slow.

3. An index contains a key generated from one or more columns in a table or view, and a pointer to a storage location that maps to a specified row of data. (The form of the index in the database is similar to the table of contents of a book, the key value is like the title in the table of contents, and the pointer is equivalent to the page number. The index can quickly find the content (table data) like a table of contents, without having to scan the entire data table to find it.)

4. The index is built on the table and provides an internal method for arranging the data in the table in the database. The storage of the table consists of two parts, one is the data page of the table, and the other is the index page. The index is stored on the index page.

5. Once the index is created, it will be automatically managed and maintained by the database. (For example, when inserting, updating, and deleting a record into a table, the database automatically makes corresponding changes in the index. When executing a query, the query optimizer estimates the cost of the various data retrieval methods available, and chooses from them The most efficient query plan.)

6. The index is not the more the better. It is necessary to correctly understand the importance and design principles of the index, and create a suitable index. (When the amount of data is small, there is no need to add an index)

Advantages and disadvantages of using indexes in the database :

Advantages: 1. Accelerate data retrieval: The index can quickly find data rows based on the value of one column or multiple columns. 2. Optimizing queries: The query optimizer relies on indexes to function, and indexes can speed up operations such as joins, sorting, and grouping. 3. Enforce uniqueness of rows: By creating unique indexes for columns, you can ensure that the data in the table is not duplicated.

Disadvantages: 1. Although the index improves the query speed, it will reduce the speed of updating the table, because when updating the table, MySQL not only saves the data, but also saves the index. 2. Create an index file that will occupy disk space.

Index classification

MySQL indexes can be divided into the following five categories: common indexes, unique indexes, primary key indexes, full-text indexes, and spatial indexes. (Generally speaking, the first three are enough, the commonly used are primary key index and unique index)

normal index

Ordinary index (index) is the basic index type in MySQL, allowing the insertion of duplicate values ​​and null values ​​in the column that defines the index. The key to the index is index.

unique index

The value of a unique index (unique) column must be unique, and null values ​​are allowed. In the case of a composite index, the combination of column values ​​must be unique. Multiple unique indexes can be created on a table.

primary key index

The primary key index (primary key) is a special unique index that does not allow null values. Generally, the primary key index is created while the table is being built. You can also increase the primary key by modifying the table, but a table can only have one primary key index. (generally consistent with the actual physical order)

Full-text index

Fulltext indexing refers to supporting full-text lookups of values ​​on the columns that define the index, allowing the insertion of duplicate and null values ​​in the indexed columns. The index only indexes columns of type char, varchar, and text, and can only be used in

The MylSAM storage engine table is compiled. By default in MySQL, it has little effect on Chinese.

Spatial index

A spatial index (spatial) is an index on a field of a spatial data type. There are 4 spatial data types in MySQL: geometry, point, linestring and polygon. Spatial indexes are only created on tables in the storage engine MyISAM. For starters, such indexes are rarely used. (Our storage engine is innoDB)

Other categories

If classified according to the number of columns to create the index key value, the index can also be divided into single-column index and composite index.

If classified according to the storage method, it can be divided into binary tree (B-Tree) index and Hash index.

Basic Principles of Index Setup

When creating an index in a data table, in order to make the use of the index more efficient, you must consider which fields to create the index on and what type of index to create. The principles are as follows:

1. A table creates a large number of indexes, which will affect the performance of insert, update and delete statements. Avoid creating too many indexes on frequently updated tables, and limit the number of indexes.

2. If the amount of data in the table is large, there are fewer updates to the table data and more queries, and multiple indexes can be created to improve performance.

3. Fields that often require sorting, grouping, and union operations must be indexed, that is, indexes are created on fields that are used for join, where judgment, and order by sorting.

4. Creating indexes on views can significantly improve query performance.

5. Try not to index fields that contain a large number of duplicate values ​​in the database. Establishing indexes on such fields may reduce the performance of the database.

6. Create an index on the primary key. In InnoDB, it is very efficient to access data through the primary key. Only one primary key index can be created per table.

7. To limit the number of indexes. For indexes that are no longer used or rarely used, delete them in time.

create index

There are usually three command ways to create an index: use the CREATE INDEX statement to create an index, create an index when creating a table, and create an index by modifying the table.

Indexing with the CREATE INDEX statement

Format: CREATE [unique|fulltext|spatial] INDEX index name ON table name (which column of the table name has column name 1, column name 2...)

Example: Create an ascending common index ename_index on the ename column of the employee table.

CREATE INDEX ename_index ON employee(ename ASC);

Example: Create a unique index ename_uni_index on the ename column of the employee table.

CREATE UNIQUE INDEX ename_uni_index ON employee(ename);

Example: Create a composite index empno_ename_index on the empno and ename columns of the employee table.

CREATE INDEX empno_ename_index ON employee(empno,ename);

You can use the show index from table_name statement to view the indexes that have been created in the table

SHOW INDEX FROM employee; as follows

We can also directly view the index at the design table by right-clicking the employee table in navicat, as follows

Create an index when creating a table

Example: When creating the dept_index table, a unique index dname_index is created for the dname field, and a prefix index loc_index is created for the first 3 characters of the loc field.

CREATE TABLE IF NOT EXISTS dept_index (

deptno INT(2),

dname VARCHAR(14),

loc VARCHAR(13),

PRIMARY KEY (deptno),

UNIQUE INDEX dname_index(dname),

INDEX loc_index(loc(3))

);

Create an index when modifying a table

Example: Create a composite index of deptno and dname on the dept_index table.

ALTER TABLE dept_index ADD INDEX deptno_dname_index(deptno, dname);

View the indexes created by the dept_index table

Show index from dept_index;

Create Index Considerations

1. Only the owner of the table has permission to create indexes on the table.

2. The name of the index must conform to the MySQL naming rules and must be unique in the table. (The primary key index must be unique, and the unique index is not necessarily the primary key. There can only be one primary key on a table, but there can be one or more unique indexes.)

3. When a unique constraint is created for a table, MySQL will automatically create a unique index. When creating a unique index, you should ensure that the indexed column does not include duplicate data, and do not have two or more null values ​​(null). Because the two null values ​​are also regarded as duplicate data when creating an index, if there is such data, it must be deleted first, otherwise the index cannot be successfully created. That is, a unique index only allows one null value.

Index performance analysis

After MySQL 5.0, the query diagnostic analysis tool "Show Profiles" comes with it, which can locate various resource consumption (such as CPU, IO, etc.) of a SQL statement execution, as well as the time consumed by the SQL execution.

To use it, you first need to open the profile

The default database is not enabled, and the variable profiling is a user variable, which must be re-enabled every time

Check if the profile is enabled

show variables like "%pro%";

set open profile

(Because I check and know that mine is turned on, so I don't need to do this step again)

set profiling = 1;

View the time-consuming details of SQL execution

show profiles; -- View the total execution time of all SQL.

Example: Execution time performance analysis for employee names before and after being indexed (the process is as follows)

-- Create table emp_index from employee table

CREATE TABLE emp_index AS SELECT * FROM employee;

-- open profile

set profiling = 1;

-- Query the name of the employee before the index is added

SELECT ename FROM emp_index;

-- View the execution time of SQL

show profiles;

-- add index to ename

CREATE INDEX ename_index ON emp_index(ename);

-- Query the employee name after indexing

SELECT ename FROM emp_index;

-- View the execution time of the latest SQL

show profiles;

The result is as follows:

drop index

Deleting an index is an unused index, and needs to be updated and maintained in a timely manner, because the index has an upper limit. It can be deleted by DROP statement and ALTER TABLE statement.

Use the drop index statement to drop an index

Format: DROP INDEX index name ON table name ;

example:

DROP INDEX ename_index ON employee;

Use the alter table statement to delete an index

You can use CREATE INDEX ename_index ON employee(ename ASC); recreate the index ename_index, and then delete it as a test with alter table

Format:

ALTER TABLE table name [DROP PRIMARY KEY| DROP INDEX index name| DROP FOREIGN KEY fk_symbol

example:

ALTER TABLE employee DROP INDEX ename_index;

Guess you like

Origin blog.csdn.net/qq_46007633/article/details/124087799