• notice
  • Congratulations on the launch of the Sought Tech site

Mysql index type summary and use skills and precautions

In database tables, indexing fields can greatly improve query speed.Suppose we create a mytable table:

The code is as follows:
CREATE TABLE mytable(   ID INT NOT NULL,     username VARCHAR(16) NOT NULL );

We randomly inserted 10,000 records into it, including one: 5555, admin.

When searching for the record with username="admin", SELECT * FROM mytable WHERE username='admin';, if an index has been created on the username, MySQL can find the record accurately without any scanning.On the contrary, MySQL scans all records, that is, 10,000 records are to be queried.

The index is divided into single-column index and composite index.Single-column index, that is, an index contains only a single column, a table can have multiple single-column indexes, but this is not a composite index.Composite index, that is, a single index contains multiple columns.

MySQL index types include:

One.General index

This is the most basic index, it has no restrictions.It can be created in the following ways:

1.Create index

The code is as follows:
CREATE INDEX indexName ON mytable(username(length));

If it is CHAR or VARCHAR type, length can be less than the actual length of the field; if it is BLOB and TEXT type, length must be specified, the same below.

2.Modify the table structure

The code is as follows:

ALTER mytable ADD INDEX [indexName] ON (username(length))-Specify directly when creating the table

CREATE TABLE mytable(   ID INT NOT NULL,    username VARCHAR(16) NOT NULL,   INDEX [indexName] (username(length))  );  

--The syntax for deleting an index:

DROP INDEX [indexName] ON mytable;

Second, unique index

It is similar to the previous ordinary index, except that the value of the index column must be unique, but null values ​​are allowed.If it is a composite index, the combination of column values ​​must be unique.It can be created in the following ways:

The code is as follows:
CREATE UNIQUE INDEX indexName ON mytable(username(length))
-Modify the table structure
ALTER mytable ADD UNIQUE [indexName] ON (username(length))
--Specify directly when creating the table
CREATE TABLE mytable(   ID INT NOT NULL,  ;   username VARCHAR(16) NOT NULL,   UNIQUE [indexName] (username(length))  );

3.Primary key index

It is a special unique index and no null values ​​are allowed.Generally, the primary key index is created at the same time when the table is built:

The code is as follows:
CREATE TABLE mytable(   ID INT NOT NULL,     username VARCHAR(16) NOT NULL,   PRIMARY KEY(ID)  );

Of course, you can also use the ALTER command.Remember: a table can only have one primary key.

Four.Combined Index

In order to visually compare single-column index and composite index, add multiple fields to the table:

The code is as follows:
CREATE TABLE mytable(   ID INT NOT NULL,    username VARCHAR(16) NOT NULL,   city VARCHAR(50) NOT NULL,   age INT NOT NULL ); 

In order to further squeeze the efficiency of MySQL, it is necessary to consider building a composite index.Just build name, city, age into an index:

The code is as follows:
ALTER TABLE mytable ADD INDEX name_city_age (name(10),city,age);[code]
When building a table, username length is 16, here 10 is used.This is because under normal circumstances the length of the name will not exceed 10, which will speed up the index query speed, will also reduce the size of the index file, and improve the update speed of INSERT.

If a single-column index is created on usernname, city, and age respectively, so that the table has 3 single-column indexes, the query efficiency will be very different from the above-mentioned composite index, which is far lower than our composite index.Although there are three indexes at this time, MySQL can only use the one that seems to be the most efficient single-column index.

The establishment of such a composite index is actually equivalent to the establishment of the following three sets of composite indexes:

usernname,city,age   usernname,city   usernname  why is there no composite index like city and age? This is because of the result of the "leftmost prefix" of the MySQL composite index.The simple understanding is to only start the combination from the leftmost.Not as long as queries containing these three columns will use this composite index, the following SQL will use this composite index:
[code]
SELECT * FROM mytable WHREE username="admin" AND city="Zhengzhou"  SELECT * FROM mytable WHREE username="admin"

The following ones will not be used:

The code is as follows:

SELECT * FROM mytable WHREE age=20 AND city="Zhengzhou"  SELECT * FROM mytable WHREE city="Zhengzhou"

5.Timing of indexing

Here we have learned to build indexes, so under what circumstances do we need to build indexes? Generally speaking, the columns appearing in WHERE and JOIN need to be indexed, but this is not completely the case, because MySQL only applies to <, <=,=, >, >=, BETWEEN, IN, and sometimes The LIKE will use the index.For example:

The code is as follows:
SELECT t.Name  FROM mytable t LEFT JOIN mytable m     ON t.Name=m.username WHERE m.age=20 AND m.city='Zhengzhou'

At this time, you need to index city and age.Since the userame of the mytable table also appears in the JOIN clause, it is also necessary to index it.

I just mentioned that only some LIKEs need to be indexed.Because in the query beginning with wildcard% and _, MySQL will not use the index.For example, the following sentence will use the index:

The code is as follows:

SELECT * FROM mytable WHERE username like'admin%'

The following sentence will not be used:
The code is as follows:
SELECT * FROM mytable WHEREt Name like'%admin'

Therefore, you should pay attention to the above difference when using LIKE.

Six.Shortcomings of the index

The above are all about the benefits of using indexes, but excessive use of indexes will cause abuse.Therefore, the index will also have its shortcomings:

1.Although the index greatly improves the query speed, it will also reduce the speed of updating the table, such as INSERT, UPDATE and DELETE on the table.Because when updating the table, MySQL must not only save the data, but also save the index file.

2.Index files that will occupy disk space when indexing.In general, this problem is not serious, but if you create multiple composite indexes on a large table, the index file will expand very quickly.

Index is only a factor in improving efficiency.If your MySQL has a large amount of data tables, you need to spend time researching and establishing the best index or optimizing query statements.

Seven.Precautions for using the index

When using the index, there are some tips and precautions:

1.The index will not contain columns with NULL values

As long as the column contains a NULL value, it will not be included in the index.As long as one column in a composite index contains a NULL value, then this column is invalid for this composite index.So we don't let the default value of the field be NULL when designing the database.

2.Use short index

For indexing the list, you should specify a prefix length if possible.For example, if there is a CHAR(255) column, if the multi-value is unique within the first 10 or 20 characters, then do not index the entire column.Short index can not only improve query speed but also save disk space and I/O operations.

3.Index column sorting

MySQL queries only use one index, so if an index is already used in the where clause, then ordeThe columns in r by will not use indexes.Therefore, do not use the sort operation when the database default sorting can meet the requirements; try not to include multiple column sorts, and if necessary, it is best to create a composite index for these columns.

4.Like statement operation

Under normal circumstances, the use of like operations is discouraged.If it must be used, how to use it is also a problem.Like "%aaa%" will not use index but like "aaa%" can use index.

5.Do not perform calculations on the column

The code is as follows:

select * from users where YEAR(adddate)<2007;

The operation will be performed on each row, which will cause the index to fail and perform a full table scan, so we can change it to:

The code is as follows:
select * from users where adddate<'2007-01-01';

6.Do not use NOT IN and <> operations

Above, the MySQL index types are introduced.I hope to be helpful.

Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

atorvastatin 10mg sale & lt;a href="https://lipiws.top/"& gt;purchase lipitor online cheap& lt;/a& gt; lipitor 10mg oral

Ivwhtf

2024-03-08

Leave a Reply

+