D
D
devilsD2019-03-30 02:04:13
MySQL
devilsD, 2019-03-30 02:04:13

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)

2) Make one PARAMS table with the TYPE column which will indicate what kind of parameter it is (country or genre) and the 1st intermediate table. Here, a composite index on the film_id and type fields may still be appropriate.
Here, too, the search is only about the intermediate table.
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`)

the TYPE column will not be in the intermediate table, I specified it in the query to simplify the example, because the filter will probably use the main table with parameters.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stalker_RED, 2019-03-30
@Stalker_RED

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.

L
Lazy @BojackHorseman MySQL, 2019-03-30
Tag

1 will be faster if where has a comparison with the attributes of the films table

R
res2001, 2019-03-30
@res2001

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 question

Ask a Question

731 491 924 answers to any question