What is the index? How to optimize mysql query by index

1. Index

      When a single MySQL table records the number is too large, CRUD performance will be a sharp decline. Build MySQL MySQL index for efficient operation is very important, the index can greatly improve the retrieval speed of MySQL. Unless the single-table data will always continue to rise in the future, do not start consider the split, the split will bring logic, deployment, operation and maintenance of the complex. Generally integer-based table in ten million or less, the string-based tables in the following five million is not much problem, and in fact many times the performance of a single table MySQL is still a lot of room for optimization, even more than the normal amount of data to support tens of millions.


Index advantages and disadvantages:

Advantage: significantly reduces the amount of data that needs to scan the server, the server can help avoid sorts and temporary tables, fast retrieval, random I / O becomes a sequential I / O, reducing I / O times, quicker retrieval; the index grouping and sorting, grouping and sorting can be accelerated;

Disadvantages: the index table itself, and therefore take up storage space, in general, 1.5 times the data table of the index table space occupied; to maintain and create index table takes time costs, the cost increases as the amount of data ; constructing the index will reduce the data table modification operations (delete, add, modify) the efficiency, because also need to modify the index table at the same time to modify the data table; the need to lock the table when creating an index, so the actual business operations need to idle during.


2. Index type

      Mysql present, there are several index types: FULLTEXT, HASH, BTREE, RTREE.

FULLTEXT

      It is the full-text index, only MyISAM engine support. It can be CREATE TABLE, ALTER TABLE, CREATE INDEX use, but can now create full-text index is only CHAR, VARCHAR, TEXT columns.

      MyISAM and full-text indexing is not born together, it appears to solve the "% word%" low efficiency of this type of fuzzy search for the text of the problem WHERE name LIKE.
FULLTEXT (full text) index, is only available for MyISAM and InnoDB

  • For large data sets, to add data to a no FULLTEXT index table, and the addition rate has a FULLTEXT index table FULLTEXT index faster than data is added to.

  • MySQL comes with the full-text index of the previous 5.6 version can only be used for MyISAM storage engine, if the engine is other data, then the full-text index will not take effect. After the 5.6 version of the InnoDB storage engine began to support full-text indexing

  • In MySQL, full-text indexing detachment English is useful, the current Chinese is not yet supported. After the 5.7 version of the plug-ins by using ngram began to support Chinese.

  • In MySQL, if the string is too short to retrieve the desired results can not be retrieved, retrieval string length of at least 4 bytes. In addition, if the retrieval characters include stop words, the stop words are ignored.

HASH

      A hash index calculated reference values ​​of the index columns the hashCode value, then the value stored execute the physical location of the row of data in the corresponding position hashCode, because using a hashing algorithm, and therefore very fast access, but a value corresponds to only one hashCode, and is distributed way hash. Since the only (the only almost 100%), and the like in the form of key-value pairs HASH, it is suitable as an index.

      HASH index can locate one, do not need to look like a tree indexes as layer by layer, and therefore has a very high efficiency. However, this efficiency is conditional, that is, only the "=" and "in" under conditions effective for the scope of inquiry, ordering and combination index is still inefficient.

BTREE

      BTREE (B + TREE) is a kind of index to the index value according to a certain algorithm, the data stored in a tree structure (binary tree), every time a query is started from the access root tree, traversing Node, obtaining leaf. Since BTREE non-leaf nodes do not store data (data), and therefore need to check all the data to the leaf nodes, and leaf nodes are the same height, so that all the data query speed is the same. This is the default in MySQL and the most common type of index.

RTREE

      RTREE rarely used in MySQL, supports only geometry data type, the type of storage engine supports only MyISAM, BDb, InnoDb, NDb, Archive few.

      Relative to BTREE, RTREE strength lies in the range of search.


3. The index species

General index: only speed up queries.

The only index: + speed up query column value unique (there can be null).

Primary key index: a unique value to speed up queries + column (not have null) + only one table.

Composite index: multi-column values ​​make up an index, a combination of dedicated searching, its efficiency is greater than the index merge, follow the "most left-prefix" principle, the most commonly used as a search or sort on the leftmost column, in descending order, combined index quite to establish a col1, col1col2, col1col2col3 three indexes, but col2 or col3 can not use the index.

