The principle mysql series --sql

count(*)

MyISAM engine the total number of ⾏ ⼀ tables exist on the disk, so YES count (*) when a direct return this number, efficiency A HIGH; but added a condition can not quickly return
⽽ InnoDB engine trouble, it perform count (*) when required data ⼀ ⾏ ⼀ ⾏ read out from the engine ⾥ screen and then accumulated count.

InnoDB row select line is calculated because different things to the number of books, single read data to ensure the accuracy of things.
For COUNT (*) MySQL optimized, the general index less than the primary key index data, count for each calculated index value (*) is the same, mysql optimizer chooses the smallest index tree traversal

While MyISAM table count (*) soon, but do not support the transaction;
Show Status command although the Table to return soon, but not accurate;
InnoDB table direct count (*) will traverse the whole table, though accurate, but can cause performance problems .

For the total number of scenes frequently queried, can record their own table Total
buffer count: Total number of queries redis start their subsequent decrease the number of maintenance, but the decrease is not inserted between the database and the data quantity redis atomic operation and cause the actual number of data differ, while It does not support distributed transaction.
Database count: The total number of each table stored in the database, and using the actual number of things to achieve data consistency

Therefore especially must first figure out count () semantics. count () is a function ⼀ polymerized, result sets returned ⼀ ⾏ ⾏ judged if the count function parameter is not NULL, the cumulative value is increased by one, or without. Finally, the cumulative return value.
Therefore, count (*), count (primary key id) and count (. 1) are represented by the total number of ⾏ returns full EMPTY criteria set; ⽽ count (field), then return to full EMPTY condition data ⾏ ⾥ ⾯ parameters "field" is not the total number of NULL.

⾄ time to analyze the performance difference, so you can remember to get accustomed principle:
What 1. server layer to give anything;
2. InnoDB only to the necessary value;
3. The optimizer only optimize count (*) of semantics "fetch ⾏" other "significant ⽽ easy to ⻅" optimization does not do.

count (primary key id) , the InnoDB engine will traverse the entire table, each value of the id ⼀ camera settings that are taken out, returned to the server layer. Get the server layer id, judgment is unlikely to be empty, press ⾏ accumulation.
COUNT (1) , traverse the entire table InnoDB engine, but not the value. For each server layer ⼀ ⾏ returned put ⼀ digital "1" into, determination is impossible to empty, by ⾏ accumulation.
Just look at the difference between these two Using the method, you can come out the contrast, count (1) Perform ⽐ have to count (primary key id) fast. Because the return id involve ⾏ parsed data, and the copy operation ⻉ field value from the engine.
count (Field) :
1. If the "field" is defined as not null, then ⼀ ⾏ ⾏ read out from the recording ⾥ ⾯ this field, it can not be determined null, accumulated by ⾏;
2. if the "field" is defined to allow is null, then perform to time, there may be null to judge, but also to determine the value taken out again to record the next, not only accumulate null.

That is the principle of Article first frame before the screen for, server layer to what field, InnoDB returns what field.
But count (*) are the exception, not the entire field will be taken out, ⽽ is designed ⻔ optimized, not value. count (*) is certainly not null, according to ⾏ accumulate.

Primary key id definitely comes in handy empty ah, why not in accordance with the count (*) under optimized to handle, because too many similar optimization, count (*) has been optimized, others temporarily optimization

The conclusion is: sorted efficiency, then, count (field) <count (primary key id) <count (1) ≈count (*), so I suggest you try Using the count (*).
Self-test Conclusion: selct (1)> select ( *)> select ( auto-increment primary keys)> select (Business numeric primary key)> select (Business char primary key)> select (Normal unique index)

order by

Full field sort
MySQL will give each thread is allocated memory block ⼀ Use to sort, called sort_buffer.

select city,name,age from t where city='杭州' order by name limit 1000;

1. Initialize sort_buffer, is determined to put START name, city, age three fields;
2. Find the first frame from a full index EMPTY city city = 'Hangzhou' conditions the primary key id, that is, in FIG. ID_X;
3. primary key id ⾏ remove the entire index, taking name, city, age values of three fields, the stored START sort_buffer;
4. remove the primary key of the record id ⼀ city from the index;
5. the step is repeated until the value of the city is less than 3,4 underexposed query is for still, the corresponding primary key is id in FIG ID_Y;
6. the data do a quick sort_buffer the sorted field name;
7. the sorted results are returned from the former to the client 1000 ⾏.

"Sort by name" this action may be completed in memory, it may be necessary Using external sort, depending on the desired memory and sorting parameters sort_buffer_size.
sort_buffer_size, MySQL is open for the sort of memory (sort_buffer) is zoomed ⼩. If the amount of data to be sorted ⼩ in sort_buffer_size, sorting is completed in memory. However, if the amount of data sorted zoomed too, memory does not fit, you must not adversely Using temporary disk sorting pieces of auxiliary files.

rowid sort
⾥ screens appear on ⾯ this algorithm process, only the original data table read ⼀ times, the rest of the operations are in sort_buffer and temporary files in to perform to the problem lies in the field for a long time memory holds a small amount of data, It is divided into as many temporary files, inefficient

= 16 max_length_for_sort_data the SET;
max_length_for_sort_data, is ⼀ parameters ⻓ degrees MySQL secondary ⻔ Use to control the ordering of data ⾏. Meaning that, if a single camera settings that ⻓ exceeds this value, MySQL is considered a single ⾏ zoomed too, want to change ⼀ algorithms

city, name, age define the total ⻓ of these three fields was 36, I max_length_for_sort_data set to 16, get a new algorithm:
The new algorithm clicks into place sort_buffer the field, only to be sort of a column (that is, the name field) and the primary key id .
But this time, because the results of sorting and less city age field value, and can not return directly, the entire process becomes YES comp Submenu shown below:
1. Initialize sort_buffer, is determined to put START two fields, i.e., id and name;
2. find the first frame from a full index EMPTY city city = 'Hangzhou' primary key id conditions, i.e. in FIG ID_X;
3. id to the primary key index remove the entire ⾏, take name, these two fields id , the deposit START sort_buffer;
4. remove a record from the index ⼀ city primary key ID;
3,4- until dissatisfaction EMPTY city = 'Hangzhou' for still conditions, i.e. in FIG ID_Y 5. repeat steps;
6. sort_buffer data sorting according to the field name into ⾏;
7. traversing sorting results, taken before ⾏ 1000, and taken back to the original table City, name and age three fields returned to the client in accordance with the value of id.
That is: find and sort field into conditions where id by sort_buffer, the sorted id to the original table by extraction results returned.

Sorting is resistance performance operation by the sort field joint index and made of filter fields, the natural order, or use a covering index

 

Small surprise 

 1, add up, download Union high commission, to receive major platform hidden coupons, each purchase province 10 8 do not fragrant, registered under the Fanger Wei code by the user can add micro letter liershuang123 (Micro Signal) receive the value of thousand Yuan massive learning video.

We dedicate part of the information for the table:

Software eBook: link: https: //pan.baidu.com/s/1_cUtPtZZbtYTF7C_jwtxwQ extraction code: 8ayn
architect two: link: https: //pan.baidu.com/s/1yMhDFVeGpTO8KTuRRL4ZsA extraction code: ui5v
architect stage courses: link: https: //pan.baidu.com/s/16xf1qVhoxQJVT_jL73gc3A extraction code: 2k6j

          

 2, I paid money buying front end of a source separation scaffolding, is 10 yuan sale, plus micro-channel acquisition source liershuang123

 

 

Guess you like

Origin www.cnblogs.com/leskang/p/12610630.html