我运行一个大型论坛,该论坛维护用于后端数据存储的 MySQL 数据库。“会话”表跟踪登录的用户和来宾。目前大约有 10 万条记录,所以不是那么大。但是,当我们修剪旧记录时,这个会话表会出现在慢查询日志中:
# Time: 120719 10:05:11
# User@Host: xxx[xxx] @ [10.x.x.x]
# Thread_id: 369051896 Schema: forumdb Last_errno: 0 Killed: 0
# Query_time: 8.352811 Lock_time: 0.000028 Rows_sent: 0 Rows_examined: 19635 Rows_affected: 19635 Rows_read: 0
# Bytes_sent: 13 Tmp_tables: 0 Tmp_disk_tables: 0 Tmp_table_sizes: 0
SET timestamp=1342710311;
DELETE FROM session
WHERE lastactivity < 1342709401;
Run Code Online (Sandbox Code Playgroud)
我已经确认 lastactivity 表上有一个 BTREE 索引:
mysql> SHOW INDEX FROM session FROM forumdb;
+---------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | …Run Code Online (Sandbox Code Playgroud)