search
HomeDatabaseMysql Tutorialmysql性能有关问题定位

mysql性能有关问题定位

Jun 07, 2016 pm 04:23 PM
mysqlpositionperformancequestion

mysql性能问题定位 ? 2 条评论 ? mysql性能问题定位 2013 年 8 月 28 日, by 徐旭 , 所属分类 数据库, 测试技术. ??使用mysql作为基础数据库的应用,可能会遇到一些数据库方面的性能问题,我们可以通过一些方法进行问题定位。以下介绍可以定位性能问题的四种

mysql性能问题定位

?

2 条评论 ?

mysql性能问题定位

2013 年 8 月 28 日, by 徐旭 , 所属分类 数据库, 测试技术.

??使用mysql作为基础数据库的应用,可能会遇到一些数据库方面的性能问题,我们可以通过一些方法进行问题定位。以下介绍可以定位性能问题的四种方法,欢迎拍砖。

一、开启慢查询日志:

记录执行查询时间大于long_query_time的sql,long_query_time默认为2s;

show variables like ‘%slow%’

?

?

得到图中所示信息,这里可以查看到慢查询日志是否开启,慢查询日志文件的存放目录。

开启慢查询日志的方法:

1、vi ?/etc/my.cnf(这个是mysql的默认读取配置文件目录,一般会将my.cnf文件放在这下面)

[mysqld]下添加

slow_query_log=ON

long_query_time=1(sql语句执行时间超过该参数值,则会打印在慢查询日志中),默认执行时间超过2s的sql会打印在慢查询日志中。

修改了my.cnf中的配置项,需要重启数据库。

2、不重启数据库的情况下,执行 set global slow_query_log=ON,可以开启慢查询日志。

开启慢查询日志后,跟踪慢查询日志文件中的慢查询sql,再具体分析,通过调整sql写法,或者添加正确的索引,可以看到意想不到的性能效果。

二、分析慢查询sql:

1、Explain 打印执行计划

执行的selcet语句前面加上explain,可以告诉你mysql如何执行该条语句。

?

这里需要额外注意type、key、rows 、extra列展示的内容。

其中,

Type=all,表示使用的是全表扫描,在数据量大的情况下,全表扫描是非常耗性能的,这个需要特别注意;

Type=index,表示使用索引扫描,只会遍历索引树;

Type=range,表示使用索引范围扫描,常见于between 、>、

Type=ref,非唯一性索引扫,返回匹配某个单独值得所有行

Type=eq_ref 唯一性索引扫描,对于每个索引键,表中只有一条记录与之匹配

Type=const/system???读常量,最多只会有一条记录匹配,由于是常量,实际上只须要读一次

Type=null 不需要扫描表

访问类型从上到下由差变为最好。

key表示select中使用到的索引,如果为null,表示没有使用索引,从查询效率上讲,使用索引比不使用索引快。但并不是所有的都要加索引,索引也存在不足,这里就不详解。

rows表示执行该条sql所需要扫描的行数,这个没有绝对值可参考,一般来说越小越好,如果100万数据量的数据库,rows是70万,通过这个可以判断sql的查询性能很差,如果100万条数据量的数据库,rows是1万,从我个人的角度,还是能接受的。

extra

一些十分重要的额外信息,重点关注出现关键字:

Using filesort:当Query 中包含order by 操作,而且无法利用索引完成排序操作的时候,MySQL Query Optimizer 不得不选择相应的排序算法来实现。

Using temporary:在某些操作中必须使用临时表时,在 Extra 信息中就会出现Using temporary ,主要常见于 GROUP BY 和 ORDER BY 等操作中

当 执行计划Extra 出现Using filesort 、Using temporary 时,可以考虑是否需要进行sql优化和调整索引,最后再调整my.cnf 中与排序或者临时表相关的参数,如sort_buffer_size或者tmp_table_size.

2、show full processlist 查看哪条sql一直占用进程

?

Time表示执行当前操作所耗费的时间,单位为秒(s); State表面当前线程的状态,Info表示正在执行的操作; 这里如果发现time值比较大,state一直处于一个状态,那么从Info中我们可以获得耗时长的操作,再具体分析; 注意观察State中出现关于lock关键字的状态。 这个命令可以很直观地看到正在执行的sql,及其当前状态,操作比较方便。

3、show profile 定位sql在数据库中资源占用情况(注意,一个是show profiles,一个是show profile)

Show profiles主要展示在当前会话中,profiling_history_size条sql执行的时间、query_id,默认为15条,最大为100条,不能设为0。 SHOW VARIABLES LIKE ‘%profiling_history_size%’

