search
HomeDatabaseMysql TutorialMySQL慢查询分析_MySQL

bitsCN.com
MySQL慢查询分析 在我们做系统性能调优的时候,数据库的慢查询语句的优化是必不可少的,特别是电子商务类型的重度MYSQL应用类型。下面我们一起来看看怎么做好MYSQL的慢查询分析吧。1,开启MYSQL的慢查询日志   首先在my.cnf配置里面加入慢查询配置,然后建立慢查询的日志文件,并把用户和组修改为mysql,最后重启mysqld。 vim /etc/my .cnf# 在配置文件的[mysqld]下面加入以下几行log-slow-queries= /var/log/mysql-slow .loglong_query_time=0.01 #表示查询时间超过10ms的都认为是慢查询log-queries-not-using-indexes #表示没有使用索引的查询也记录日志 touch /var/log/mysql-slow .logchown mysql.mysql /var/log/mysql-slow .log/etc/init .d /mysqld restart接着测试一下慢查询是否生效,可以访问一下phpmyadmin或者跑一条select sleep(1),然后再cat一下/var/log/mysql-slow.log,如果看到有记录就表示设置成功了。不过,生成慢查询日志只是忠实的 记录了每一条慢查询,对于我们做分析并不方便。 2,安装mysqlsla慢查询分析工具wget http: //hackmysql .com /scripts/mysqlsla-2 .03. tar .gztar xzf mysqlsla-2.03. tar .gzcd mysqlsla-2.03     perl Makefile.PLmakemake install#安装信息#Installing /usr/local/share/perl5/mysqlsla.pm#Installing /usr/local/share/man/man3/mysqlsla.3pm#Installing /usr/local/bin/mysqlsla#Appending installation info to /usr/lib/perl5/perllocal.pod file /usr/local/bin/mysqlsla#其实是一个perl脚本#/usr/local/bin/mysqlsla: a /usr/bin/perl -w script text executable3,慢查询统计 #统计出现次数最多的前10条慢查询mysqlsla -lt slow /var/log/mysql-slow .log - top 10 - sort c_sum > top10_count_sum.log#统计执行时间的总和前10条慢查询mysqlsla -lt slow /var/log/mysql-slow .log - top 10 - sort t_sum > top10_time_sum.log#统计平均执行时间最长的前10条慢查询(常用)mysqlsla -lt slow /var/log/mysql-slow .log - top 10 - sort t_avg > top10_time_avg.log打开其中一个log统计文件,你会看到:   Report for slow logs: /var/log/mysql-slow.log 被分析的慢查询日志文件 40 queries total, 12 unique 40条查询;除了重复的,有12条查询 Sorted by ‘t_avg’ 按平均查询时间排序 Grand Totals: Time 4 s, Lock 0 s, Rows sent 236, Rows Examined 8.63k______________________________________________________________________ 001 ___Count : 1 (2.50%)这条SQL出现了1次,占SQL总数的2.5%Time : 588.994 ms total执行时间总和, 588.994 ms avg平均每次查询的时间, 588.994 ms最短时间 to 588.994 ms max最长时间 (13.78%)Lock Time (s) : 91 µs total, 91 µs avg, 91 µs to 91 µs max (2.34%)Rows sent : 30 avg, 30 to 30 max (12.71%)Rows examined : 899 avg, 899 to 899 max (10.41%)Database :Users :coreseektest@localhost : 100.00% (1) of query, 100.00% (40) of all users Query abstract:SET timestamp=N; SELECT * FROM ecm_goods WHERE goods_name LIKE ‘S’ ORDER BY ecm_goods.brand_id ASC LIMIT N, N; Query sample:SET timestamp=1341467496;SELECT * FROM `ecm_goods` WHERE goods_name like ‘冰箱’ ORDER BY `ecm_goods`.`brand_id` ASCLIMIT 0, 30;______________________________________________________________________ 002 ___Count : 2 (5.00%) 这条SQL出现了2次,占SQL总数的5%Time : 57.38 ms total 执行时间总和, 28.69 ms avg 平均每次查询的时间, 27.503 ms 最短时间 to 29.877 ms max 最长时间 (1.34%)Lock Time (s) : 134 µs total, 67 µs avg, 64 µs to 70 µs max (3.44%)Rows sent : 3 avg, 3 to 3 max (2.54%)Rows examined : 3 avg, 3 to 3 max (0.07%)Database :Users :    coreseektest@localhost : 100.00% (2) of query, 100.00% (40) of all usersQuery abstract:SET timestamp=N; SELECT * FROM documents LIMIT N, N;Query sample:SET timestamp=1341399487;SELECT * FROM `documents` LIMIT 0, 30; …其他省略… 如果需要做更复杂的统计,可以参考官方文档:http://hackmysql.com/mysqlsla_guide如果希望每隔一段时间,比如一天,出一次慢查询统计的话,可以写一个shell脚本,然后放到/etc/crontab里面。这样的话,就可以定期做查询优化。  作者 alex.wu bitsCN.com

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's licensing compare to other database systems?How does MySQL's licensing compare to other database systems?Apr 25, 2025 am 12:26 AM

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

When would you choose InnoDB over MyISAM, and vice versa?When would you choose InnoDB over MyISAM, and vice versa?Apr 25, 2025 am 12:22 AM

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

Explain the purpose of foreign keys in MySQL.Explain the purpose of foreign keys in MySQL.Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

What are the different types of indexes in MySQL?What are the different types of indexes in MySQL?Apr 25, 2025 am 12:12 AM

There are four main index types in MySQL: B-Tree index, hash index, full-text index and spatial index. 1.B-Tree index is suitable for range query, sorting and grouping, and is suitable for creation on the name column of the employees table. 2. Hash index is suitable for equivalent queries and is suitable for creation on the id column of the hash_table table of the MEMORY storage engine. 3. Full text index is used for text search, suitable for creation on the content column of the articles table. 4. Spatial index is used for geospatial query, suitable for creation on geom columns of locations table.

How do you create an index in MySQL?How do you create an index in MySQL?Apr 25, 2025 am 12:06 AM

TocreateanindexinMySQL,usetheCREATEINDEXstatement.1)Forasinglecolumn,use"CREATEINDEXidx_lastnameONemployees(lastname);"2)Foracompositeindex,use"CREATEINDEXidx_nameONemployees(lastname,firstname);"3)Forauniqueindex,use"CREATEU

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.

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software