MySQL table with the following structure
CREATE TABLE `t_article` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`content` varchar(255) NOT NULL COMMENT "",
`like_count` int(10) unsigned NOT NULL DEFAULT "0" COMMENT "",
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT="";
insert data:
INSERT INTO `t_article` (`id`, `content`, `like_count`) VALUES ("1", "aaa", "4");
INSERT INTO `t_article` (`id`, `content`, `like_count`) VALUES ("2", "rrrr", "53");
INSERT INTO `t_article` (`id`, `content`, `like_count`) VALUES ("3", "ttt", "7");
INSERT INTO `t_article` (`id`, `content`, `like_count`) VALUES ("4", "rree", "6");
INSERT INTO `t_article` (`id`, `content`, `like_count`) VALUES ("5", "rrr", "888");
the requirement is to query the list of articles, sorted from large to small according to the number of likes (like_count) field. The SQL statement is as follows:
SELECT id FROM t_article ORDER BY like_count DESC LIMIT 0,4;
how to build an index is reasonable. The reason for the problem is that after reading many big articles, you will say that the index field should not be updated frequently, but the like_count field must be updated frequently.
there is another derivative problem. The following SQL performs an analysis of whether there is something wrong with type as index. This SQL does not use where conditions and sort fields, but still performs a full table scan. How to optimize it
SELECT id FROM t_article LIMIT 0,4;
EXPLAIN analysis