MySQL 性能:使用 GROUP_CONCAT 的单个查询,还是两个单独的查询?
我有一个MySQL数据库,其中每个用户都有一个帐户,每个帐户可以有多个权限。
我的最终目标是得到帐户的用户名和以逗号分隔的权限 ID 列表。有两种方法可以实现此目的:
SELECT a.username, GROUP_CONCAT(rp.permission_id) as permission_ids
FROM account AS a
JOIN role_permission AS rp
ON rp.role_id = a.role_id
WHERE a.id = 1902
... 或 ...
SELECT username
FROM account
WHERE id = 1902;
SELECT permission_id
FROM account_permission
WHERE account_id = 1902
通过单个查询,我就可以得到完全符合我想要的结果。对于两个查询,我必须使用第二个结果集在应用程序 (PHP) 中创建逗号分隔列表。
是否有任何性能原因不选择第一个选项?我以前从未使用过 GROUP_CONCAT,所以我不知道它对性能的影响。
I have a MySQL database in which each user has an account, and each account can have multiple permissions.
My ultimate goal is to end up with the account's username, and a comma-delimited list of permissions ids. There are two ways I can accomplish this:
SELECT a.username, GROUP_CONCAT(rp.permission_id) as permission_ids
FROM account AS a
JOIN role_permission AS rp
ON rp.role_id = a.role_id
WHERE a.id = 1902
... or ...
SELECT username
FROM account
WHERE id = 1902;
SELECT permission_id
FROM account_permission
WHERE account_id = 1902
With the single query, I get my results exactly as I want them. With two queries, I have to create the comma-delimited list in the app (PHP) using the second result set.
Are there any performance reasons to NOT choose the first option? I have never used GROUP_CONCAT before, so I don't know the implications of it, performance-wise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
性能应该还不错——比两个查询要好。您需要注意 长度是虽然有限:
The performance should be OK - better than two queries. You need to be aware that the length is limited though: