I have two FactLik tables (2 million rows) and UpdateStPrice (500 thousand rows rows). I need to update the FactLik table using the rules.
UpdateStPrice
| PRODUCTKEY | WAREHOUSEKEY | STARTDATE | ENDDATE | PRIORITY | UNITPRICE | --------------------------------------------------------------------------- | 36975 | 6 | 20120630 | 20121011 | 0 | 395 | | 36975 | 6 | 20121018 | 20291231 | 0 | 371 | | 36975 | 6 | 20121126 | 20121211 | 120 | 313 | | 36975 | 6 | 20121126 | 20121219 | 120 | 288 | | 36975 | 6 | 20121212 | 20291231 | 120 | 313 |
FactLik
| TIMEKEY | PRODUCTKEY | PRODUCTGROUPKEY | WAREHOUSEKEY | PRICE | ------------------------------------------------------------------- | 20121205 | 36975 | 89 | 6 | (null) |
FactLik Row Update Rules
- For the same
ProductKey , WarehouseKey look for UpdateStPrice in the line, where Priority is MAX and FactLik.TimeKey is between StartDate and EndDate . - Now find the line where
StartDate MAX. - Now find the line where
EndDate is MIN.
Expected Result from UpdateStPrice :
| PRODUCTKEY | WAREHOUSEKEY | STARTDATE | ENDDATE | PRIORITY | UNITPRICE | --------------------------------------------------------------------------- | 36975 | 6 | 20121126 | 20121211 | 120 | 313 |
Result of FactLik :
| TIMEKEY | PRODUCTKEY | PRODUCTGROUPKEY | WAREHOUSEKEY | PRICE | ------------------------------------------------------------------- | 20121205 | 36975 | 89 | 6 | 313 |
Smallfiddle
Bigfiddle
I have my QUERY , which is very slow, in fact it runs slower for more than 12 hours. I have some indexes for both tables (supposedly an execution plan), but they don't help at all :)
So, if you can help me optimize this query, I will be very grateful.
source share