《MysqL实例MysqL慢查询查找和调优测试》要点:
本文介绍了MysqL实例MysqL慢查询查找和调优测试,希望对您有用。如果有疑问,可以联系我们。
代码如下:
log_slow_querIEs = /var/log/MysqL/MysqL-slow.log
long_query_time = 2
log-querIEs-not-using-indexes
这将使得慢查询和没有使用索引的查询被记录下来.
这样做之后,对MysqL-slow.log文件执行tail -f命令,将能看到其中记录的慢查询和未使用索引的查询.
随便提取一个慢查询,执行explain:
代码如下:
explain low_query
你将看到下面的结果:
+----+-------------+---------------------+------+---------------+------+---------+------+------+-------------+
| ID | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | some_table | ALL | NulL | NulL | NulL | NulL | 166 | Using where |
+----+-------------+---------------------+------+---------------+------+---------+------+------+-------------+
注意上面的rows和key列.rows显示该查询影响了多少行记录,我们不想让这个值太大.key显示用了哪个索引,为NulL时表示查询未用任何索引.
如果想让查询更快,你或许需要为某些列增加索引:
CREATE INDEX myapp_mytable_myfIEld_IDx on myapp_mytable(myfIEld);
除了配置MysqL配置文件来实现记录慢查询外,还有下面的方法可以记录慢查询:
代码如下:
SELECT t.table_SCHEMA AS `db`,
t.table_name AS `table`,
s.INDEX_name AS `index name`,
s.ColUMN_name AS `FIELD name`,
s.SEQ_IN_INDEX `seq IN index`,
s2.max_columns AS `# cols`,
s.CARDINAliTY AS `card`,
t.table_ROWS AS `est rows`,
ROUND(((s.CARDINAliTY / IFNulL(t.table_ROWS,0.01)) * 100),2) AS `sel %`
FROM informatION_SCHEMA.STATISTICS s
INNER JOIN informatION_SCHEMA.tableS t ON s.table_SCHEMA = t.table_SCHEMA AND s.table_name = t.table_name
INNER JOIN (
SELECT table_SCHEMA,table_name,INDEX_name,MAX(SEQ_IN_INDEX) AS max_columns
FROM informatION_SCHEMA.STATISTICS
WHERE table_SCHEMA != 'MysqL' GROUP BY table_SCHEMA,INDEX_name ) AS s2 ON s.table_SCHEMA = s2.table_SCHEMA AND s.table_name = s2.table_name AND s.INDEX_name = s2.INDEX_name
WHERE t.table_SCHEMA != 'MysqL' /* Filter out the MysqL system DB */
AND t.table_ROWS > 10 /* Only tables with some rows */
AND s.CARDINAliTY IS NOT NulL /* Need at least one non-NulL value in the fIEld */
AND (s.CARDINAliTY / IFNulL(t.table_ROWS,0.01)) < 1.00 /* unique indexes are perfect anyway */
ORDER BY `sel %`,s.table_SCHEMA,s.table_name /* DESC for best non-unique indexes */
liMIT 10;
《MysqL实例MysqL慢查询查找和调优测试》是否对您有启发,欢迎查看更多与《MysqL实例MysqL慢查询查找和调优测试》相关教程,学精学透。内存溢出PHP学院为您提供精彩教程。
总结以上是内存溢出为你收集整理的Mysql实例MySQL慢查询查找和调优测试全部内容,希望文章能够帮你解决Mysql实例MySQL慢查询查找和调优测试所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)