始终进行我知道只会偶尔返回结果的数据库查询的最有效方法是什么?

发布于 2024-11-01 10:04:46 字数 935 浏览 3 评论 0原文

我正在尝试弄清楚如何在我的网站上实现一项新功能。我的网站允许人们下载群组成员上传的内容。这项新功能将允许群组中的 1 名成员成为“超级”用户,其上传的内容无法下载。所以目前我的查询(或多或少)是 -

SELECT * FROM uploads WHERE group = $groupid

我想我会将成员的“超级”状态存储在“成员”表中,因此在执行之前可能会查找此信息以获取超级成员的成员 ID查询。

$all_group_members = SELECT members FROM groups WHERE group_id = $groupid
$super_member = SELECT memberid FROM members WHERE memberid IN ($all_group_members) AND super_status = 1 

然后将查询修改为 -

SELECT * FROM uploads WHERE group = $groupid AND uploader ! = $super_member

然而,组中可能并不总是有超级成员,所以我的问题是是否有更有效的方法来做到这一点,例如将超级成员 id 存储在会话 cookie 中?将超级用户 ID 存储在组表中怎么样 - 仍然涉及执行查询 -

SELECT super_user FROM groups WHERE groupid = $group_id 

但至少少了一个查询。

难道就没有办法解决这个问题吗?我知道大约 5% 的时间里只会有一个超级用户,所以 95% 的时间这将是浪费查询。也许会话 cookie 可以存储 group_has_super_user 变量。

我还缺少其他执行此操作的方法吗?

谢谢

I am trying to work out how to implement a new piece of functionality on my website. My site allows people to download things uploaded by members of a group. The new piece of functionality will allow 1 member of the group to be a 'super' user whos uploads cannot be downloaded. So at the moment my query (more or less) is -

SELECT * FROM uploads WHERE group = $groupid

I am thinking I will store the 'super' status of the member in the 'members' table so will probably look up this to get the member id of the super member before doing the query.

$all_group_members = SELECT members FROM groups WHERE group_id = $groupid
$super_member = SELECT memberid FROM members WHERE memberid IN ($all_group_members) AND super_status = 1 

Then modify the query to be -

SELECT * FROM uploads WHERE group = $groupid AND uploader ! = $super_member

However there may not always be a super member in a group, so my question is is there a more effective way to do this, for example store the super members ids in the session cookie? What about storing the super user id in the group table - still involves doing a query -

SELECT super_user FROM groups WHERE groupid = $group_id 

But atleast thats one less query.

Is there just no way around this - I know that there will only be a super user about 5% of the time so 95% of the time this will be wasted query. Perhaps the session cookie could store a group_has_super_user variable.

Any other ways of doing this that im missing?

thanks

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

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

发布评论

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

评论(2

ぽ尐不点ル 2024-11-08 10:04:46

我认为其他群组成员无法从我的群组中下载任何内容。因此,只需在 上传 中添加一个可下载字段,并将其设置为

  • 1(如果是简单会员上传)或
  • 0(如果是超级会员上传)。

那么您必须运行的唯一查询是:

SELECT * FROM uploads WHERE group = $groupid AND downloadable != 0

此外,如果成员身份发生变化(例如其他人获得超级权限),则应在组中重新计算这些标志。

不要忘记为 groupid + downloadable (或 downloadable + groupid)创建索引(或将主键设置为)!

I assume, other group members can't download anything from my group's stuff. So, simply add a downloadable field to the uploads, and set it to

  • 1, if a simple member has uploaded it, or
  • 0, if a super member has.

Then the only query you have to run is:

SELECT * FROM uploads WHERE group = $groupid AND downloadable != 0

Also, if the membership is changing (e.g. someone else gets the super right), these flags should be recalculated in the group.

Don't forget to create index for (or set the primary key to) groupid + downloadable (or downloadable + groupid)!

记忆で 2024-11-08 10:04:46
CREATE TABLE uploads (id, user_id, …)
CREATE TABLE groups (id, …)
CREATE TABLE group_users (group_id, user_id, is_super, …)

SELECT  u.*
FROM    group_users gu
JOIN    uploads u
ON      u.user_id = gu.user_id
WHERE   gu.group_id = $group_id
        AND gu.is_super = 0
CREATE TABLE uploads (id, user_id, …)
CREATE TABLE groups (id, …)
CREATE TABLE group_users (group_id, user_id, is_super, …)

SELECT  u.*
FROM    group_users gu
JOIN    uploads u
ON      u.user_id = gu.user_id
WHERE   gu.group_id = $group_id
        AND gu.is_super = 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文