使用IN检查计数的此查询的更快的MySQL方法?

时间:2015-06-21 03:38:35

标签: mysql sql-in

我正在开发一个与标签链接的视频数据库,当我执行此查询时,我需要等待很长时间,它会创建一个临时表等等,但我认为这应该有更快的方法:

EXPLAIN SELECT SQL_NO_CACHE  `video_id` 
FROM  `video_tag_link` 
WHERE  `tag_id` 
IN (
'2752',  '36370'
)
GROUP BY  `video_id` 
HAVING COUNT(  `video_id` ) =2
LIMIT 0 , 20

MySQL Result: 1
SIMPLE
video_tag_link
range
PRIMARY,video_id
PRIMARY
4
NULL
300268
Using where; Using index; Using temporary; Using filesort

Mysql table
CREATE TABLE IF NOT EXISTS `video_tag_link` (
  `video_id` int(10) unsigned NOT NULL,
  `tag_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`tag_id`,`video_id`),
  KEY `video_id` (`video_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Total of 45mil records in this table.

任何可以帮助我解决此问题的人都会这样做吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用标记ID作为整数

EXPLAIN SELECT SQL_NO_CACHE  `video_id` 
FROM  `video_tag_link` 
WHERE  `tag_id` 
**IN (2752,  36370)**
GROUP BY  `video_id` 
HAVING COUNT(  `video_id` ) =2
LIMIT 0 , 20