php interview topic---16, MySQL creates high-performance index test site

php interview topic---16, MySQL creates high-performance index test site

1. Summary

One sentence summary:

Note: Only write boutique

 

1. The basis of the index?

Book-like directory: Indexing a book-like directory, to find a specific topic of a book, you need to search the book's directory and locate the corresponding page number
Index -> row number -> data: The storage engine uses a similar method to query data, first go to the index to find the corresponding value, and then find the corresponding data row according to the matching index

 

2. What is the impact of indexes on performance?

1. Greatly reduce the amount of data that the server needs to scan, greatly improve the query speed, reduce the writing speed and occupy the disk
2. Help the server avoid sorting and temporary tables
3. Change random I/O to sequential I/O

 

3. For a very small table, such as 50 pieces of data, is an index faster or a full table scan faster?

Full table scan is fast: because there are more steps to query the index, and one more step is slow

 

4. What are the usage scenarios of the index?

High efficiency of full table scans for small tables: For very small tables, full table scans are more efficient in most cases
Very efficient for medium to large tables: indexes are very efficient
Very large tables use partition + index: the cost of creating and using indexes will increase, which can be solved by using partition technology

 

5. What is the type of index (basic)?

Ordinary index: very ordinary index: the most basic index, without any constraints
Unique Index: Unique Constraint: Similar to Normal Index, but with Unique Constraint
Primary key index: uniqueness constraint + no nulls allowed: special unique index, no nulls allowed

 

There are many types of indexes, all of which are common indexes implemented at the storage engine layer
: very common indexes: the most basic indexes without any constraints
Unique indexes: unique constraints: similar to ordinary indexes, but with unique constraints
Primary key index : uniqueness constraint + no nulls allowed: special unique index, no nulls allowed

 

 

6. What is the difference between a unique index and a primary key index (often test)?

There can be multiple unique indexes: a table can only have one primary key index, and there can be multiple unique indexes
The primary key index is a special unique index: the primary key index must be a unique index, and the unique index is not a primary key index
The primary key index has additional functions: the primary key can form a referential integrity constraint with the foreign key to prevent data inconsistency

 

7. Type of index (advanced)?

Composite Index: Combine multiple columns together to create an index that can cover multiple columns
Foreign key index (rarely used): Only InnoDB type tables can use foreign key index to ensure data consistency, integrity and cascading operations
Full-text index (rarely used): MySQL's own full-text index can only be used in MyISAM, and can only perform full-text search in English

 

8. What are the principles of MySQL index creation?

1. The most suitable columns for indexing are the columns that appear in the WHERE clause, or the columns in the join clause rather than the columns that appear after the SELECT keyword
2. To index strings, a prefix length should be specified, which can save a lot of index space
3. Avoid creating too many indexes, which will occupy additional disk space and reduce the efficiency of write operations
4. Select the shortest data type as possible for the primary key, which can effectively reduce the disk usage of the index and improve the query efficiency


1. The most suitable column for indexing is the column that appears in the WHERE clause, or the column that appears in the join clause instead of the column that appears after the SELECT keyword
2. The larger the cardinality of the index column, the better the indexing effect
3. To index strings, a prefix length should be formulated, which can save a lot of index space
. 4. Create a composite index according to the situation, which can improve query efficiency
. 5. Avoid creating too many indexes. The index will occupy additional disk space and reduce write operations. Efficiency
6. Select the shortest data type as possible for the primary key, which can effectively reduce the disk usage of the index and improve the query efficiency

 

9. Notes on MySQL index?

1. Like query, % cannot be in front, you can use full-text index: where name like "% wang%" index will be invalid
2. If the column in the condition before or has an index, and the latter does not, the index will not be used
3. The column type is a string, and the value must be quoted when querying, otherwise the index will fail: where name=100; name is a string, so 100 must be quoted


1. The compound index follows the prefix principle
2. Like query, % cannot be in the front, you can use the full-text index: where name like "% wang%" The index will be invalid
3. The column is null can use the index
4. If MySQL estimates that the use of the index is more than full The table scan is slower and will give up the use of the index: where id>1 and id <100: because there are more steps to query the index.
5. If the column in the condition before or has an index, and the latter does not, the index will not be used.
6. The column type is a string, and the value must be quoted when querying, otherwise the index will fail: where name=100; name is a string, so 100 must be quoted

 

 

 

2. The content is in the summary

 

 

 

 

Reprinted in: https://www.cnblogs.com/Renyi-Fan/p/11073647.html

Guess you like

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