Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Index optimization

Author  Topic 

daheri
Starting Member

9 Posts

Posted - 2012-01-24 : 04:00:35
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.

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2012-01-24 : 05:31:52
Your index will stop being used here:
AND isnull(p.CounterpartCompany_FK,0) = isnull(f.CounterpartCompany_FK,0)
AND isnull(p.Project_FK,0) = isnull(f.Project_FK,0)
So if your selectivity is still high on transaction type, dept, account then try to get rid of the expressions somehow or incorporate them into the index. Also check your collation is the same on both tables where there are strings.

But your number 1 priority should be a bit of groundwork:
1) What are your volumes
2) What are your performance targets (and why). How will you know when you've hit "good enough"?
3) How and what are you measuring?
4) Have you got a test DB set up with representative data?
5) Have you got a scientific try/measure/record/retry sort of plan?
6) Have an open mind!

200K rows is not huge. Sometimes if selectivity is low and you select a significant number of rows then an index is the wrong approach anyway. They just add I/O so the optimiser will not use them.
Have you considered or analysed partitioning? Can you put data into partitions such that the delete basically just removes everything in a partition?
A few things to think about, but no solutions I'm afraid - there's rarely a free lunch with these kind of things. You just have to work at it.
Go to Top of Page

daheri
Starting Member

9 Posts

Posted - 2012-01-24 : 06:23:53
Thanks very much for your reply and suggestions!

Lot of things i had not thought about. I'll have a look at it and see where it brings me
Go to Top of Page
   

- Advertisement -