抓取具有关系的数据。 1 个大查询或几个小查询

发布于 2024-11-14 08:27:36 字数 626 浏览 6 评论 0原文

这样的表结构

假设我有一个像在此处输入图像描述

,我想获取所有用户元数据

,最好进行一次大查询连接图像中的所有表…………

SELECT [columns ...] FROM usermetasections LEFT JOIN usermetakeys ON ...
LEFT JOIN usermetas ON ...
LEFT JOIN users ON ...
WHERE users.id = xxx

或者

将它们分成更小的查询,例如……

Loop through SELECT * FROM usermetasections
    Loop through SELECT * FROM usermetakeys WHERE section = xxx
        Loop thought SELECT * FROM usermetas WHERE key = xxx AND user = xxx

我认为如果我采用第一种方法,“机器”性能会更快,但对开发人员性能不利?更难阅读,需要更多代码来将查询解析为 PHP 对象?

Suppose I have a table structure like

enter image description here

I want to grab all user metas

is it better to do a big query joining all tables in the image ...

SELECT [columns ...] FROM usermetasections LEFT JOIN usermetakeys ON ...
LEFT JOIN usermetas ON ...
LEFT JOIN users ON ...
WHERE users.id = xxx

...

... or break them into smaller queries like ...

Loop through SELECT * FROM usermetasections
    Loop through SELECT * FROM usermetakeys WHERE section = xxx
        Loop thought SELECT * FROM usermetas WHERE key = xxx AND user = xxx

I think if I go the 1st approach, it will be faster for "machine" performance but bad for developer performance? harder to read, more code to parse queries into PHP objects?

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

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

发布评论

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

评论(1

你好,陌生人 2024-11-21 08:27:36

通常,您让数据库本身进行连接。这就是关系数据库管理系统的用途。它包含无数的优化,其性能比普通应用程序代码更好。

有时,这确实会在通过网络发送信息方面产生大量开销(例如,您循环超过 1000 条记录,并且仅当满足一些复杂条件时,您才需要来自其他表的信息),在这种情况下,您可以让您的应用程序代码进行连接,但即使如此,您也可以使用 SELECT yourfields FROM yourtables-joined WHERE yourtable.id IN (list,of,IDs) 重新查询

Usually, you let the database itself do the joining. That's what a relational database management system is made for. It contains countless optimizations that will perform better than your average application code.

Sometimes that does indeed create a lot of overhead in regards to information being sent over the network (say, you loop over 1000 records, and only if a few complex conditions are met, you need the info from the other table) in which case you could let your application-code do the join, but even then you can just re-query with SELECT yourfields FROM yourtables-joined WHERE yourtable.id IN (list,of,IDs)

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