Hi All,
I have the following query,it takes more than 10 minutes. How can it be optimized. please help. thanks
DECLARE @InitDt AS date
SELECT @InitDt = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-16, 0)
;with cte as
(
SELECT ROW_NUMBER() OVER (PARTITION BY dim.Act_ID , dt.YEAR_NUMBER, dt.MONTH_OF_YEAR_NUMBER ORDER BY dt.FULL_DATE DESC) rn
,dim.Act_id, dim.Mid
, ft.Amount
,dt.YEAR_NUMBER
, dt.MONTH_OF_YEAR_NUMBER
, dt.FULL_DATE
, ft.END_DATE
FROM dbo.FACT_Table ft
JOIN DIM_Table dim ON ft.DIMID_ACCOUNT = dim.DIMID_ACCOUNT
JOIN DIM_DATE dt ON dt.FULL_DATE BETWEEN ft.BEGIN_DATE AND ft.END_DATE
WHERE dt.FULL_DATE >= @InitDt
)
SELECT *
FROM cte c
where rn = 1
---------------
The fact table has huge amount of data. The purpose of this query to retrieve only month-end/latest amount for each Act_Id, for the last 16 months.
The dim-date and Fact has no pk-fk relationship
The indexes on tables are as follows
- Dim_date (unique,Non-clustered) on FULL_DATE, 2- Dim_Date (Pk,Unique, clustered, asc) on DimId date (this dimid_date is being used for lookup)
-FACT_Table( Non-Unique, Non-Clustered) on BEGIN_DATE and END_DATE
-DIM_table(uniquee non-clustered on Act_id (with few other fields of the table).
Hope this will help in understdanding the scenareo. ( Please see latest query plan attached , after query execution)
I have the following query,it takes more than 10 minutes. How can it be optimized. please help. thanks
DECLARE @InitDt AS date
SELECT @InitDt = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-16, 0)
;with cte as
(
SELECT ROW_NUMBER() OVER (PARTITION BY dim.Act_ID , dt.YEAR_NUMBER, dt.MONTH_OF_YEAR_NUMBER ORDER BY dt.FULL_DATE DESC) rn
,dim.Act_id, dim.Mid
, ft.Amount
,dt.YEAR_NUMBER
, dt.MONTH_OF_YEAR_NUMBER
, dt.FULL_DATE
, ft.END_DATE
FROM dbo.FACT_Table ft
JOIN DIM_Table dim ON ft.DIMID_ACCOUNT = dim.DIMID_ACCOUNT
JOIN DIM_DATE dt ON dt.FULL_DATE BETWEEN ft.BEGIN_DATE AND ft.END_DATE
WHERE dt.FULL_DATE >= @InitDt
)
SELECT *
FROM cte c
where rn = 1
---------------
The fact table has huge amount of data. The purpose of this query to retrieve only month-end/latest amount for each Act_Id, for the last 16 months.
The dim-date and Fact has no pk-fk relationship
The indexes on tables are as follows
- Dim_date (unique,Non-clustered) on FULL_DATE, 2- Dim_Date (Pk,Unique, clustered, asc) on DimId date (this dimid_date is being used for lookup)
-FACT_Table( Non-Unique, Non-Clustered) on BEGIN_DATE and END_DATE
-DIM_table(uniquee non-clustered on Act_id (with few other fields of the table).
Hope this will help in understdanding the scenareo. ( Please see latest query plan attached , after query execution)