用MySQL Slow Log解决MySQL CPU占用高的问题_MySQL
bitsCN.com
用MySQL Slow Log解决MySQL CPU占用高的问题
在Linux VPS系统上有时候会发现MySQL占用CPU高,导致系统的负载比较高。这种情况很可能是某个SQL语句执行的时间太长导致的。优化一下这个SQL语句或者优化一下这个SQL引用的某个表的索引一般能解决问题。
但是怎么找到是哪个SQL语句的执行时间过长呢?可以通过MySQL Slow Log来找,详解如下。
首先找到MySQL的配置文件my.cnf,根据不同版本的mysql开启慢查询的配置也不一样
mysql 5.0
[mysqld]
long_query_time = 1
log-slow-queries = /var/log/mysql/slow.log
mysql 5.1
[mysqld]
long_query_time = 1
slow_query_log=1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time 是指执行超过多久的sql会被log下来,这里是1秒。
log-slow-queries和slow_query_log_file 设置把日志写在哪里
把上述参数打开,运行一段时间,就可以关掉了,省得影响生产环境
接下来就是分析了,我这里的文件名字叫 /var/log/mysql/slow.log。
先mysqldumpslow –help下,主要用的是
-s ORDER what to sort by (t, at, l, al, r, ar etc), ‘at’ is default
-t NUM just show the top n queries
-g PATTERN grep: only consider stmts that include this string
-s,是order的顺序,说明写的不够详细,主要有
c,t,l,r和ac,at,al,ar,分别是按照query次数,时间,lock的时间和返回的记录数来排序,前面加了a的时倒序
-t,是top n的意思,即为返回前面多少条的数据
-g,后边可以写一个正则匹配模式,大小写不敏感的
mysqldumpslow -s c -t 20 /var/log/mysql/slow.log
mysqldumpslow -s r -t 20 /var/log/mysql/slow.log
上述命令可以看出访问次数最多的20个sql语句和返回记录集最多的20个sql。
mysqldumpslow -t 10 -s t -g “left join” /var/log/mysql/slow.log
这个是按照时间返回前10条里面含有左连接的sql语句。
用了这个工具就可以查询出来那些sql语句是性能的瓶颈,进行优化,比如加索引,该应用的实现方式等。
bitsCN.com
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.
