数据库速度优化(多次查询)

发布于 2024-11-24 02:17:41 字数 1748 浏览 5 评论 0原文

我必须开发一个电影网络应用程序。 目前,在我的数据库中,我有一个用于人物和电影之间关系的表,另一个用于演员(也是人物)和电影之间关系的表。 最后一个还包含有关角色的信息。

我的问题根本不是关于规范化,我想知道在速度方面对每个表进行两次查询是否更有效,或者将它们合并到一个元组中,在某些元组中字符为 NULL 值并执行一次查询是否更有效询问 ?

编辑:

现在的两个查询(图面,了解他的表演和职业生涯):

$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 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
        ;
    ';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情感失落者 2024-12-01 02:17:41

我强烈怀疑单查询方法会更快。

我更强烈地怀疑,当您不使用存储过程而是从脚本解释器(如 PHP)或任何其他允许您与 MySQL 交互的进程一个接一个地执行两个查询时,它会更快。

如果您想要为您的案例提供可靠的答案,有一个非常简单的答案:

使用真实数据针对您的特定场景进行测试并比较性能。 (这称为“基准测试”。)

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".)

ぇ气 2024-12-01 02:17:41

取决于查询。 LEFT JOIN 在 MySQL 中是很常见的。使用适当的索引进行单个查询。除非有必要,否则不要优化它(分解为较小的查询)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文