在我的数据库中,我有一张桌子' karmalog'。它基本上是系统中的操作日志。它包含几行100k。我想检索最新的36个日志项,但只检索那些与一系列日志类型匹配的日志项(称为' event')。
以下是查询:
SELECT id
FROM karmalog
WHERE event
IN (
'FAV_IMG_ADD', 'FOLLOW', 'COM_POST', 'IMG_VOTE', 'LIST_VOTE', 'JOIN', 'CLASS_UP', 'LIST_CREATE', 'FORUM_REPLY', 'FORUM_CREATE', 'FORUM_SUBSCRIBE', 'IMG_GEO', 'IMG_ADDSPECIE', 'SPECIE_ADDVIDEO', 'EARN_MEDAL'
)
ORDER BY id DESC
LIMIT 0 , 36
此查询目前需要0.2到0.5秒。我想把它变成0.00x范围。由于上面的查询检索单个列,单个条件和单个排序参数,我认为我只是创建一个合适的索引,一切都会很好。到目前为止没有运气。这是表定义,从不相关的字段中删除:
DROP TABLE IF EXISTS `karmalog`;
CREATE TABLE `karmalog` (
`id` int(11) NOT NULL auto_increment,
`event` enum('EDIT_PROFILE','EDIT_AVATAR','EDIT_EMAIL','EDIT_PASSWORD','FAV_IMG_ADD','FAV_IMG_ADDED','FAV_IMG_REMOVE','FAV_IMG_REMOVED','FOLLOW','FOLLOWED','UNFOLLOW','UNFOLLOWED','COM_POSTED','COM_POST','COM_VOTE','COM_VOTED','IMG_VOTED','IMG_UPLOAD','LIST_CREATE','LIST_DELETE','LIST_ADMINDELETE','LIST_VOTE','LIST_VOTED','IMG_UPD','IMG_RESTORE','IMG_UPD_LIC','IMG_UPD_MOD','IMG_UPD_MODERATED','IMG_VOTE','IMG_VOTED','TAG_FAV_ADD','CLASS_DOWN','CLASS_UP','IMG_DELETE','IMG_ADMINDELETE','IMG_ADMINDELETEFAV','SET_PASSWORD','IMG_RESTORED','IMG_VIEW','FORUM_CREATE','FORUM_DELETE','FORUM_ADMINDELETE','FORUM_REPLY','FORUM_DELETEREPLY','FORUM_ADMINDELETEREPLY','FORUM_SUBSCRIBE','FORUM_UNSUBSCRIBE','TAG_INFO_EDITED','JOIN','IMG_GEO','IMG_ADDSPECIE','IMG_REMOVESPECIE','SPECIE_ADDVIDEO','SPECIE_REMOVEVIDEO','EARN_MEDAL') NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `user_sec_id` (`user_sec_id`),
KEY `image_id` (`object_id`),
KEY `date_event` (`date_created`,`event`),
KEY `event` (`event`),
KEY `date_created` (`date_created`),
KEY `date_created_2` (`date_created`,`id`),
KEY `event_2` (`event`,`delete`),
CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE SET NULL,
CONSTRAINT `user_sec_id` FOREIGN KEY (`user_sec_id`) REFERENCES `user` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
正如你所看到的,我已经尝试了一些指数。此版本中未显示另外两个索引尝试(id,event和event,id),两者都无济于事。在目前为止尝试的所有组合中,MySQL解释说它一直在使用filesort。
有什么想法吗?
答案 0 :(得分:0)
这是您的查询:
SELECT id
FROM karmalog
WHERE event IN ('FAV_IMG_ADD', 'FOLLOW', 'COM_POST', 'IMG_VOTE', 'LIST_VOTE', 'JOIN',
'CLASS_UP', 'LIST_CREATE', 'FORUM_REPLY', 'FORUM_CREATE', 'FORUM_SUBSCRIBE',
'IMG_GEO', 'IMG_ADDSPECIE', 'SPECIE_ADDVIDEO', 'EARN_MEDAL'
)
ORDER BY id DESC
LIMIT 0 , 36;
要尝试的第一个索引是karmalog(event, id)
。第二个是karmalog(id, event)
。这些都不在您的列表中。