For a multi-tenant single shared database should the tenantid field be included in the primary key and clustered index? Or is adding an additional index on tenantid just as performant?
We’re running into performance issues on a production system whose only index is the clustered index on the primary key.
ALL sql select statements start with tenantid in their linq to entities statements such as
invoiceitems.tenantid = thecurrenttenantid order by invoicedate
CURRENT SCHEMA
Tenants (tenantid uniqueidentifier primary key, tenantname) Foreign Keys (tenantid) Indexes(Clustered on tenantid)
Customers (tenantid uniqueidentifier, customerid uniqueidentifier primary key, customername varchar(50)) Foreign Keys (tenantid, customerid) Indexes (clustered on customerid)
Invoices (tenantid uniqueidentifier, invoiceid uniqueidentifier primary key, billcustomerid uniqueidentifier, shipcustomerid uniqueidentifier, invoicedate datetime) Foreign Keys (tenantid, billcustomerid, shipcustomerid) Indexes (clustered on invoiceid)
InvoiceItems (tenantid uniqueidentifier, invoiceitemid uniqueidentifier primarykey, invoiceid uniqueidentifier, lineitemorder int) Foreign Keys (tenantid, invoiceid) Indexes (clustered on invoiceitemid)
SqlAzure requires that each table has a clustered index so it's currently just on primarykeyid since that’s the default. Right now that is the only index on each table. There are various foreign keys in the tables throughout the system and none of the foreign key table fields are indexed.
We're trying to resolve some performance issues right now and were wondering what would be the best clustered index and if any other indexes might be helpful. We're hoping we don't have to change the existing clustered index unless we absolutely have to but we are willing to do so. In SqlAzure AFAIK you cannot simply adjust the clustered index in an existing table - you have to create a new table with the desired clustered index and insert all records from the old table to the new table (and handle all of the foreign key constraints and other table dependencies).
ALL sql select statements start with tenantid in their linq to entities statements.
invoiceitems.tenantid = thecurrenttenantid order by invoicedate
Some sql select statements just have an order - some have other join condition values when bringing in child tables like
invoiceitems.tenantid = thecurrenttenantid and invoice.invoiceid = invoiceitems.invoiceid order by invoicedate
Here are a few ideas (we're open to others besides this) - which of these would be best and why?
PRIMARY KEY INDEX OPTIONS
To speed up access to the tenant's records
Option 1 - Add a non-clustered index on tenantid
Invoices (tenantid uniqueidentifier, invoiceid uniqueidentifier primary key, billcustomerid uniqueidentifier, shipcustomerid uniqueidentifier, invoicedate datetime) Foreign Keys (tenantid, billcustomerid, shipcustomerid) Indexes (clustered on invoiceid, non-clustered on tenantid)
Option 2 - Change the primary key from primaryid to tenantid + primaryid and change the clustered index to tenantid + primaryid.
Invoices (tenantid uniqueidentifier primary key, invoiceid uniqueidentifier primary key, billcustomerid uniqueidentifier, shipcustomerid uniqueidentifier, invoicedate datetime) Foreign Keys (tenantid, billcustomerid, shipcustomerid) Indexes (clustered on tenantid + invoiceid)
FOREIGN KEY INDEX OPTIONS
To speed up joins
Option 3 - Add non-clustered indexes on all foreign key fields only on foreignkeyid.
Invoices (tenantid uniqueidentifier, invoiceid uniqueidentifier primary key, billcustomerid uniqueidentifier, shipcustomerid uniqueidentifier, invoicedate datetime) Foreign Keys (tenantid, billcustomerid, shipcustomerid) Indexes (clustered on invoiceid, non-clustered on billcustomerid, non-clustered on shipcustomerid)
Option 4 - Change all foreign keys from foreignkeyid to tenantid + foreignkeyid and add an index on tenantid + foreignkeyid
Invoices (tenantid uniqueidentifier, invoiceid uniqueidentifier primary key, billcustomerid uniqueidentifier, shipcustomerid uniqueidentifier, invoicedate datetime) Foreign Keys (tenantid, tenantid + billcustomerid, tenantid + shipcustomerid) Indexes (clustered on invoiceid, non-clustered on tenantid + billcustomerid, non-clustered on tenantid + shipcustomerid)
SQL SELECT OPTIMIZATION INDEX OPTIONS
To speed up often used queries like select fields from invoices where tenantid = value order by invoicedate
Option 5 - add indexes on most often used sort order fields within each table besides tenantid.
Invoices (tenantid uniqueidentifier, invoiceid uniqueidentifier primary key, billcustomerid uniqueidentifier, shipcustomerid uniqueidentifier, invoicedate datetime) Foreign Keys (tenantid, billcustomerid, shipcustomerid) Indexes (clustered on invoiceid, non-clustered on invoicedate)
Option 6 - add indexes on tenantid + “most often used sort order field” within each table and add non-clustered index on tenantid + “most often used sort order field”
Invoices (tenantid uniqueidentifier, invoiceid uniqueidentifier primary key, billcustomerid uniqueidentifier, shipcustomerid uniqueidentifier, invoicedate datetime) Foreign Keys (tenantid, billcustomerid, shipcustomerid) Indexes (clustered on invoiceid, non-clustered on tenantid + invoicedate)