Full-text indexing: the content of the text is divided word search.


4. Use Policy Index


4.1 When to use the index?

  • Primary key automatically create a unique index;
  • WHERE or ORDER BY often as a query;
    column statement appears to be indexed;
  • As the sort column to be indexed;
  • Query tables and other related fields, foreign key relationships indexes
  • Tendency under high concurrent combination index;
  • Function may be used for the polymerization column index, for example, the max (column_1) column_1 or when the count (column_1) need to be indexed.


4.2 When not to use indexes?

  • Frequent additions and deletions to the column not to index;
  • There are a large number of duplicate columns are not indexed;
  • Table records too little not to index. Only when the database already have enough test data, its performance results have practical reference value. If only a few hundred of data recorded in the test database, they tend to perform after the completion of the first query was all loaded into memory, which will make subsequent query commands are executed very fast - with or without the use of the index . Only when the database record of more than 1000, the amount of data also exceeded the total amount of memory on the MySQL server, database performance test results to be meaningful.


4.3 Failure of the index case:

  • Can not have a column in the composite index is NULL, if there is, then this column for the composite index is invalid;
  • In a SELECT statement, the index can only be used once, if used in the WHERE clause, then do not use the ORDER BY in;
  • LIKE operation, '% aaa%' not use the index, i.e. the index will fail, but the 'aaa%' can use the index;
  • Expression or function using the index will fail in a column index, for example:
select * from table where ceate_time > unix_timestamp(curdate());

It will take place on each line operations, which will lead to failure while the index for full table scan, so we can change the current time by the program as a parameter:

select * from table where ceate_time > 1524561911;

Other wildcards same, that is to say, the conditions used in the query in a regular expression, only use the index in the case of the first character in the search template is not a wildcard;

  • Used in the query conditions are not equal, including <symbols> and symbol! = Index will lead to failure. In particular: If you are using the primary key index! = The index will not fail, if the primary key index type or an integer index <symbol or> symbol index does not cause failure. (Not equal, including <symbols> symbols and, if a very small proportion of the total recorded, then it will not fail!);
  • Use IS NULL in the query or IS NOT NULL Index can lead to failure;
  • String without the single quotation marks will cause the index fail. More accurate to say that the type of failure can lead to inconsistencies, such as mobile field is a string type, using WHERE mobile = 99999 will result in a failure should be replaced WHERE mobile = '99999';
  • Use the query criteria OR connect multiple conditions can cause failure of the index, unless the conditions of each OR links are indexed and, at this time should be changed twice a query, and then connected by UNION ALL;
  • If the sort fields using the index, then select the field will have to be indexed field, the index otherwise fail. In particular: If the sort is the primary key index select * will not result in failure of the index;
  • Try not to include multi-column sorting, if we must, it is best to build composite index for this queue.


4.4 mysql query optimization:

Field:

  • Make use TINYINT, SMALLINT, MEDIUM_INT integer type and not as INT, if the non-negative plus UNSIGNED;
  • VARCHAR length distribution only really need the space;
  • Instead of using the integer string or an enumeration type;
  • Try to use TIMESTAMP instead of DATETIME;
  • Do not have too many single table field, it is recommended within 20;
  • Avoid using NULL fields, query optimization and is difficult to take up extra space index;
  • With integer to store IP.

index:

  • The index is not possible, to create targeted based on the query, consider listed on WHERE and ORDER BY commands that involve indexing can be used whether the index or full table scan according to EXPLAIN to see;
  • Fields should be avoided to a NULL value is determined in the WHERE clause, will cause the engine to give up using the index and a full table scan;
  • Value distribution is sparse field is not suitable for building an index, such as "sex" field that only two or three values;
  • Character field to build only the prefix index;
  • Character is best not to field a primary key;
  • No foreign key constraint guaranteed by the program;
  • Try not UNIQUE, bound by the procedural guarantees;
  • Idea and query sequence is consistent when using a multi-column index, delete unnecessary single index.

