当前位置:网站主页>网站技术资料>mysql>文章内容

mysql常用命令七(MySQL的优化方法)

SELECT * from table_name WHERE key_part1 = # ORDER BY key2

二十四、学会使用EXPLAIN

对于每一条你认为太慢的查询使用EXPLAIN!
mysql> explain select t3.DateOfAction, t1.TransactionID
-> from t1 join t2 join t3
-> where t2.ID = t1.TransactionID and t3.ID = t2.GroupID
-> order by t3.DateOfAction, t1.TransactionID;
+-------+--------+---------------+---------+---------+------------------+------+---------------------------------+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+-------+--------+---------------+---------+---------+------------------+------+---------------------------------+
| t1 | ALL | NULL | NULL | NULL | NULL | 11 | Using temporary; Using filesort |
| t2 | ref | ID | ID | 4 | t1.TransactionID | 13 | |
| t3 | eq_ref | PRIMARY | PRIMARY | 4 | t2.GroupID | 1 | |
+-------+--------+---------------+---------+---------+------------------+------+---------------------------------+

ALL和范围类型提示一个潜在的问题。

二十五、学会使用SHOW PROCESSLIST
使用SHOW processlist来发现正在做什么:
+----+-------+-----------+----+---------+------+--------------+-------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------+-----------+----+---------+------+--------------+-------------------------------------+
| 6 | monty | localhost | bp | Query | 15 | Sending data | select * from station,station as s1 |
| 8 | monty | localhost | | Query | 0 | | show processlist |
+----+-------+-----------+----+---------+------+--------------+-------------------------------------+
在mysql或mysqladmin中用KILL来杀死溜掉的线程。

二十六、如何知晓MySQL解决一条查询
运行项列命令并试图弄明白其输出:
SHOW VARIABLES;
SHOW COLUMNS FROM ...G
EXPLAIN SELECT ...G
FLUSH STATUS;
SELECT ...;
SHOW STATUS;

  • 快速服务服务:网站策划 | 网站建设 | 北京网站建设 | 网站推广 | 网罗SEO
  • 联系网罗: 咨询电话:010-89630319 Mail:admin#wonlu.com Msn:刘先生 QQ:26402294
  •   2006-2008 Wonlu.com 北京网罗无限-京ICP备06036090号 网罗SEO