对用户列表中的每个人的数据库查询是否过多?
我目前正在使用 MySQL 和 MyISAM。
我有一个函数,它返回我的应用程序中朋友或一般用户的用户 ID 数组,并且在显示它们时, foreach 似乎是最好的。
现在我的问题是我只有 ID,因此我需要嵌套一个数据库调用来根据循环中的用户 ID 获取每个用户的其他信息(即姓名、头像、其他字段)。
我不期望有几十万的用户(主要是为了业余爱好学习),虽然我应该怎么做这个,比如灵活地将代码放在foreach中进行显示,但不依赖ID数组,所以我运气不好使用单个查询?
关于我可以适当地显示列表的任何一般结构或提示?
我的查询量(列表中每个用户 1:1)是否不合适? (虽然有 0..n 页的用户,但一次 10 页,看起来并没有我刚刚意识到的那么糟糕。)
I am currently using MySQL and MyISAM.
I have a function of which returns an array of user IDs of either friends or users in general in my application, and when displaying them a foreach seemed best.
Now my issue is that I only have the IDs, so I would need to nest a database call to get each user's other info (i.e. name, avatar, other fields) based on the user ID in the loop.
I do not expect hundreds of thousands of users (mainly for hobby learning), although how should I do this one, such as the flexibility of placing code in a foreach for display, but not relying on ID arrays so I am out of luck to using a single query?
Any general structures or tips on what I can display the list appropriately with?
Is my amount of queries (1:1 per users in list) inappropriate? (although pages 0..n of users, 10 at a time make it seem not as bad I just realize.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 IN() MySQL 方法,即
这将返回用户 ID 与这些 ID 匹配的所有行。添加的 ID 越多,效率就越低,但您提到的 10 个左右应该就可以了。
You could use the IN() MySQL method, i.e.
That will return all rows where the userid matches those ID's. It gets less efficient the more ID's you add but the 10 or so you mention should be just fine.
为什么不能使用左连接一次性获取所有数据?听起来您正在获取一个列表,但是您只需要获取单个用户的所有信息。是这样吗?
请记住,数据库是关于结果集的,虽然通常您可以在需要时仅返回一行,但您几乎不需要获取一行然后返回以获取更多信息。
例如,朋友列表可能保存在用户条目的文本列中。
Why couldn't you just use a left join to get all the data in 1 shot? It sounds like you are getting a list, but then you only need to get all of a single user's info. Is that right?
Remember databases are about result SETS and while generally you can return just a single row if you need it, you almost never have to get a single row then go back for more info.
For instance a list of friends might be held in a text column on a user's entry.
无论您期望拥有小型数据库还是大型数据库,我都会考虑使用InnoDB引擎而不是MyISAM。它的处理开销确实比 MyISAM 高一点,但是您可以获得所有额外的好处(随着您的爱好的增长),包括 JOIN,这将允许您从多个表中提取特定数据:
将从用户返回 id 以及姓名和头像来自配置文件(其中两个表的 id 匹配)
在线有许多讨论数据库规范化的资源,您可能会喜欢:http://www.devshed.com/c/a/ MySQL/数据库标准化简介/
Whether you expect to have a small database or large database, I would consider using the InnoDB engine rather than MyISAM. It does have a little higher overhead for processing than MyISAM, however you get all the added benefits (as your hobby grows) including JOIN, which will allow you to pull in specific data from multiple tables:
Would return id from Users and name and avatar from Profiles (where id of both tables match)
There are numerous resources online talking about database normalization, you might enjoy: http://www.devshed.com/c/a/MySQL/An-Introduction-to-Database-Normalization/