Domanda

I'm using SQL Server 2012 and encountered strange problem.

This is the original query I've been using:

DELETE FROM [TABLE_TEMP]

INSERT INTO [TABLE_TEMP]
   SELECT H.*, NULL 
   FROM [TABLE_Accounts_History] H
   INNER JOIN [TABLE_For_Filtering] A ON H.[RSIN] = A.[RSIN]
   WHERE
      H.[NUM] = (SELECT TOP 1 [NUM] FROM [TABLE_Accounts_History]
                 WHERE [RSIN] = H.[RSIN] 
                   AND [AccountSys] = H.[AccountSys]
                   AND [Cl_Acc_Typ] = H.[Cl_Acc_Typ]
                   AND [DATE_DEAL] < @dte
                 ORDER BY [DATE_DEAL] DESC)
      AND H.[TYPE_DEAL] <> 'D'

Table TABLE_Accounts_History consists of 3 200 000 records.

Table TABLE_For_Filtering is around 1 500 records.

Insert took me 2m 40s and inserted 1 600 000 records for further work.

But then I decided to attach a column from pretty small table TABLE_Additional (only around 100 recs):

DELETE FROM [TABLE_TEMP]

INSERT INTO [TABLE_TEMP]
   SELECT H.*, P.[prof_type]    
   FROM [TABLE_Accounts_History] H
   INNER JOIN [TABLE_For_Filtering] A ON H.[RSIN] = A.[RSIN]
   LEFT JOIN [TABLE_Additional] P ON H.[ACCOUNTSYS] = P.[AccountSys]
   WHERE        H.[NUM] = ( SELECT TOP 1    [NUM]
                        FROM            [TABLE_Accounts_History]
                        WHERE           [RSIN] = H.[RSIN]
                                    AND [AccountSys] = H.[AccountSys]
                                    AND [Cl_Acc_Typ] = H.[Cl_Acc_Typ]
                                    AND [DATE_DEAL] < @dte
                        ORDER BY        [DATE_DEAL] DESC)
        AND H.[TYPE_DEAL] <> 'D' 

And now it takes ages this query to complete. Why is it so? How such small left join possibly can dump performance? How can I improve it?

An update: no luck so far with LEFT JOIN. Indexes, no indexes, hinted indexes.. For now I've found a workaround by using my first query and UPDATE after it:

UPDATE  [TABLE_TEMP]
SET     [PROF_TYPE] = P1.[prof_type]
FROM    [TABLE_TEMP] A1
LEFT JOIN
        [TABLE_Additional] P1
    ON  A1.[ACCOUNTSYS] = P1.[AccountSys]

Takes only 5s and does pretty much the same I've been trying to achieve. Still SQL Server performance is mystery to me.

È stato utile?

Soluzione

The 'small' left join is actually doing a lot of extra work for you. SQL Server has to go back to TABLE_Additional for each row from your inner join between and TABLE_Accounts_History and TABLE_For_Filtering. You can help SQL Server a few ways to speed this up by trying some indexing. You could:

1) Ensure TABLE_Accounts_History has an index on the Foreign Key H.[ACCOUNTSYS]

2) If you think that TABLE_Additional will always be accessed by the AccountSys, i.e. you will be requesting AccountSys in ordered groups, you could create a Clustered Index on TABLE_Additional.AccountSys. (in orther words physically order the table on disk in order of AccountSys)

3) You could also ensure there is a foreign key index on TABLE_Accounts_History.

Altri suggerimenti

left outer join selects all rows from left table. In Your case your left table has 3 200 000 this much rows and then comparing with each record to your right table. One solution is to use Indexes which will reduce retrieval time.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top