Hi,I am joining two tables for a delete operation, and i am trying to learn the best way to build an index for this. Just now i have created a clustered non-unique index on all the joined columns. But i feel it is still slow. Is there a way this index could be more optimized? CREATE TABLE #DATASETS_TO_UPDATE ( TransactionType_FK INT, TransactionVersion_FK INT, Department_FK INT, Account_FK INT, CounterpartCompany_FK INT, Project_FK INT, Company_FK INT, Currency_FK INT, Year INT, Month INT, Amount float ) CREATE CLUSTERED INDEX IX_ToUpdateMain ON #DATASETS_TO_UPDATE (TransactionType_fk, Department_fk, account_fk, counterpartcompany_fk, project_fk, company_fk, currency_fk, year, month)
The table im joining against has almost the same columns, except a few more. Here is the index for it:CREATE NONCLUSTERED INDEX [test] ON [FactBase].[YTD] ( [TransactionType_FK] ASC, [Department_FK] ASC, [Account_FK] ASC, [CounterPartCompany_FK] ASC, [Project_FK] ASC, [Company_FK] ASC, [Currency_FK] ASC, [Year] ASC) DELETE p FROM FactBase.Periodic p INNER JOIN #DATASETS_TO_UPDATE f ON p.TransactionType_FK = f.TransactionType_FK AND p.Department_FK = f.Department_FK AND p.Account_FK = f.Account_FK AND isnull(p.CounterpartCompany_FK,0) = isnull(f.CounterpartCompany_FK,0) AND isnull(p.Project_FK,0) = isnull(f.Project_FK,0) AND p.Company_FK = f.Company_FK AND p.Currency_FK = f.Currency_FK AND p.Year = f.Year AND p.Month = f.Month
The temp table could have about 200.000 rows and the Periodic potentially have millions of rows, but currently have about 200.000 as well.