first of all, there is a table with a table structure of
CREATE TABLE `article_like` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "id",
`article_id` int(10) unsigned NOT NULL COMMENT "id",
`user_id` int(10) unsigned NOT NULL COMMENT "id",
`create_at` datetime NOT NULL COMMENT "",
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_article_id_user_id` (`article_id`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="";
A user can only like an article once, so the article id and user id are uniquely indexed.
< H1 > sample data < / H1 >
SQL ID explain
explain
ID
explain
< H1 > here comes the problem < / H1 >
what I want to ask this time is how to build this index in this scenario. My idea now is to set up two indexes. One is to make a unique index with the article id and the user id, and then to create a separate index with the article id. But in this way, MySQL will still choose the previous unique index. This question will be used in many scenarios. If there are comrades who know, can you help solve?