[Mysql index classification]

1. Index classification:

 Index column number classification

   single column index

   Multi-column index (compound index)

 

Type classification:

Normal INDEX : The most basic index without any restrictions

Primary key index (PRIMARY) : It is a special unique index that does not allow null values. Generally, the primary key index is created at the same time when the table is created. 

Unique index (UNIQUE) : Similar to "normal index", the difference is: the value of the index column must be unique, but null values ​​are allowed.

Full-text index (FULLTEXT) : It can only be used for MyISAM tables. It is used to retrieve textual information in an article. For large data, generating full-text indexes is time-consuming and space-consuming.

Combined index : In order to improve the efficiency of mysql, a combined index can be established, following the "leftmost prefix" principle.

Spatial index : It is an index established on the field of spatial data type. There are 4 types of spatial data in MYSQL, namely GEOMETRY, POINT, LINESTRING, and POLYGON.

 

What is the difference between mysql index types normal, unique and full text?

normal: indicates a normal index

unique: Indicates a unique index that does not allow duplicates. If the field information is guaranteed not to be duplicated, for example, when the ID number is used as an index, it can be set to unique

full textl: Represents an index for full text search. FULLTEXT works best when searching for very long articles. It is used for relatively short text, if it is only one or two lines of words, ordinary INDEX can also be used.

In summary, the category of the index is determined by the content characteristics of the indexed field, usually normal is the most common.

 

Second, the SQL statement to add an index

1. Add PRIMARY KEY (primary key index) 

MySQL>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` )

 

2. Add UNIQUE (unique index) 

mysql>ALTER TABLE `table_name` ADD UNIQUE ( `column` ) 

 

3. Add INDEX (normal index) 

mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column` ) 

 

4. Add FULLTEXT (full-text index) 

mysql>ALTER TABLE `table_name` ADD FULLTEXT ( `column`) 

 

5. Add a multi-column index 

mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )

 

query index

SHOW INDEX FROM table_name;

 

view index

mysql> show index from tblname;

mysql> show keys from tblname;

 

drop index

drop index index_name on table_name ;

alter table table_name drop index index_name ;

alter table table_name drop primary key ;

 

 

SQL statement to query the size of disk space occupied by all databases:

select table_schema,concat(truncate(sum(data_length)/1024/1024,2),'MB') as data_size,

concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size

from information_schema.tables

group by table_schema

order by sum(data_length) desc;

 

 

SQL statement to query the disk size of all tables in a single database:

select table_name,concat(truncate(data_length/1024/1024,2),'MB') as data_size,

concat(truncate(index_length/1024/1024,2),'MB') as index_size

from information_schema.tables where table_schema='employees'

order by data_length desc;

 

 

Occupancy of the lookup table

SELECT CONCAT(table_schema,'.',table_name) AS 'Table Name', 

CONCAT(ROUND(table_rows/1000000,2),'M') AS 'Number of Rows', 

CONCAT(ROUND(data_length/(1024*1024*1024),2),'G') AS 'Data Size', 

CONCAT(ROUND(index_length/(1024*1024*1024),2),'G') AS 'Index Size' , 

CONCAT(ROUND((data_length+index_length)/(1024*1024*1024),2),'G')

AS'Total'FROM information_schema.TABLES 

WHERE table_schema LIKE 'edb_a%';

 

Three, the seven principles of indexing

In order to make the use of the index more efficient, when creating an index, you must consider which fields to create an index on and what type of index to create. There are seven principles:

1. Choose a unique index

2. Index fields that frequently require sorting, grouping, and union operations

3. Build indexes for fields that are often used as query conditions

4. limit the number of indexes

5. Use indexes with as little data as possible

6. Use prefixes as much as possible to index

7. Delete indexes that are no longer used or rarely used

 

Example:

1. The leftmost prefix matching principle, a very important principle, mysql will always match to the right until it encounters a range query (>, <, between, like) and stop matching, such as a = 1 and b = 2 and c > 3 and d = 4 If the index of (a,b,c,d) order is established, d will not use the index, if the index of (a,b,d,c) is established, it can be used, a,b,d The order can be adjusted arbitrarily.

 

2. = and in can be out of order, such as a = 1 and b = 2 and c = 3. The (a,b,c) index can be built in any order, and the mysql query optimizer will help you optimize it into a form that the index can recognize

 

3. Try to choose a column with a high degree of discrimination as an index. The formula for the degree of discrimination is count(distinct col)/count(*), which indicates the proportion of fields that are not repeated. The larger the proportion, the fewer records we scan, and the distinction between unique keys The degree is 1, and some status and gender fields may have a degree of 0 in front of big data. Some people may ask, is there any experience value for this ratio? Different usage scenarios, this value is also difficult to determine. Generally, the fields that need to be joined are required to be above 0.1, that is, an average of 10 records are scanned.

 

4. The index column cannot participate in the calculation, keep the column "clean", such as from_unixtime(create_time) = '2014-05-29', the index cannot be used, the reason is very simple, the b+ tree stores all the field values ​​in the data table , but when retrieving, you need to apply the function to all elements to compare, which is obviously too expensive. So the statement should be written as create_time = unix_timestamp('2014-05-29');

 

5. Try to expand the index, do not create a new index. For example, there is already an index of a in the table, and now you need to add an index of (a, b), then you only need to modify the original index

 

 

 

 

Fourth, the inadequacy of the index

The above are all about the benefits of using indexes, but excessive use of indexes will lead to abuse. So indexing also has its drawbacks:

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

2. Create an index file that will occupy disk space. In general, this problem is not serious, but if you create multiple composite indexes on a large table, the index file will swell very quickly. Indexes are only one factor to improve efficiency. If your MySQL table has a large amount of data, you need to spend time researching to establish the best index, or optimize the query statement.

 

5. Precautions for using indexes

Here are some tips and considerations when working with indexes:

1. The index will not contain columns with NULL values

As long as the column contains NULL values, it will not be included in the index. As long as there is one column in the composite index that contains NULL values, 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 indexes

Index the list, specifying a prefix length if possible. For example, if you have a CHAR(255) column, don't index the entire column if most of the values ​​are unique within the first 10 or 20 characters. Short indexes 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, the columns in the order by will not use the index. Therefore, the default ordering of the database can conform to

 

Do not use sorting operations when required; try not to include sorting on multiple columns, and create composite indexes for these columns if necessary.

4. The like statement operation is generally discouraged from using the like operation. If it must be used, how to use it is also a problem. like "%aaa%" will not use the index while like "aaa%" will use the index.

5. Do not operate on columns

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

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

select * from users where adddate<‘2007-01-01';

6. Do not use NOT IN and <> operations

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326221341&siteId=291194637