I have a query which first performs joins inside cte
and then based on cte join with other table and perform group by with some aggregations and get the top 15 records.
WITH join_table as (
SELECT pl.yearmonth,
pl.pdid,
pl.OrderId,
pl.OrderNo AS OrderNumber,
pl.pmc,
pl.pmcode AS pmaa,
pl.spid,
pl.issuingdate,
pl.plguid,
pusa.SizeCode,
pusa.atnumber,
pu.tpc,
pu.ItemQty AS puItemQty
FROM table1 pl
JOIN table3 pusa ON pl.plguid=pusa.plguid
JOIN table2 pu ON pusa.plguid=pu.plguid AND pusa.puguid=pu.puguid
WHERE pl.pmode='BBB'
and pl.pltype='CCC'
and pl.plstatus='AAA'
and pu.ctcode='S'
AND pu.ctype='DDD'
AND pu.tpc <> 'EEE'
),
get_top_15PM as (
SELECT TOP 15 pl.pmc, sum(cast(puItemQty as BIGINT)) AS SumpuItemQty
FROM join_table pl
join abctable b on b.pdid= pl.pdid and b.spid= pl.spid
WHERE pl.issuingdate > '2021-8-1'
and prodtypeid in (select prodtypeid from prodtype where prodgrpid in (3,4,5,7,8,9,10,13,20))
group by pl.pmc
ORDER BY SumpuItemQty DESC
)
SELECT DISTINCT
pl.yearmonth,
pl.pdid,
pl.OrderId,
pl.OrderNumber,
pl.pmc,
pl.pmaa,
pl.spid,
pl.issuingdate,
pl.atnumber,
pl.tpc,
pl.puItemQty,
s.SizeName AS Size,
s.SizeLength,
s.SizeWidth,
s.SizeHeight,
s.SizeVolume,
s.SizeWeight
FROM join_table pl
JOIN PLSize s ON pl.plguid=s.plguid AND pl.SizeCode=s.SizeCode
WHERE pl.pmc IN ( SELECT pmc from get_top_15PM)
AND pl.yearmonth>=202100
This query is taking 20+ hours to run on Azure SQL Database.
Details:
Azure database pricing tier: S6 with 750GB storage with 20% unused storage space left
TableName rows TotalSpaceGB UsedSpaceGB UnusedSpaceGB
table2 332,318,173 117.72 117.71 0.01
table3 153,700,352 60.78 60.76 0.01
table1 15,339,815 13.21 13.20 0.01
abctable 1,232,868 0.81 0.80 0.00
Estimation execution plan with query
Actual Execution plan with query
wait type: (14ms)PAGEIOLATCH_SH:dev-db:1(*)
or NULL
sometimes. Got this using sp_WhoIsActive
.
Note:
Actual execution plan was taken from the running query as shown in this SO answer
This tables contains clustered index on primary keys and nonclustered index on other fields which I am not using in filters or joins or order by.
table1
is parent of table2
and table2
is parent of table3
Any advice or suggestions are highly appreciable.
Update-1
--table1
index_name index_description index_keys
idx_table1_pmcode nonclustered pmcode
PK_table1 clustered, unique, primary key plguid
--table2
index_name index_description index_keys
IX_table2_plguid nonclustered plguid
PK_table2 clustered, unique, primary key puguid
--table3
index_name index_description index_keys
IX_table3_plguid nonclustered plguid
PK_table3 clustered, unique, primary key pusguid
--abctable
index_name index_description index_keys
nci_wi_abctable_2BC1D nonclustered dpyearmonth
nci_wi_abctable_FAA89 nonclustered dpnumber, ptid
PK_abctable clustered, unique, primary key OrderId
DDL
--table1
SET ANSI_PADDING ON
GO
CREATE NONCLUSTERED INDEX [idx_table1_pmcode] ON [dbo].[table1]
(
[pmcode] ASC
)
INCLUDE([OrderID],[OrderNo],[yearmonth],[pdid]) WITH (STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF, ONLINE = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
--table2
SET ANSI_PADDING ON
GO
CREATE NONCLUSTERED INDEX [IX_table2_plguid] ON [dbo].[table2]
(
[plguid] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF, ONLINE = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
--table3
SET ANSI_PADDING ON
GO
CREATE NONCLUSTERED INDEX [IX_table3_plguid] ON [dbo].[table3]
(
[plguid] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF, ONLINE = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
--abctable
SET ANSI_PADDING ON
GO
CREATE NONCLUSTERED INDEX [nci_wi_abctable_2BC1D] ON [dbo].[abctable]
(
[dpyearmonth] ASC
)
INCLUDE([pduid],[pdid]) WITH (STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF, ONLINE = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [nci_wi_abctable_FAA89] ON [dbo].[abctable]
(
[dpyearmonth] ASC,
[ptid] ASC
)
INCLUDE([OrderStatus],[spid]) WITH (STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF, ONLINE = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO