MySQL 查询数据库响应时长详解
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="//q9.itc.cn/images01/20240620/5d73459379534a259fa797d504dd1b94.png" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前言</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">做为</span>一名测试工程师,在性能测试中,<span style="color: black;">查找</span>数据库的响应时长是一个<span style="color: black;">要紧</span>指标。MySQL <span style="color: black;">供给</span>了多种<span style="color: black;">办法</span>来监控和优化<span style="color: black;">查找</span>性能。本文将<span style="color: black;">仔细</span>介绍<span style="color: black;">怎样</span><span style="color: black;">运用</span> MySQL 的内置功能和<span style="color: black;">工具</span>来<span style="color: black;">查找</span>数据库响应时长,并分享<span style="color: black;">有些</span>性能优化的技巧。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">启用<span style="color: black;">查找</span>日志</p>开启慢<span style="color: black;">查找</span>日志慢<span style="color: black;">查找</span>日志用于记录执行时间超过指定阈值的 SQL 查询,默认<span style="color: black;">状况</span>下是关闭的。在 MySQL 配置文件(<span style="color: black;">一般</span>是 my.cnf 或 my.ini)中添加以下配置:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">slow_query_log = 1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">slow_query_log_file = /var/log/mysql/slow.log</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">long_query_time = 1</p>slow_query_log: 启用慢<span style="color: black;">查找</span>日志。slow_query_log_file: 指定慢<span style="color: black;">查找</span>日志文件的路径。long_query_time: 设置记录慢<span style="color: black;">查找</span>的阈值(单位:秒),例如设置为 1 秒。重启 MySQL 服务以应用配置更改:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sudo service mysql restart</p>查看慢<span style="color: black;">查找</span>日志<span style="color: black;">运用</span>命令行查看慢<span style="color: black;">查找</span>日志:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">cat /var/log/mysql/slow.log</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span> EXPLAIN 分析<span style="color: black;">查找</span></p>基本用法EXPLAIN 命令用于分析 SQL <span style="color: black;">查找</span>的执行计划,<span style="color: black;">帮忙</span>优化<span style="color: black;">查找</span>性能。示例:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EXPLAIN SELECT * FROM users WHERE age > 30;</p>结果<span style="color: black;">包括</span>以下字段:id: <span style="color: black;">查找</span>的<span style="color: black;">独一</span>标识符。select_type: <span style="color: black;">查找</span>的类型(如 SIMPLE、PRIMARY、UNION 等)。table: 表的名<span style="color: black;">叫作</span>。type: <span style="color: black;">拜访</span>类型(如 ALL、index、range 等)。possible_keys: 可能<span style="color: black;">运用</span>的索引。key: <span style="color: black;">实质</span><span style="color: black;">运用</span>的索引。rows: 扫描的行数。Extra: 额外的信息,如<span style="color: black;">是不是</span><span style="color: black;">运用</span>文件排序或临时表。优化<span style="color: black;">意见</span><span style="color: black;">运用</span>索引优化<span style="color: black;">查找</span>。避免<span style="color: black;">运用</span> SELECT *,只<span style="color: black;">查找</span><span style="color: black;">必须</span>的列。<span style="color: black;">尽可能</span>减少子<span style="color: black;">查找</span>,改用 JOIN。<span style="color: black;">运用</span>适当的分区和分片。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span> SHOW PROFILE</p>启用和<span style="color: black;">运用</span>SHOW PROFILE 命令用于<span style="color: black;">仔细</span><span style="color: black;">表示</span><span style="color: black;">查找</span>的执行时间。先启用 profiling:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SET profiling = 1;</p>执行<span style="color: black;">查找</span>:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT * FROM users WHERE age > 30;</p>查看<span style="color: black;">查找</span>的 profiling 信息:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SHOW PROFILES;</p>获取<span style="color: black;">详细</span>的执行<span style="color: black;">过程</span>和时间:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SHOW PROFILE FOR QUERY 1;</p>解释结果结果<span style="color: black;">包括</span><span style="color: black;">查找</span>的各个<span style="color: black;">周期</span>及其耗时,如:Starting: 初始化<span style="color: black;">周期</span>。Opening tables: 打开表。System lock: 系统锁。Table lock: 表锁。init: 初始化。optimizing: <span style="color: black;">查找</span>优化。statistics: 统计信息。preparing: <span style="color: black;">查找</span>准备。executing: <span style="color: black;">查找</span>执行。Sending data: 发送数据。end: <span style="color: black;">查找</span>结束。query end: <span style="color: black;">查找</span>结束处理。closing tables: 关闭表。freeing items: 释放内存。cleaning up: 清理。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span> Performance Schema</p>启用 Performance SchemaPerformance Schema 是一个内置的<span style="color: black;">工具</span>,用于监控 MySQL 服务器的运行<span style="color: black;">状况</span>。在 MySQL 配置文件中添加以下配置:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">performance_schema = ON</p>重启 MySQL 服务以应用配置更改:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sudo service mysql restart</p><span style="color: black;">查找</span> Performance SchemaPerformance Schema <span style="color: black;">供给</span>了一系列表格,用于监控和分析性能。示例:<span style="color: black;">查找</span>耗时最长的 10 个 SQL 语句:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT sql_text, SUM_TIMER_WAIT/1000000 AS total_time_ms</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM performance_schema.events_statements_summary_by_digest</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ORDER BY SUM_TIMER_WAIT DESC</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LIMIT 10;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span><span style="color: black;">外边</span><span style="color: black;">工具</span></p>MySQL WorkbenchMySQL Workbench 是一款官方<span style="color: black;">供给</span>的 GUI <span style="color: black;">工具</span>,<span style="color: black;">供给</span>了丰富的性能监控和分析功能。<span style="color: black;">能够</span><span style="color: black;">运用</span> MySQL Workbench 的<span style="color: black;">查找</span>性能分析功能,查看<span style="color: black;">查找</span>执行计划和优化<span style="color: black;">意见</span>。第三方<span style="color: black;">工具</span>Percona Toolkit:Percona <span style="color: black;">供给</span>的一组命令行<span style="color: black;">工具</span>,用于管理和优化 MySQL。Monyog:<span style="color: black;">商场</span>化的 MySQL 性能监控<span style="color: black;">工具</span>。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">性能优化技巧</p>索引优化创建合适的索引,避免全表扫描。<span style="color: black;">运用</span>复合索引<span style="color: black;">加强</span>多列<span style="color: black;">查找</span>性能。<span style="color: black;">查找</span>优化避免<span style="color: black;">运用</span> SELECT *,只<span style="color: black;">查找</span>必要的列。<span style="color: black;">运用</span> JOIN 替代子<span style="color: black;">查找</span>,减少<span style="color: black;">查找</span><span style="color: black;">繁杂</span>度。<span style="color: black;">定时</span>分析和优化表结构。数据库配置<span style="color: black;">调节</span> MySQL 缓存设置,如 query_cache_size 和 innodb_buffer_pool_size。<span style="color: black;">定时</span>监控和<span style="color: black;">调节</span> MySQL 配置,以适应<span style="color: black;">实质</span>负载。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">总结</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>本文的<span style="color: black;">仔细</span>介绍,相信您<span style="color: black;">已然</span><span style="color: black;">把握</span>了<span style="color: black;">查找</span> MySQL 数据库响应时长的<span style="color: black;">办法</span>和技巧。<span style="color: black;">恰当</span>利用这些<span style="color: black;">工具</span>和<span style="color: black;">办法</span>,<span style="color: black;">能够</span><span style="color: black;">明显</span><span style="color: black;">加强</span>数据库<span style="color: black;">查找</span>性能。<span style="color: black;">倘若</span>在<span style="color: black;">运用</span>过程中遇到问题,<span style="color: black;">能够</span>参考 MySQL 官方文档或社区获取<span style="color: black;">更加多</span><span style="color: black;">帮忙</span>。<a style="color: black;"><span style="color: black;">返回<span style="color: black;">外链论坛:www.fok120.com</span>,查看<span style="color: black;">更加多</span></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">责任编辑:网友投稿</span></p>
太棒了、厉害、为你打call、点赞、非常精彩等。 你的话语如春风拂面,让我心生暖意。
页:
[1]