在我的慢查询日志中,我每小时大约会收到 5 次这些:
# Query_time: 11.420629 Lock_time: 0.000033 Rows_sent: 0 Rows_examined: 0
SET timestamp=1267487708;
INSERT INTO record_lock (record_lock.module_id, record_lock.module_record_id, record_lock.site_id, record_lock.user_id, record_lock.expiration_date_time, record_lock.date_time_created) VALUES ('40', '12581', '940', '155254', '2010-03-02 00:24:57', '2010-03-01 23:54:57');
# Query_time: 2.095374 Lock_time: 0.000031 Rows_sent: 0 Rows_examined: 0
SET timestamp=1267488361;
DELETE
FROM record_lock
WHERE record_lock.user_id = 221659 AND record_lock.expiration_date_time IS NOT NULL;
record_lock 表当前使用 InnoDB,它现在有不到十几个记录。
我们的系统中有几千个活跃用户。每次他们编辑一条记录时,我们都会插入到这个表中。在系统中任何地方的每个页面加载时,我们 1)从表中选择以查看当前用户是否有任何锁,以及 2)如果有用户的任何记录,则对该表运行 DELETE 查询,引用WHERE 子句中的表的主键。
这是表的架构:
CREATE TABLE IF NOT EXISTS `record_lock` (
`module_id` int(10) unsigned NOT NULL,
`module_record_id` int(10) unsigned NOT NULL,
`site_id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`expiration_date_time` datetime NOT NULL,
`date_time_created` datetime DEFAULT NULL,
PRIMARY KEY (`module_id`,`module_record_id`),
KEY `record_lock_site_id` (`site_id`),
KEY `index_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;