The following editor will bring you a summary of MysqlSlowQuery operations. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look
Mysql slow query explanation
MySQL’s slow query log is a type provided by MySQL Logging, which is used to record statements whose response time exceeds the threshold in MySQL, specifically refers to SQL whose running time exceeds the long_query_time value, which will be recorded in the slow query log. The default value of long_query_time is 10, which means running statements for more than 10S. By default, Mysql database does not start the slow query log. We need to manually set this parameter. Of course, if it is not needed for tuning, it is generally not recommended to start this parameter, because turning on the slow query log will cause or More or less will have a certain performance impact. The slow query log supports writing log records to files and also supports writing log records to database tables; the slow query log is used to record some query statements that are too slow, which can help administrators analyze the problem. The log defaults to no To enable it, you need to add a series of parameters to the configuration file to start it manually
Reason for turning on Mysql slow query
The database is a place where bottlenecks can easily occur. Nowadays, Nosql is being discussed so much that everyone is probably depressed by the database. What most affects the speed of MySQL are those very slow query statements. These slow statements may be written unreasonably or are joint queries of multiple tables under big data, etc., so we need to find out these statement, analyze the reasons, and optimize them. This is also the reason for posting this blog post
Enable mysql slow query method
1) Method 1 , Log in to the mysql database terminal and open
mysql> show variables like "%long%"; <SPAN style="COLOR: #ff00ff"> //查看一下默认为慢查询的时间10秒 </SPAN> +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.000000 | +-----------------+-----------+ 1 row in set (0.00 sec) mysql> set global long_query_time=1; <SPAN style="COLOR: #ff00ff">//设置成1秒,加上global,下次进mysql已然生效</SPAN> Query OK, 0 rows affected (0.00 sec) mysql> show variables like "%slow%"; <SPAN style="COLOR: #ff00ff"> //查看一下慢查询是不是已经开启 </SPAN> +---------------------+---------------------------------+ | Variable_name | Value | +---------------------+---------------------------------+ | log_slow_queries | OFF | | slow_launch_time | 2 | | slow_query_log | OFF | | slow_query_log_file | /usr/local/mysql/mysql-slow.log | +---------------------+---------------------------------+ 4 rows in set (0.00 sec) mysql> set slow_query_log='ON'; <SPAN style="COLOR: #ff00ff"> //加上global,不然会报错的</SPAN> ERROR 1229 (HY000): Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL mysql> set global slow_query_log='ON'; <SPAN style="COLOR: #ff00ff">//这个参数设置为ON,表示启用慢查询,可以捕获执行时间超过一定数值的SQL语句。</SPAN> Query OK, 0 rows affected (0.28 sec) mysql> show variables like "%slow%"; <SPAN style="COLOR: #ff00ff">//查看是否已经开启 </SPAN> +---------------------+---------------------------------+ | Variable_name | Value | +---------------------+---------------------------------+ | log_slow_queries | ON | | slow_launch_time | 2 | | slow_query_log | ON | | slow_query_log_file | /usr/local/mysql/mysql-slow.log | +---------------------+---------------------------------+ 4 rows in set (0.00 sec)
Method 2. Modify the mysql configuration file my.cnf
[root@www ~]# vim /etc/mysql.cnf
Add the following content in the [mysqld] area configuration
......
slow_query_log = 1 //开启慢查询日志,将1改为ON也可以 long_query_time = 1 //mysql慢查询时间,指定达到多少秒才算慢查询;这里推荐设置超过1秒的执行语句就会被记录到慢查询日志中 slow_query_log_file= /var/lib/mysql/mysql-slow.log //慢查询日志路径。这里slow_query_log_file修改为log-slow-queries 也可以 //主要是上面三行内容,还可以添加下面几行细节性配置 long-queries-not-using-indexes //记录没有使用索引的查询语句 min_examined_row_limit = 1000 //记录查找了多达1000次而引起的慢查询 log-slow-admin-statements //记录ALTER TABLE等语句引发的慢查询 log-slow-slave-statements //记录从服务器产生的慢查询
Analysis Tool
The analysis tool is actually to analyze the data recorded in mysql-slow.log. It will be displayed at once (in fact, you can also get the required information out by writing a shell script yourself).
# [root@www ~]#Cat mysql-slow.log // View command
/usr/local/mysql/libexec/mysqld, Version: 5.1.26-rc-log (Source distribution). started with: Tcp port: 3306 Unix socket: /tmp/mysql.sock Time Id Command Argument # Time: 100814 13:28:30 # User@Host: root[root] @ localhost [] # Query_time: 10.096500 Lock_time: 0.045791 Rows_sent: 1 Rows_examined: 2374192 SET timestamp=1281763710; select count(distinct ad_code) as x from ad_visit_history where ad_code in (select ad_code from ad_list where media_id=15); # Time: 100814 13:37:02 # User@Host: root[root] @ localhost [] # Query_time: 10.394134 Lock_time: 0.000091 Rows_sent: 1 Rows_examined: 2374192 SET timestamp=1281764222; select count(distinct ad_code) as x from ad_visit_history where ad_code in (select ad_code from ad_list where media_id=15); # Time: 100814 13:37:16 # User@Host: root[root] @ localhost [] # Query_time: 4.608920 Lock_time: 0.000078 Rows_sent: 1 Rows_examined: 1260544 SET timestamp=1281764236; select count(*) as cou from ad_visit_history where ad_code in (select ad_code from ad_list where id=41) order by id desc;
Example:
Option h requires an argument ERROR: bad option Usage: mysqldumpslow [ OPTS... ] [ LOGS... ] Parse and summarize the MySQL slow query log. Options are --verbose verbose --debug debug --help write this text to standard output -v verbose -d debug //查错 -s ORDER what to sort by (t, at, l, al, r, ar etc), 'at' is default //排序方式query次数,时间,lock的时间和返回的记录数来排序 -r reverse the sort order (largest last instead of first) //倒排序 -t NUM just show the top n queries //显示前N多个 -a don't abstract all numbers to N and strings to 'S' -n NUM abstract numbers with at least n digits within names //抽象的数字,至 少有n位内的名称 -g PATTERN grep: only consider stmts that include this string //配置模式 -h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard), //mysql所以机器名或者IP default is '*', i.e. match all -i NAME name of server instance (if using mysql.server startup script) -l don't subtract lock time from total time //总时间中不减去锁定时间
The above is the detailed content of Code summary of Mysql slow query operation. For more information, please follow other related articles on the PHP Chinese website!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

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

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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.

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),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment