Database design 多租户表是否应该在主键和外键中包含租户ID?
对于多租户单共享数据库,主键和聚集索引中是否应包含租户ID字段?或者在tenantid上添加一个附加索引是否也同样有效 我们在一个生产系统上遇到性能问题,该系统的唯一索引是主键上的聚集索引 所有sql select语句在其linq to entities语句中都以tenantid开头,例如Database design 多租户表是否应该在主键和外键中包含租户ID?,database-design,azure,indexing,azure-sql-database,multi-tenant,Database Design,Azure,Indexing,Azure Sql Database,Multi Tenant,对于多租户单共享数据库,主键和聚集索引中是否应包含租户ID字段?或者在tenantid上添加一个附加索引是否也同样有效 我们在一个生产系统上遇到性能问题,该系统的唯一索引是主键上的聚集索引 所有sql select语句在其linq to entities语句中都以tenantid开头,例如 invoiceitems.tenantid = thecurrenttenantid order by invoicedate 当前模式 租户(租户唯一标识符主键,租户名称) 外键(tenantid) 索引
invoiceitems.tenantid = thecurrenttenantid order by invoicedate
当前模式
租户(租户唯一标识符主键,租户名称)
外键(tenantid)
索引(聚集在tenantid上)
客户(租户唯一标识符,客户ID唯一标识符主键,客户名称varchar(50))
外键(tenantid、customerid)
索引(聚集在customerid上)
发票(tenantid uniqueidentifier、invoiceid uniqueidentifier主键、billcustomerid uniqueidentifier、shipcustomerid uniqueidentifier、invoicedate datetime)
外键(tenantid、billcustomerid、shipcustomerid)
索引(聚集在invoiceid上)
InvoiceItems(租户唯一标识符,invoiceitemid唯一标识符primarykey,invoiceid唯一标识符,lineitemorder int)
外键(tenantid、invoiceid)
索引(聚集在invoiceitemid上)
SqlAzure要求每个表都有一个聚集索引,因此它当前仅位于primarykeyid上,因为这是默认值。现在,这是每个表上唯一的索引。整个系统中的表中有各种外键,外键表字段都没有索引
我们现在正试图解决一些性能问题,我们想知道什么是最好的聚集索引,以及其他索引是否有帮助。我们希望我们不必改变现有的聚集索引,除非我们绝对必须这样做,但我们愿意这样做。在SqlAzure AFAIK中,您不能简单地调整现有表中的聚集索引-您必须使用所需的聚集索引创建一个新表,并将旧表中的所有记录插入新表(并处理所有外键约束和其他表依赖项)
所有sql select语句在其linq to entities语句中都以tenantid开头
invoiceitems.tenantid = thecurrenttenantid order by invoicedate
有些sql select语句只有一个顺序,有些在引入子表时有其他连接条件值,如
invoiceitems.tenantid = thecurrenttenantid and invoice.invoiceid = invoiceitems.invoiceid order by invoicedate
以下是一些想法(除此之外,我们还向其他人开放)其中哪一个最好,为什么
主键索引选项
加快对租户记录的访问
选项1-在tenantid上添加非聚集索引
发票(tenantid uniqueidentifier、invoiceid uniqueidentifier主键、billcustomerid uniqueidentifier、shipcustomerid uniqueidentifier、invoicedate datetime)
外键(tenantid、billcustomerid、shipcustomerid)
索引(聚集在invoiceid上,非聚集在tenantid上)
选项2-将主键从primaryid更改为tenantid+primaryid,并将聚集索引更改为tenantid+primaryid。
发票(tenantid uniqueidentifier主键、invoiceid uniqueidentifier主键、billcustomerid uniqueidentifier、shipcustomerid uniqueidentifier、invoicedate datetime)
外键(tenantid、billcustomerid、shipcustomerid)
索引(聚集在tenantid+invoiceid上)
外键索引选项
加速加入
选项3-仅在foreignkeyid上的所有外键字段上添加非聚集索引。
发票(tenantid uniqueidentifier、invoiceid uniqueidentifier主键、billcustomerid uniqueidentifier、shipcustomerid uniqueidentifier、invoicedate datetime)
外键(tenantid、billcustomerid、shipcustomerid)
索引(聚集在invoiceid上,未聚集在billcustomerid上,未聚集在shipcustomerid上)
选项4-将所有外键从foreignkeyid更改为tenantid+foreignkeyid,并在tenantid+foreignkeyid上添加索引
发票(tenantid uniqueidentifier、invoiceid uniqueidentifier主键、billcustomerid uniqueidentifier、shipcustomerid uniqueidentifier、invoicedate datetime)
外键(tenantid,tenantid+billcustomerid,tenantid+shipcustomerid)
索引(聚集在invoiceid上,未聚集在tenantid+billcustomerid上,未聚集在tenantid+shipcustomerid上)
SQL选择优化索引选项
加速常用的查询,如从发票中选择字段,其中tenantid=按发票日期计算的价值订单
选项5-在每个表中除tenantid之外最常用的排序顺序字段上添加索引。
发票(tenantid uniqueidentifier、invoiceid uniqueidentifier主键、billcustomerid uniqueidentifier、shipcustomerid uniqueidentifier、invoicedate datetime)
外键(tenantid、billcustomerid、shipcustomerid)
索引(在invoiceid上聚集,在invoicedate上非聚集)
选项6-在每个表中的tenantid+“最常用排序顺序字段”上添加索引,并在tenantid+“最常用排序顺序字段”上添加非聚集索引。
发票(tenantid uniqueidentifier、invoiceid uniqueidentifier主键、billcustomerid uniqueidentifier、shipcustomerid uniqueidentifier、invoicedate datetime)
外键(tenantid、billcustomerid、shipcustomerid)
索引(聚集在invoiceid上,非聚集在tenantid+invoicedate上)看起来您已经考虑了很多。不管我或其他人怎么说,唯一确定的方法就是自己测量。在这种情况下,这不再是一个SQLAzure问题,而是一个一般的SQLServer查询优化问题 为你的情况