I’ve got a MySQL table with about 100,000 records. When I try to select about 20 rows from this table using the primary key (an unsigned int) it takes about a second to execute. Is there any way to optimise this?
Table: films
Column Type Null Default
film_id int(11) No
film_name varchar(45) No
film_overview text No
film_tagline varchar(150)Yes NULL
release_date date Yes NULL
api_id int(11) No
category_id int(11) No 0
Indexes on films
Keyname Type Unique Packed Column Cardinality Collation Null Comment
PRIMARY BTREE Yes No film_id 110236 A No
api_id BTREE No No api_id 110236 A No
film_id BTREE No No film_id 110236 A No
category_id BTREE No No category_id 4 A No
EXPLAIN
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY films ALL NULL NULL NULL NULL 115029 Using where
2 DEPENDENT SUBQUERY popular_films ALL NULL NULL NULL NULL 18 Using where
QUERY
SELECT film_name FROM `films`
WHERE film_id in (SELECT film_id FROM popular_films);
Now that you’ve added the query, the answer is simple: just use a join:
It looks like your popular_films table has the same structure as films, and so probably shouldn’t be a separate table.