I have to develop a web application for movies. For now, in my database, I have a table for the relations between figures and movies and another for the relations between actors (wich are also figures) and movies. The last one also contains information about the character.
My question in not about the normalization at all, I would like to know if it's more efficient in term of speed to do two queries for each tables or to merge them to a single one with NULL values for character in some tuples and do a single query ?
EDIT:
The two queries for now (figure side, to get his acting and career):
$query1 = '
SELECT RelationFigureCardType.department, RelationFigureCardType.job, Movie.card_type_id, Movie.id, Movie.original_name, Movie.release_date, Movie.cover, Movie.cover_ext
FROM relation_figure_cards AS RelationFigureCard
开发者_开发知识库 RIGHT JOIN relation_figure_card_types AS RelationFigureCardType ON
(RelationFigureCard.relation_figure_card_type_id = RelationFigureCardType.id)
RIGHT JOIN cards AS Movie ON
(RelationFigureCard.card_id = Movie.id)
WHERE
RelationFigureCard.figure_card_id = '.$figureId.'
AND RelationFigureCard.last_card_version = 1
ORDER BY Movie.card_type_id, RelationFigureCardType.priority, Movie.release_date
;
';
$query2 = '
SELECT RelationActorCard.is_main, RelationActorCard.is_guest, Movie.card_type_id, Movie.id, Movie.original_name, Movie.release_date, Movie.cover, Movie.cover_ext, Character.id, Character.original_name, Character.cover, Character.cover_ext
FROM relation_actor_cards AS RelationActorCard
RIGHT JOIN cards AS `Character` ON
(RelationActorCard.character_card_id = Character.id)
RIGHT JOIN cards AS Movie ON
(RelationActorCard.card_id = Movie.id)
WHERE
RelationActorCard.figure_card_id = '.$figureId.'
AND RelationActorCard.last_card_version = 1
ORDER BY Movie.card_type_id, Movie.release_date
;
';
I'd strongly suspect the single-query approach to be faster.
I'd even more strongly suspect it to be faster when you're not using a stored procedure but executing the two queries one after the other from a scripting interpreter like PHP, or any other process that allows you to interface with MySQL.
If you want a reliable answer for your case, there is a very simple answer:
Perform a test for your particular scenario with real data and compare the performance. (This is called "benchmarking".)
Depends on the query. LEFT JOIN is something normal in MySQL. Make a single query with proper indexes. Do not optimize it (break to smaller queries), unless it's necessary
精彩评论