dongtu1357 2011-04-14 10:08
浏览 9
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • doujuyang1764 2011-04-14 10:34
    关注

    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)!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值