High Performance Mysql (third edition)

1. Mysql architecture and history

  1. MyISAM only write data to memory, waiting for the operating system on a regular basis to brush out the data on the disk. So, we can not guarantee power failure, data is not lost.
  2. When infobright for Mysql data is too large, and as a data warehouse.
  3. Modify the table engine, it will lose all of the original engine characteristics table.
  4. When Mysqldump import data, you need to pay attention to mysqlDump default will load the drop table between the create table statement , not pay attention to this point, will result in the loss of data in a database table Central Plains.
  5. Mysql5.5 start Mysql InnoDB became the default engine.

2. Server performance analysis

  1. Performance analysis tools:
    1. NewRelic。
    2. xhprof。
  2. Mysql 5.0, slow query log can be displayed in milliseconds.

3. Schema datatypes and Optimization

1. integer less than the cost of a string type. Because the character string needs to set and collation.

2. Time Type:

  1. DateTime: storage time and date, accurate to the second. <1001 - 9999 2008>
  2. TimeSamp: storage time and date, accurate to the second. DateTime only half of the storage space, and will change with time zones, with a special automatic update capability. And the time allowed smaller. <January 1, 1970 --2038 2008>

3. Integer type:

  1. Data range: -2 ^ (n-1) ~ 2 ^ (n-1) - 1, N is the number of bits of space. And can select unsigned property, which can not be negative, the upper limit of positive number doubled. For a given length of the integer is useless and does not limit the range of values ​​stored in the control data bits only interactive display some database tools. In storage and computing, Int (1) and Int (20) are the same.
    1. tinyInt: 8 bit data space.
    2. smallInt:16位。
    3. MediumInt: 24位。
    4. Int: 32 bits.
    5. BigInt: 64 bits.
  2. Real Type: For use decimal smaller data storage for large amounts of data, they can be considered instead of using bigInt Decimal, the latter multiplied by the corresponding decimal multiples can.
  3. String Type: varchar can store data for longer, and will not clear the end of the empty string data; char length is fixed, the data is automatically cleared at the end of the empty string. MYISAM default string compression index, which leads to the query is much slower, resulting in a significant decline in performance.

4. Index

Better to use the article index

  1. Where conditions are listed when using the index: the first lookup based on the index value, and then returns the row containing the value.
  2. Primary key index and the non-primary key index is different, the value of the primary key index is stored in the data field of the entire line, rather than the entire primary key values ​​of data rows stored in the index field, and storing the value of the primary key field.

2. The most left-prefix:

When using a composite index, use only in accordance with the order of the index key, you can use the index. Skip Left can not use the most to the index.

3. covering index: Select the column, the index is custom columns.

4. The role of the index

  1. Let the server quickly navigate to the location specified in the table.
  2. Reducing the amount of data the server needs to be scanned.
  3. Avoid sorting and temporary tables.
  4. Converting the random I / O as a sequential I / O.

5. Prefix index

  1. Definition: For the Blob and Text, or long varchar type column, if the use of the general index, because a large amount of data, the index will lead to dramatic growth in space, therefore, in order to reduce the index space, therefore, the index prefix like. And for mysql for these three types, the index does not allow the full length of the columns.
  2. Calculate the most suitable prefix length: COUNT (DISTINCT LEFT (name, 1)) / count (*) AS pre1. Modify the length.
  3. Achieved: TABLE Alter userthe ADD KEY (name (. 8));. 8: This name is the prefix index for setting the length of the column.

6. Combined Index

  1. Before mysql5.0, it can not be used simultaneously to multiple indexes, only allow a single index onset. But after mysql 5.0, the index is created when there is a problem, mysql optimization will be indexed, the index can be used, combined into one index to use.

7. clustered index

  1. Definition: not index type, but a data storage. InnoDB clustered index is actually stored in the same structure of a B-Tree index and data lines. Inode corresponding to the index data is actually stored in the leaf pages on. While the data lines because they can not be stored in two different places, a table can have only one clustered index.
  2. InnoDB aggregated data by the primary key.
  3. the Table Optimize : When you delete data, mysql will not recover, was occupied by the deleted data storage space, as well as index bits. But empty in there, but waiting for new data to make up for the vacancy, so there is a lack, if half past one, there is no data to fill the vacancy, then so too a waste of resources. So for more frequent trouble writing table, to conduct optimize regularly, once a month, depending on the actual case may be.

8. covering index

  1. Definitions: select the fields, there is in the index, you do not need to come back to the table query data. Will greatly enhance the performance.

9. Sort index scan do

  1. mysql ordered result generated only by sorting operation and by an index sequential scan .
  2. If after explain type of index, the description uses an ordered index sequential scanning method.
  3. mysql can use the same sort index only meet, but also to meet to find rows of data.
  4. achieve:
    1. Only when the order of the index column order and the ORDER BY clause is exactly the same, and all sort direction (positive or reverse order) are the same time, mysql will use the index to do to sort the results.
    2. If the query needs associated with multiple tables, only when the field ORDER BY clause reference in its entirety for the first table, you can use the index to sort. ORDER BY also have to meet the most left-prefix index.
    3. If the first column of the index is constant, then the ORDER BY may not be required to meet the most left-prefix. eg: where name = 'Alex' order by age. index (name, age). However, if inconsistent direction and shoot the sequence order index order can not be used to index.

10. The compression (compression prefix) Index

  1. Definition: prefix by compression, reducing storage space index, the index can be placed so that more memory. The default can only compress strings, integers, you need to set the parameters.
  2. Calculated: a first value to a full save index block, then the first value and the other values ​​are compared to obtain the remaining number of bytes and a different portion of the same suffix prefix, then this part can be stored. eg: 1. perform 2. performance, when the stored data storage: 7, ance.

11. The redundant and duplicate index

  1. Definition: duplicate index refers to the same column in accordance with the same order to create the same type index.

12. Index and lock

  1. After mysql5.1, InnoDB can filter the server off-line after the release of the lock, but in earlier versions of mysql, only when a transaction is committed, will release the lock.

13. Sorting Optimization

  1. Distinct: an intermediate table is generated after execution, will reduce performance, occurs Using temporary.
  2. Using fileSort: because the sort of data is too large in file sorting, and adding indexes, sort fields in the index.

5. Query Optimization

  1. using (Field): field must be the same name in both tables to use. ON equivalent functionality.
select t1.id, t1.name
from test t1
inner join test2 using (id)
等同于
inner join test2 t2 ON t2.id = t1.id
  1. Even when the query table, association table out of the best fields in the index, but the index using data does not need to return to the rows of data acquisition data.
  2. Delays associated with : the need to obtain access to the records and then returns all the columns in the original table columns query needs according to the association. This technique can be used to optimize the query associated with the limit clause.
问题:查询多少页的多少条数据
1.常规查询
SELECT id,name,age
FROM `user`
LIMIT 50, 5

2.延迟关联
SELECT  id, name, age
FRO `user`
INNER JOIN (   // 先查询出需要的id,因为id是主键存在索引,可以直接从索引中,获取到需要的id。
	SELECT id	// 再使用inner join,根据id,查询需要的其余列。
	FROM `user`
	LIMIT 50, 5
) as user1 USING(id)


Guess you like

Origin blog.csdn.net/weixin_38608626/article/details/88817456