时间:2021-07-01 10:21:17 帮助过:10人阅读
我们来执行一个超过3秒的查询,如下: Java代码 mysql> select * from wei where text='orange'; +---------+--------+ | id | text | +---------+--------+ | 4103519 | orange | +---------+--------+ 1 row in set (3.79 sec) 再执行一个超过3秒的和一个没有超过3秒的: Java代码 mysql> select * from wei where text='xishizhaohua'; Empty set (3.82 sec) Java代码 [mysql> select * from wei where id=4564; +------+--------------------+ | id | text | +------+--------------------+ | 4564 | yyyyyyyyyyyyyyyyyy | +------+--------------------+ 1 row in set (0.02 sec) 可以通过下面的命令查看现在这个session有多少个慢查询:
现在我们可以查看mysql_slow.log(win7默认在C:/ProgramData/MySQL/MySQL Server 5.1/data下面),里边内容如下,内容比较明了,包括查询花费的语句及时间,还包括查询时的时间戳等信息,其中Rows_examined为检查的行数,对我们优化也很有帮助 # Time: 121017 17:38:54 # User@Host: root[root] @ localhost [127.0.0.1] # Query_time: 3.794217 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 4194304 SET timestamp=1350466734; select * from wei where text='orange'; # Time: 121017 17:46:22 # User@Host: root[root] @ localhost [127.0.0.1] # Query_time: 3.819219 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 4194304 SET timestamp=1350467182; select * from wei where text='xishizhaohua'; 3.优化 其实定位到了慢查询语句就已经完成了一大不了,执行explain或者desc命令查看慢查询语句,如下图:
问题很明显,解决方式也很明显,建索引了。 Java代码 mysql> create index text_index on wei(text); Query OK, 4194304 rows affected (1 min 58.07 sec) Records: 4194304 Duplicates: 0 Warnings: 0 然后在执行查询操作,用时明显少了很多。 Java代码 mysql> select * from wei where text='orange'; +---------+--------+ | id | text | +---------+--------+ | 4103519 | orange | +---------+--------+ 1 row in set (0.33 sec) Java代码 mysql> select * from wei where text='xishizhaohua'; Empty set (0.01 sec) bitsCN.com