使用Symfony的ACL时,最好使用JOIN查询或IN数组查询? [英] When using Symfony's ACL, is it better to use a JOIN query or an IN array query?

查看:145
本文介绍了使用Symfony的ACL时,最好使用JOIN查询或IN数组查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题基于的讨论我的使用Symfony ACL的学说查询

  • 有一个类/实体,例如产品
  • 它已映射到数据库表
  • 每一行代表一个对象(一种产品)
  • 我们正在使用ACL来允许/限制对产品的访问

考虑到这一点,我们如何查询以获取用户有权访问的所有产品的列表?

浏览每个产品,并检查用户是否可以访问是毫无疑问的……这很快就会变糟.

Going through each product, and checking if the user has access to is out of question... This will quickly go bad.

在前面的讨论中,我们正在研究IN数组或JOIN查询.

In the previous discussion, we were toying with either IN array, or a JOIN query.

IN数组方法(从acl_entries table获取用户有权访问的产品ID的列表),然后在products table上进行IN数组查询.

IN ARRAY APPROACH Get a list of product ids a user has access to (from the acl_entries table), and then do an IN array query on the products table.

加入方法acl_entries tableproducts table加入.

(请注意,在两种情况下均未使用parent_acls)

(Note, in both cases parent_acls aren't being used)

推荐答案

让我们看一下两种情况下的时间复杂度:

Let's look at time complexities for both cases:

内部阵列方法: M rows的实体表,其ACL条目数组为size N(ACL表中的行与此处无关)

IN ARRAY APPROACH: Entity Table of M rows, with ACL entries array of size N (rows in the ACL table not relevant here)

时间复杂度:O [N * log(M)]

TIME COMPLEXITY: O[N*log(M)]

加入方法: M rows的实体表,以及N rows

时间复杂度:O [M + N]

TIME COMPLEXITY: O[M + N]

实际上,我们通常会遇到这样的情况,

In practice we generally have a situation like,

在阵列中

N=10,000
M=1,000,000
O=>60,000

加入

N=10,000
M=1,000,000
O=>1,010,000

从理论上讲,数组中最坏的情况是

And in theory, the worst case scenario for in array would be

在阵列中

N=1,000,000,000
M=1,000,000,000
O=>9,000,000,000

加入

N=1,000,000,000
M=1,000,000,000
O=>2,000,000,000


这是什么意思?摘要/TL; DR

如果仅授予每个用户访问实体中一部分对象的权限,则使用IN数组.

If each User is only granted access to a fraction of the objects in an entity use IN array.

如果每个用户对实体的每个对象都有一个ACL条目,请使用JOIN.尽管获得的收益将不会达到几个数量级(除非您拥有数万亿的产品),所以您可能仍想使用IN阵列.

If each User will have an ACL entry for every Object of an Entity, use JOIN. Although the gains will not be of several orders of magnitude (unless you have trillions of products), so you might still want to use IN array.

在两种情况下,仅在绝对必要时才使用ACL!选民FTW!

In both cases, use ACL only when absolutely necessary! Voters FTW!

这篇关于使用Symfony的ACL时,最好使用JOIN查询或IN数组查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