抓取具有关系的数据。 1 个大查询或几个小查询
这样的表结构
假设我有一个像
,我想获取所有用户元数据
,最好进行一次大查询连接图像中的所有表…………
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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,您让数据库本身进行连接。这就是关系数据库管理系统的用途。它包含无数的优化,其性能比普通应用程序代码更好。
有时,这确实会在通过网络发送信息方面产生大量开销(例如,您循环超过 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)