以前從未見過。運行相同的查詢,1強制索引。沒有索引,結果不正確(按錯誤順序),索引結果按正確的順序排列。只有使用索引的問題是由於某種原因,它更慢。指數是在COMMENT_ID和user_id說明與索引,返回不同的結果
沒有指數:
SELECT DISTINCT topic_id FROM comments
WHERE user_id=9384
AND (status = 1 or status = 0)
ORDER BY comment_id DESC LIMIT 15
搭配指數:
SELECT DISTINCT topic_id FROM comments force index(index_comment_user)
WHERE user_id=9384
AND (status = 1 or status = 0)
ORDER BY comment_id DESC LIMIT 15
任何想法?我真的想要得到正確的順序,而不會減慢查詢速度。我會通過索引來做到這一點。
這裏是SQL結構。
CREATE TABLE `db`.`comments` (
`comment_id` int(10) unsigned NOT NULL auto_increment,
`old_comments_id` mediumint(8) unsigned default NULL,
`user_id` mediumint(8) unsigned default NULL,
`content` text character set latin1,
`status` tinyint(3) unsigned default NULL,
`added_date` datetime default NULL,
`category_id` tinyint(3) unsigned default NULL,
`helpful` tinyint(3) unsigned default NULL,
`modified_date` datetime default NULL,
`topic_id` mediumint(8) unsigned default NULL,
`last_mod_user_id` mediumint(8) unsigned default NULL,
PRIMARY KEY USING BTREE (`comment_id`),
KEY `Index_user_id` (`user_id`),
KEY `Index_added_date` (`added_date`),
KEY `Index_comments_status` USING BTREE (`status`),
KEY `Index_user_activity` USING BTREE (`comment_id`,`user_id`),
KEY `Index_user_activity2` USING BTREE (`user_id`,`topic_id`),
KEY `Index_question_id` USING BTREE (`topic_id`,`status`),
KEY `Index_user_activity3` (`user_id`,`status`,`topic_id`,`comment_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2040237 DEFAULT CHARSET=utf8;
是MySQL的挑剔,在索引中的列的順序 - 如果你創建的user_id的指數Ie ,column_id會有幫助嗎? – Rup 2010-07-26 08:48:04
請同時顯示確切的表格定義。 – Tomalak 2010-07-26 08:50:28
添加了sql表結構 – David 2010-07-26 09:09:06