MySQL MySQL [ten] recognize recognized index index

MySQL understanding Index

 

What is the index?

In MySQL index, also known as a "key", it is a data structure storage engine used to quickly find the record. Index for good performance
is critical, especially when the amount of data in the table more and more, the index more important effect on performance.
Index optimization should be the most effective means to optimize the performance of queries. Index can easily improve query performance by several orders of magnitude.
Dictionary index is equivalent to the sequencer table, if you want to check a word, if you do not use the sequencer table, you will need hundreds of pages from one page to check.

Principle index

Indexing works

The purpose of the index is to improve query efficiency, and we used the catalog for books is a reason: to locate the chapter, and then navigate to a section in this chapter, and then find the pages. There are similar examples: dictionary, check train trips, airplane flights, etc.

Essentially: to filter through continuous narrow range of data you want to get the final results you want, while the random events become the order of events, that is to say, with this indexing mechanism, we can always use Find a way to lock the same data.

Database is the same, but obviously much more complex, because not only faced with the equivalent of a query, and the query range (>, <, between, in), fuzzy queries (like), and set the query (or), and so on. Database should choose how to deal with all kind of ways the problem? We recall the example of the dictionary, can not put the data into segments and sub-queries it? The simplest if 1000 data, the first segment is divided into 1 to 100, 101 to 200 into the second segment, a third segment into 201-300 ...... article such check data 250, as long as the third stage to find , all of a sudden go to 90% of invalid data in addition. But if it is a record 10 million, divided into paragraphs is better? Slightly algorithm based on the students think of the search tree, the average complexity is lgN, has good query performance. But here we have overlooked a critical issue, the complexity of each model is based on the same operating costs to consider. The database implementation is more complicated, on the one hand the data is stored on disks, on the other hand in order to improve performance, but also every part of the data can be read into memory to compute, because we know the cost of access to the disk is about one hundred thousand access memory around times, so simple search tree is difficult to meet the complex application scenarios.

Disk IO and pre-reading

Mentioned earlier access to the disk, then here briefly explain the disk IO and pre-reading, reading data on the disk is a mechanical movement, each time it takes to read the data can be divided seek time, rotational latency, transmission time three section, refers to seek the magnetic track arm moves to the specified time required, generally in the mainstream disk 5ms or less; rotational delay is that we hear disk rotational speed, such as a disk 7200, it can be represented by 7200 revolutions per minute , that is able to turn 120 times a second, rotational latency is 1/120/2 = 4.17ms; refers to a transmission time to read or write data from disk to disk, usually in a few tenths of milliseconds, with respect to The first two times negligible. So once the disk access time, namely a disk IO time equal to approximately about 5 + 4.17 = 9ms, it sounds pretty good, but you know a 500 -MIPS (Million Instructions Per Second) The machine can perform per second 5 million instructions, because the instruction relies on the electrical properties, in other words the first time to perform IO can perform about 450 million instructions, databases easily one hundred thousand millions and ten million data, each 9 milliseconds, obviously a disaster. FIG computer hardware delay the comparison chart for reference:

Considering the very high disk IO operation, the computer operating system to do some optimization, when the IO once, not just the current disk address of the data, but also the adjacent data is read into memory buffer , because the local pre-reading principle tells us that when a computer accesses data address when adjacent data will soon be accessed. Every time we read IO data call a (page). How much data with a specific operating system, generally for the 4k or 8k, that is, when we read the data in a fact only occur once IO, the data structure design theory for the index is very helpful.

Understanding key

Copy the code
Mysql knowledge in Key: 
    index Key general index can speed up queries, secondary index 
    unique key unique + index, secondary index 
    primary key unique clustered index + + non-null 
        primary key as a condition of a query if the index can make it take effect efficiency is always higher 
    foreign key does not have an index, but its appearance in the associated field is the unique index 
    field primary key and unique identifier do not need to add an index 
        can directly use the index to speed up queries 
    can be unique time do not try to index 
        unique addition is the index of the only constraint outside can do, if you do, the only constraint 
        b + trees more healthy
Copy the code
Copy the code
Index: directory actually use when searching a table 
    clustered index: leaf node is stored directly in the line of content 
        only innodb storage engine have a clustered index 
        the primary key 
    secondary indexes: specific address digits leaf node points to data 
        innodb and are myisam there may 
