我正在尝试显示最新的'抽奖'新闻条目,但我想先显示未过期的新闻条目。我还想先命令他们展示最新的条目。
但是,无论我如何编写查询或索引它,MySQL总是希望使用filesort。
SELECT F.sid, F.title, F.url, F.description, F.bodytext, F.img, F.url, F.submitted_by, F.category, F.ptime, F.expired,
SUM(if(C.active = 1, 1, 0)) AS commentCount
FROM News AS F
LEFT JOIN news_comments.comments AS C ON F.sid = C.postid
WHERE category = 'sweepstakes' AND F.sid
GROUP BY F.sid
ORDER BY F.expired, F.sid DESC
LIMIT 1, 8
+----+-------------+-------+------+-------------------------------+--------------+---------+-----------------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+-------------------------------+--------------+---------+-----------------------------+------+----------------------------------------------+
| 1 | SIMPLE | F | ref | category | category | 98 | const | 158 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | C | ref | get_comments | get_comments | 4 | News.F.sid | 1 | Using index |
+----+-------------+-------+------+-------------------------------+--------------+---------+-----------------------------+------+----------------------------------------------+
2 rows in set (0.00 sec)
我有类别,sid和过期的索引。 “过期”列包含1表示已过期,0表示未过期。
可以用不同的方式重写此查询以避免filesort吗?
编辑:表格结构
News | CREATE TABLE `News` (
`sid` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(80) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(80) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(160) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`bodytext` mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`img` varchar(80) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`submitted_by` varchar(16) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`category` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`ptime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`expired` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`sid`),
KEY `category` (`category`,`sid`,`expired`)
) ENGINE=MyISAM AUTO_INCREMENT=1082 DEFAULT CHARSET=latin1