Mysql advanced [3] discusses Mysql optimization

1. View the detailed information of sql through explain

Mysql sql optimization companies mainly optimize slow sql, optimize statements, and optimize indexes

View the detailed information of sql through explain, and analyze the problems existing in sql statements, such as whether the index is used, whether the index is used or whether it is slow, whether the index setting is unreasonable, whether the index is not displayed, whether it has touched the rule of index failure .

The way to use explian is to add explian in front of sql.

 Field analysis:

  1.  id: Query the serial number, because there may be multiple explain, it is used to mark the number of items
  2. select_type: query statement type:
    1. simple: common query
    2. union, union all: joint query
      1. derived: contains subqueries
      2. union result : join query results

    3. subquery: subquery
  3. table: the name of the table to query

  4. type: connection type

    1. system: only one row, or an empty table

    2. ​​​​​​​​const: unique index or primary key

    3. eq_ref: unique index scan

    4. ref: non-unique index scan

    5. range: index range scan

    6. index: the index is used

    7. all: full table scan

  5. possible_keys: Indexes that may be used in this query
  6. key: the index actually used by the query
  7. key_len: Displays the index size that MySQL decides to use
  8. ref: which field or constant is used with key
  9. rows: Displays the total number of rows scanned by this query. This is an estimated value, not an exact value.
  10. filtered: Indicates the percentage of data filtered by this query condition
  11. Extra: extra information
    1. ​​​​​​Using filesort: file sorting
    2. Using index: where the index is used after where, the Using index will appear; Using Where

    3. Using join buffer: connection cache

 

2. MySQL SQL optimization

1. Optimize the index

  • Remove the index if the number of table rows is small
  • The number of indexes on the table should not be too many. If there are too many indexes, it is best to remove the indexes that are not commonly used. The existence of indexes will occupy disk space. When updating data, not only the data needs to be maintained, but also the indexes need to be maintained.
  • Fields that are frequently updated should not be used as indexes, as frequent updates consume high performance
  • Those with low discrimination should not be used as indexes. If the discrimination is low, the number of queries will increase, and the performance consumption of returning to the table will increase.
  • Do not use unordered values ​​as indexes, and unordered data storage and arrangement are scattered
  • Use composite indexes as much as possible to save space
  • Need to beware of index invalidation rules, do not touch this rule, avoid full table scan

2. Add limit to the query statement to optimize sql. If there is only one query data, use limit to limit the full table scan

3. You can use the subquery without using it. You can use the connection instead of the subquery to use the connection query, because the subquery will create a temporary table, and the temporary table will be deleted after the query is completed. The process of creating and deleting is inevitable. It is bound to reduce the query time

4. If an associated query is required, the connected fields are finally indexed

 3. MySQL database optimization

Database optimization: enable slow query optimization log, configure number of connections, optimize database structure

Enable slow query optimization log

Enable the slow query optimization log, enable the log when there are slow queries, and then allocate the slow query log. It is not recommended to enable it in normal times. The log needs to be viewed under the user, not in mysql.

[root@localhost ~]# mysqldumpslow -s t /var/lib/mysql/localhost-slow.log 

Reading mysql slow query log from /var/lib/mysql/localhost-slow.log
Count: 1  Time=77.12s (77s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@[192.168.200.1]
  select tk.id,ts.* from  tb_seckill_goods ts LEFT JOIN tb_sku tk ON tk.id=ts.id where ts.id>N order by ts.price

Count: 1  Time=2.00s (2s)  Lock=0.00s (0s)  Rows=1.0 (1), root[root]@[192.168.200.1]
  select sleep(N)

 Configure the number of connections

Configuring the number of connections is the same as the pooling idea used by the connection pool, but it cannot be set too high. It should be reasonably configured according to the number of CPU cores and task processing time.

# 查看 max_connections
show global variables like '%max_connections%'
# 设置 max_connections(立即生效重启后失效)
set global max_connections = 800;

 Database structure optimization

  1. Tables with many fields, and table fields with low field usage frequency, are split into multiple tables and associated through primary keys
  2. Add intermediate tables, multiple tables that require frequent multi-table associations, you can create intermediate tables, and only need to query the intermediate table if you do not need to associate each time
  3. Reasonably increase redundant fields and standardize table design.

  4. MySQL hardware optimization

Improve hardware devices, such as choosing memory with the highest possible frequency (the frequency should not be higher than that supported by the motherboard), increasing network bandwidth , using SSD high-speed disks, and improving CPU performance , etc. CPU selection: cpu determines concurrency and processing power

 

Guess you like

Origin blog.csdn.net/qq_45656077/article/details/131101941