Sql-server – TSQL Delete All dates and stores data

deletejoin;sql servert-sql

This might sound like a stupid question but I can't seem to find a better way.
It's a posting system where stores send us info on a daily basis (can be for multiple days). That info is placed in TempTable. For every day found in TempTable it is always for a full day, and the TempTable is good for all stores found in it. So If TempTable has 2 different Stores and 2 different Days such as Day1-Store1 and Day2-Store2, It would mean that nothing happened on Day1 for Store2 and Day2 for Store1.

So, I want to delete any record that BigTable has (for a Specific Day and Store) that is not in the tempTable (for the Days found and Stores Found in the TempTable)
In other words, If Temptable has an entry for Day1-Store1 and Day2-Store2, I need to delete from the Big Table Day1-Store2 and Day2-Store1 and any other entry that is not in the temp Table…

This is what, I'm using right now (it works) but it takes way too long. I'm looking for a way to minimize the number of joins on the TempTable.
Matching Clustered indexes exist on both BigTable and TempTable (Date/Store/SKU_id)

Delete X 
From BigTable X 
join (Select Distinct postingDate from TempTable) D on X.PostingDate = D.PostingDate
Join (Select Distinct postingStore from TempTable) S on X.Store = S.PostingStore
Left join TempTable T on X.PostingDate = T.PostingDate 
    and X.Store = T.PostingStore and X.SKU_id = T.SKU_id
Where T.Sku_id is null

I'm sure this isn't the best way to do this.
Thanking you all in advance

[MORE INFO]
Since I've noticed that the suggestions given posted are to delete everything in BigTable that is not in TempTable, i thought that I should add the following point:

BigTable contains All days for all stores that have already been posted.
TempTable contains the Posting info of specific Days and Stores found in TempTable. I want to delete from BigTable the Stores and Days found in TempTable but without deleting the good entries that are already in BigTable.

If Big Table has the following entries:

Day1 Store1 Sku123 ... 
Day1 Store2 Sku124 ...
Day1 Store3 Sku123 ...
Day2 Store1 Sku124 ... 
Day2 Store2 Sku123 ... 
Day2 Store3 Sku123 ... 

And TmpTable has the following entries:

Day1 Store1 Sku124 ...
Day2 Store2 Sku123 ... 

I would want to delete from BigTable any record for Day1 and Day2 for Store1 and Store2 that is not in TempTable… So I would want to Delete the Following Records from BigTable:

Day1 Store1 Sku123 ... 
Day1 Store2 Sku124 ...
Day2 Store1 Sku124 ... 

Best Answer

If I understand it right, these queries should work:

DELETE X
From BigTableX X 
Left join TempTableX T on X.PostingDate = T.PostingDate 
    and X.Store = T.PostingStore and X.SKU_id = T.SKU_id
Where T.Sku_id is null
AND EXISTS (Select 1 from TempTableX WHERE postingDate = X.PostingDate) 
AND EXISTS (Select 1 from TempTableX WHERE PostingStore = X.Store) 

DELETE X
From BigTableX X 
CROSS APPLY (Select TOP(1) p = 1 from TempTableX WHERE postingDate = X.PostingDate) P
CROSS APPLY (Select TOP(1) s = 1 from TempTableX WHERE postingStore = X.Store) S
Left join TempTableX T on X.PostingDate = T.PostingDate 
    and X.Store = T.PostingStore and X.SKU_id = T.SKU_id
Where T.Sku_id is null

Either of these 2 queries will delete the same rows.

On my test sample they are more efficient but my test tables don't match your data and you will have to test it. They all do 4 table scan but the new query does not sort data (no DISTINCT).

Can you please provide both execution plans (old and new query) for further analysis.

It may help to add an index on postingDate and on postingStore or on postingDate/postingStore/sku_id. If CLUSTERED INDEX on TempTable (Date/Store/SKU_id) is not narrow enough (3 varchar...), it may be better to remove it.