Faster MySQL method for this query using IN with check on count?

65 Views Asked by At

I'm working on a video database that is linked with tags, when I execute this query I need to wait a long time, it creates a temporary table and such, but I think this should have a faster way:

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.

Anyone that can help me with this query?

1

There are 1 best solutions below

6
On

Can you try with tag ids as integers

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