Answer the question
In order to leave comments, you need to log in
What is the best way to design a database for the best performance?
The question is about performance and the use of indexes.
There is a table FILMS (id, title, rating)
Movies have 2 types of filters: genre and country; Accordingly, a film can have several genres and several countries. When filtering, it should look for films where there is at least one match for a pair of genre and country.
Hence 2 options for implementing the structure:
1) Create 2 tables GENRE and COUNTRY and 2 intermediate tables. If you set the search knowing the IDs of countries and genres, and search only by the intermediate table:
SELECT * FORM films f
JOIN genre_film g ON(g.film_id = f.id ) WHERE g.genre_id IN(1,2,3)
JOIN country_film c ON(c.film_id = f.id ) WHERE c.country_id IN(1,2,3)
SELECT * FORM films f
JOIN param_film p ON(p.film_id = f.id )
WHERE (p.param_id IN(1,2,3) AND p.type = `country`) AND (p.param_id IN(4,5,6) AND p.type = `genre`)
Answer the question
In order to leave comments, you need to log in
The first one will almost certainly be faster.
But ideally, you should run tests on data of about the size that you will end up with.
1 will be faster if where has a comparison with the attributes of the films table
The options are the same, and therefore the performance will be approximately the same.
Only in the second option, it is necessary to use, of course, not a textual comparison - use the directory of countries and genres, and search by country and genre IDs.
I would choose the second option, because. fewer tables. But in the first option, the queries will be more understandable.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question