Examine these my.cnf settings:
[mysqld]
Slow_query_log = ON
slow_query_log_file=/data/slow.Log
long_query_time=2
Examine this entry from /data/slow.log
# User@Host : admin[admin] @ [127.0.0.1] Id: 91420
# Query_time: 0.001668 Lock_time: 0.000075 Rows_sent: 1 Rows_examined: 3
SET timestamp=1452078485;
SELECT count (*) FROM Subscriber sb LEFT JOIN Common cm ON sb. Abr_id=cm. Id WHERE sb. Id=127183 AND sb. Deletion_time='1970-01-01';
Which option is also set in my.cnf?
A. log_queries_not_using_indexes
B. log_slow_admin_statements=1
C. log_queries_not_using_indexes=ON
D. log_throttle_queries_not_using_indexes=100
正确答案:C) log_queries_not_using_indexes=ON
-
矛盾现象:
- 配置要求:
long_query_time=2(仅记录执行超过2秒的查询) - 日志记录:查询实际耗时仅 0.001668秒(远低于阈值)
- ❓ 为什么会被记录?
- 配置要求:
-
唯一解释:
- 启用了
log_queries_not_using_indexes=ON - 该配置强制记录未使用索引的查询(即使执行时间很短)
- 启用了
-
索引使用验证:
EXPLAIN SELECT count(*) FROM Subscriber sb LEFT JOIN Common cm ON sb.Abr_id=cm.Id WHERE sb.Id=127183 AND sb.Deletion_time='1970-01-01';- 若输出
type: ALL或key: NULL→ 确认全表扫描未用索引
- 若输出
❌ 错误选项排除:
| 选项 | 错误原因 | 实际效果 |
|---|---|---|
| A | 语法不完整(缺少 =ON) |
配置无效 |
| B | 仅记录管理语句(如ALTER) |
不适用SELECT |
| D | 仅限制无索引查询的记录频率 | 不改变记录条件 |
配置验证方法:
SHOW GLOBAL VARIABLES LIKE 'log_queries_not_using_indexes';
-- 输出:ON
生产建议:
[mysqld]
log_queries_not_using_indexes=ON # 捕获无索引查询
log_throttle_queries_not_using_indexes=100 # 防日志爆炸(选项D的合理用法)
⚠️ 性能警报:
该查询扫描3行返回1行(Rows_examined: 3, Rows_sent: 1),虽当前性能影响小,但在大表上可能成为瓶颈:-- 优化建议:为 sb.Id 和 sb.Abr_id 添加索引 CREATE INDEX idx_subscriber_id ON Subscriber(Id); CREATE INDEX idx_subscriber_abr_id ON Subscriber(Abr_id);
结论:日志中短时查询被记录的唯一合理解释是启用了 log_queries_not_using_indexes=ON(选项C)。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