?

SHOW VARIABLES LIKE ‘%profiling’或者select @@profiling 查看profiling是否开启

?

?

set profiling=1 开启profiling

执行一条sql

?

?

?

?

再执行show profiles,会把最近执行的sql给展示出来,

?

?

从图中找到刚刚执行的sql,query_id是120,执行时间是0.00079725s

如果我们想看sql在各个阶段所消耗时间,则使用如下

SHOW PROFILE FOR QUERY 120

?

?

?

?

?

?

?

?

各个阶段所消耗时间一目了然。

show profile具体写法为 Show profile TYPE for query n,n为query_id,TYPE可写可不写

TYPE的取值有为ALL、BLOCK IO、CONTEXT SWITCHES、CPU、IPC、MEMORY、PAGE FAULTS、SOURCE、SWAPS

如上述例子中

SHOW PROFILE CPU,MEMORY FOR QUERY 120

?

?

?

?

?

不加for query n这句,则展示执行show profile之前执行过一条语句。

4、Mysqladmin 查询整个数据库的状态

在mysql的bin目录下,执行

./mysqladmin -u用户名 -p密码 proc stat

?

?

这里添加proc就如同show full processlist功效

stat展示当前数据库的状态

threads 表示当前线程数,Opens 当前打开的表数目,Queries per second 每秒执行的查询数,数据库性能越好,这个值就越高

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL differ from SQLite?How does MySQL differ from SQLite?Apr 24, 2025 am 12:12 AM

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

What are indexes in MySQL, and how do they improve performance?What are indexes in MySQL, and how do they improve performance?Apr 24, 2025 am 12:09 AM

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Explain how to use transactions in MySQL to ensure data consistency.Explain how to use transactions in MySQL to ensure data consistency.Apr 24, 2025 am 12:09 AM

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.

In what scenarios might you choose PostgreSQL over MySQL?In what scenarios might you choose PostgreSQL over MySQL?Apr 24, 2025 am 12:07 AM

Scenarios where PostgreSQL is chosen instead of MySQL include: 1) complex queries and advanced SQL functions, 2) strict data integrity and ACID compliance, 3) advanced spatial functions are required, and 4) high performance is required when processing large data sets. PostgreSQL performs well in these aspects and is suitable for projects that require complex data processing and high data integrity.

How can you secure a MySQL database?How can you secure a MySQL database?Apr 24, 2025 am 12:04 AM

The security of MySQL database can be achieved through the following measures: 1. User permission management: Strictly control access rights through CREATEUSER and GRANT commands. 2. Encrypted transmission: Configure SSL/TLS to ensure data transmission security. 3. Database backup and recovery: Use mysqldump or mysqlpump to regularly backup data. 4. Advanced security policy: Use a firewall to restrict access and enable audit logging operations. 5. Performance optimization and best practices: Take into account both safety and performance through indexing and query optimization and regular maintenance.

What are some tools you can use to monitor MySQL performance?What are some tools you can use to monitor MySQL performance?Apr 23, 2025 am 12:21 AM

How to effectively monitor MySQL performance? Use tools such as mysqladmin, SHOWGLOBALSTATUS, PerconaMonitoring and Management (PMM), and MySQL EnterpriseMonitor. 1. Use mysqladmin to view the number of connections. 2. Use SHOWGLOBALSTATUS to view the query number. 3.PMM provides detailed performance data and graphical interface. 4.MySQLEnterpriseMonitor provides rich monitoring functions and alarm mechanisms.

How does MySQL differ from SQL Server?How does MySQL differ from SQL Server?Apr 23, 2025 am 12:20 AM

The difference between MySQL and SQLServer is: 1) MySQL is open source and suitable for web and embedded systems, 2) SQLServer is a commercial product of Microsoft and is suitable for enterprise-level applications. There are significant differences between the two in storage engine, performance optimization and application scenarios. When choosing, you need to consider project size and future scalability.

In what scenarios might you choose SQL Server over MySQL?In what scenarios might you choose SQL Server over MySQL?Apr 23, 2025 am 12:20 AM

In enterprise-level application scenarios that require high availability, advanced security and good integration, SQLServer should be chosen instead of MySQL. 1) SQLServer provides enterprise-level features such as high availability and advanced security. 2) It is closely integrated with Microsoft ecosystems such as VisualStudio and PowerBI. 3) SQLServer performs excellent in performance optimization and supports memory-optimized tables and column storage indexes.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools