<转>深入mysql慢查询设立的详解
转深入mysql慢查询设置的详解 原链接:http://www.jb51.net/article/38274.htm 在web开发中,我们经常会写出一些SQL语句,一条糟糕的SQL语句可能让你的整个程序都非常慢,超过10秒一般用户就会选择关闭网页,如何优化SQL语句将那些运行时间 比较长的SQL语句找
深入mysql慢查询设置的详解原链接:http://www.jb51.net/article/38274.htm
在web开发中,我们经常会写出一些SQL语句,一条糟糕的SQL语句可能让你的整个程序都非常慢,超过10秒一般用户就会选择关闭网页,如何优化SQL语句将那些运行时间 比较长的SQL语句找出呢?MySQL给我们提供了一个很好的功能,那就是慢查询!所谓的慢查询就是通过设置来记录超过一定时间的SQL语句!那么如何应用慢查询呢?
1.开启MySQL的慢查询日志功能
默认情况下,MySQL是不会记录超过一定执行时间的SQL语句的。要开启这个功能,我们需要修改MySQL的配置文件,windows下修改my.ini,Linux下修改my.cnf文件,在[mysqld]最后增加如下命令:
复制代码 代码如下:
slow_query_log
long_query_time = 1
2.测试慢查询日志功能
(1)进入MySql控制台,执行如下语句:
复制代码 代码如下:
select sleep(2);
mysql> select sleep(2);
+----------+
| sleep(2) |
+----------+
| 0 |
+----------+
1 row in set (2.12 sec)
(2)查看慢查询日志文件think-slow.log,在文件最后发现:
复制代码 代码如下:
# Time: 121120 20:06:23
# User@Host: root[root] @ localhost [127.0.0.1]
# Query_time: 2.104120 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1353413183;
select sleep(2);
3.解释:
(1)slow_query_log 这句是开启记录慢查询功能,slow_query_log=0关闭;slow_query_log=1开启(这个1可以不写)
(2)long_query_time = 1 这句是记录超过1秒的SQL执行语句
(3)那么这个日志文件存放在什么地方呢?
默认是放在mysql的data目录,并且文件名为host_name-slow.log即 主机名-slow.log,比如在笔者的开发机上就是THINK-slow.log(因为偶用的Thinkpad,呵呵)
(4)如果日志文件不想放在data目录,我们可以通过如下配置指定存放的目录及日志文件名:
slow_query_log_file=file_name
其中file_name就是你的存放日志的目录和文件名,在这里注意有的资料上可能是log-slow-queries=file_name,这个在mysql5.5版已经过时!
4.如何记录低于1s的慢查询记录呢?
MySQL5.21版以前long_query_time 参数的单位是秒,默认值是10。这相当于说最低只能记录执行时间超过 1 秒的查询,怎么记录查询时间超过100毫秒的SQL语句记录呢?在mysql5.21+后版本支持毫秒记录
(1)进入MySql控制台,运行如下sql语句:
复制代码 代码如下:
set global long_query_time=0.1
该句是设置记录慢查询超过时间100ms的SQL,记住要重启mysql才能生效!
(2)测试
进入mysql控制台,执行如下sql语句:
复制代码 代码如下:
select sleep(0.5);
查看慢查询日志文件,我们看到最后添加的新信息:
复制代码 代码如下:
# Time: 121120 20:42:06
# User@Host: root[root] @ localhost [127.0.0.1]
# Query_time: 0.500028 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1353415326;
select sleep(0.5);

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

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