Query sql:

  • SQL can to find out by opening a slower slow query log;
  • Do not do arithmetic column: SELECT id WHERE age + 1 = 10, any operation on the columns will cause a table scan, which includes a database tutorial function, evaluate expressions, etc., to move the operation to the right of the equal sign as a query;
  • sql statement as simple as possible: a sql only a cpu operation; big statement demolition small statement, reducing the lock time; a big sql entire library can be blocked;
  • Do SELECT *;
  • IN OR rewritten: n OR level of efficiency, the efficiency is IN log (n) level, number of proposals controlled within IN 200;
  • Do not function and triggers the application implementation;
  • Avoid% xxx-style inquiry, '% xxx%' does not use an index, you can use full-text indexing, and then:
SELECT * FROM tablename MATCH(index_colum) ANGAINST(‘word’);
  • Less JOIN;
  • Were compared using the same type, such as a '123' and '123' ratio, ratio of 123 and 123;
  • To follow most composite index prefixed principle, the highest frequency grouping column sorting on the left, and so on;
  • Avoid the use of or in the WHERE clause = <> operator, otherwise the engine to give up using the index and a full table scan!;
  • For continuous values, without using BETWEEN IN: SELECT id FROM t WHERE num BETWEEN 1 AND 5;
  • Do not take a full list of data tables, to use LIMIT to pagination, page numbers are not too big;
  • Use a short index, if possible, you should specify a prefix length. For example, if there is a CHAR (255) column, if the 10 or 20 characters in the front, the only multi-value, then do not index the entire column. Short index can not only speed up the search and save disk space and I / O operations.


4.5 Frequently Asked Questions Index

1, the index is doing?

      Index is used to quickly find a specific value in a column in a row. Do not use the index, mysql must begin to read the entire table from the first record until you find the relevant line. The larger the table, the more time it takes. If the table has a column in the query index, mysql can quickly reach a position to find the middle of the data file, it is not necessary to see all the data.

      Most mysql index (primary key, index, unique, fulltext) stored in the B-tree, but spatial data types using R-tree index, and also supports the hash index table memory.


2, a good index complex, I understand how the index, is there a more vivid example?

      Imagine that you have in front of this dictionary, data is the text of the book, you're the cpu, and the index is the directory of the book.


3, index the better?

      In most cases, the index can significantly improve query efficiency. But:
data changes (additions and deletions) are required to maintain the index, and therefore more indexes mean more maintenance costs;
also means more control space (a book 100, there are 50 catalog?);
Too small table, building the index may be slower (read brochures 2, you need to first go to the directory?)


4, the index field types problem

      text type, an index may be built (specify length); Comprehensive MyISAM storage engine can not exceed the length of 1000 bytes; used to screen and keep the value of the index columns the same data type.


5, like to use the index?

      Minimize queries like, but it is not absolutely unavailable, 'xxx%' can be used in the index. In addition like, the operator can use the index:

<,<=,=,>,>=,between,in

These less than the index:

<>,not in,!=



6, what kind of fields are not suitable for building an index?

      Uniqueness column value is too small (such as gender, type), it is not suitable for indexing. (What size? Generally speaking, the value of data with more than 15% of the table, there is no need to build the index) data is not updated very frequently for indexing.


7, a query can use multiple indexes?

      Not


8, multi-column query how to build the index?

      A query can only use one index, a column indexed or indexed column b? Who discrimination (less the same value) higher, who built! Of course, the joint index is a good program.


9, issue joint index

-- 命中col1、col2联合索引
select col1,col2 from test where col1 = 'xxx';
-- 不能命中col1、col2联合索引
select col1,col2 from test where col2 = 'xxx';

So in most cases, there are col1, col2 index, and would not go to build col1 indexed.


10. What are the common situation can not use the index?

like '%xxx'
not in
!=

The columns function calculation, such as:

where md5(password) = "xxx"

Keep the string type field values ​​(such as phone number), the query is remember not to lose quoted value, otherwise you can not hit the index:

select * from test where mobile = 13800002222;

If the mobile field is a char or varchar type, the above query can not hit the index, should be:

select * from test where mobile = '13800002222';


11, NULL problem

      Null index will lead to non-existent, so the design table structure should be avoided in the presence of NULL.
It can be used to express other ways, such as -1.

Reference:
https://blog.csdn.net/liutong123987/article/details/79384395

https://blog.csdn.net/github_26672553/article/details/82887009

https://blog.csdn.net/tongdanping/article/ details / 79878302

Guess you like

Origin www.cnblogs.com/qingfengEthan/p/11329144.html