innodb corresponds to 2 file table structure index data + 
myisam file table structure corresponding to three auxiliary cable pure data
Copy the code

Clustered index and secondary indexes

Copy the code
Gender Age name ID 
. 1 MALE Alex 18 is 
Primary Key index 
aggregation secondary indexes index 

to create a regular index 
create index on table index name (field names) 
desc name; 
+ -------- + ------- ---------------- + ------ + ----- + --------- + ---------- + ------ 
| Field, | Type | Null | Key | the Default | Extra | 
+ -------- + -------------------- + ------ + ----- + --- --------- + ---------------- + 
| the above mentioned id | int (11 ) | NO | the PRI | NULL | AUTO_INCREMENT | 
| name | VARCHAR (20 is) | YES | | NULL | | 
| Sex | enum ( 'MALE', 'FEMALE') | NO | | MALE | |  
| Age | int (. 11 ) | YES | MUL | NULL | |
| the dep_id | int (. 11 ) | YES | | NULL | |
+--------+-----------------------+------+-----+---------+----------------+
show create table 表名;
| employee | CREATE TABLE `employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `sex` enum('male','female') NOT NULL DEFAULT 'male',
  `age` int(11) DEFAULT NULL,
  `dep_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `ind_age` (`age`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 |
Copy the code

in conclusion:

Clustered index: leaf node directly stored line content, healthy trees only three times faster query speed IO; (select * from table where id = 20;), to find content with a clustered index;

Secondary indexes: Digital leaf node points to the data memory address, we find by using secondary indexes need to return the memory address of the data table to a clustered index, holding the memory address of the data to find the real data, you need six times IO;

Note: If we do not create a primary key (clustered index) in the creation of the table structure, then mysql will automatically help us create a primary key, the primary key we can not see, because only the primary key is the clustered index, then our future inquiry data tables are through secondary index queries, because IO secondary index encountered more than a clustered index, query speed more slowly, so in general we are all custom primary key when creating the table structure, so better the advantages of using the clustered index;

What is the index?

In MySQL index, also known as a "key", it is a data structure storage engine used to quickly find the record. Index for good performance
is critical, especially when the amount of data in the table more and more, the index more important effect on performance.
Index optimization should be the most effective means to optimize the performance of queries. Index can easily improve query performance by several orders of magnitude.
Dictionary index is equivalent to the sequencer table, if you want to check a word, if you do not use the sequencer table, you will need hundreds of pages from one page to check.

Principle index

Indexing works

The purpose of the index is to improve query efficiency, and we used the catalog for books is a reason: to locate the chapter, and then navigate to a section in this chapter, and then find the pages. There are similar examples: dictionary, check train trips, airplane flights, etc.

Essentially: to filter through continuous narrow range of data you want to get the final results you want, while the random events become the order of events, that is to say, with this indexing mechanism, we can always use Find a way to lock the same data.

Database is the same, but obviously much more complex, because not only faced with the equivalent of a query, and the query range (>, <, between, in), fuzzy queries (like), and set the query (or), and so on. Database should choose how to deal with all kind of ways the problem? We recall the example of the dictionary, can not put the data into segments and sub-queries it? The simplest if 1000 data, the first segment is divided into 1 to 100, 101 to 200 into the second segment, a third segment into 201-300 ...... article such check data 250, as long as the third stage to find , all of a sudden go to 90% of invalid data in addition. But if it is a record 10 million, divided into paragraphs is better? Slightly algorithm based on the students think of the search tree, the average complexity is lgN, has good query performance. But here we have overlooked a critical issue, the complexity of each model is based on the same operating costs to consider. The database implementation is more complicated, on the one hand the data is stored on disks, on the other hand in order to improve performance, but also every part of the data can be read into memory to compute, because we know the cost of access to the disk is about one hundred thousand access memory around times, so simple search tree is difficult to meet the complex application scenarios.

Disk IO and pre-reading

Mentioned earlier access to the disk, then here briefly explain the disk IO and pre-reading, reading data on the disk is a mechanical movement, each time it takes to read the data can be divided seek time, rotational latency, transmission time three section, refers to seek the magnetic track arm moves to the specified time required, generally in the mainstream disk 5ms or less; rotational delay is that we hear disk rotational speed, such as a disk 7200, it can be represented by 7200 revolutions per minute , that is able to turn 120 times a second, rotational latency is 1/120/2 = 4.17ms; refers to a transmission time to read or write data from disk to disk, usually in a few tenths of milliseconds, with respect to The first two times negligible. So once the disk access time, namely a disk IO time equal to approximately about 5 + 4.17 = 9ms, it sounds pretty good, but you know a 500 -MIPS (Million Instructions Per Second) The machine can perform per second 5 million instructions, because the instruction relies on the electrical properties, in other words the first time to perform IO can perform about 450 million instructions, databases easily one hundred thousand millions and ten million data, each 9 milliseconds, obviously a disaster. FIG computer hardware delay the comparison chart for reference:

Considering the very high disk IO operation, the computer operating system to do some optimization, when the IO once, not just the current disk address of the data, but also the adjacent data is read into memory buffer , because the local pre-reading principle tells us that when a computer accesses data address when adjacent data will soon be accessed. Every time we read IO data call a (page). How much data with a specific operating system, generally for the 4k or 8k, that is, when we read the data in a fact only occur once IO, the data structure design theory for the index is very helpful.

Understanding key

Copy the code
Mysql knowledge in Key: 
    index Key general index can speed up queries, secondary index 
    unique key unique + index, secondary index 
    primary key unique clustered index + + non-null 
        primary key as a condition of a query if the index can make it take effect efficiency is always higher 
    foreign key does not have an index, but its appearance in the associated field is the unique index 
    field primary key and unique identifier do not need to add an index 
        can directly use the index to speed up queries 
    can be unique time do not try to index 
        unique addition is the index of the only constraint outside can do, if you do, the only constraint 
        b + trees more healthy
Copy the code
Copy the code
Index: directory actually use when searching a table 
    clustered index: leaf node is stored directly in the line of content 
        only innodb storage engine have a clustered index 
        the primary key 
    secondary indexes: specific address digits leaf node points to data 
        innodb and are myisam there may 
innodb corresponds to 2 file table structure index data + 
myisam file table structure corresponding to three auxiliary cable pure data
Copy the code

Clustered index and secondary indexes

Copy the code
Gender Age name ID 
. 1 MALE Alex 18 is 
Primary Key index 
aggregation secondary indexes index 

to create a regular index 
create index on table index name (field names) 
desc name; 
+ -------- + ------- ---------------- + ------ + ----- + --------- + ---------- + ------ 
| Field, | Type | Null | Key | the Default | Extra | 
+ -------- + -------------------- + ------ + ----- + --- --------- + ---------------- + 
| the above mentioned id | int (11 ) | NO | the PRI | NULL | AUTO_INCREMENT | 
| name | VARCHAR (20 is) | YES | | NULL | | 
| Sex | enum ( 'MALE', 'FEMALE') | NO | | MALE | |  
| Age | int (. 11 ) | YES | MUL | NULL | |
| the dep_id | int (. 11 ) | YES | | NULL | |
+--------+-----------------------+------+-----+---------+----------------+
show create table 表名;
| employee | CREATE TABLE `employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `sex` enum('male','female') NOT NULL DEFAULT 'male',
  `age` int(11) DEFAULT NULL,
  `dep_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `ind_age` (`age`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 |
Copy the code

in conclusion:

Clustered index: leaf node directly stored line content, healthy trees only three times faster query speed IO; (select * from table where id = 20;), to find content with a clustered index;

Secondary indexes: Digital leaf node points to the data memory address, we find by using secondary indexes need to return the memory address of the data table to a clustered index, holding the memory address of the data to find the real data, you need six times IO;

Note: If we do not create a primary key (clustered index) in the creation of the table structure, then mysql will automatically help us create a primary key, the primary key we can not see, because only the primary key is the clustered index, then our future inquiry data tables are through secondary index queries, because IO secondary index encountered more than a clustered index, query speed more slowly, so in general we are all custom primary key when creating the table structure, so better the advantages of using the clustered index;

Guess you like

Origin www.cnblogs.com/youxiu123/p/11493014.html