I created a query for a client and I've been tweaking it trying to get it just right for their needs. I had it almost set except for some odd counts. After a serious FACEPALM moment I realized that the DISTINCT in the COUNT and SUM were throwing off my numbers, so I removed them.
However, now the query appears to run forever, when it used to take between 2-3min. I let it run overnight and it crashed after 5hrs with the following error:
Could not allocate a new page for database 'TEMPDB' because of insufficient disk space in filegroup 'DEFAULT'. Create the necessary space by dropping objects in the filegroup, adding additional files to the filegroup, or setting autogrowth on for existing files in the filegroup.
The tempdb.mdf was at 16Gig, and I had 7 tempdb_mssql_#.ndf files at 16Gig each, and my log file was at 4Gig, very happily filling my hard drive completely.
I rebooted the server and shrunk the database so I'm back to square one, but I don't understand why it did that.
Here is the query that is causing the issues:
SELECT
DISTINCT ap.[SourcePartID] AS [Assembly Part ID],
p.[PART_X] AS [Assembly Part #],
p.[DESCR_X] AS [Assembly Part Description],
oa2.[Part Count],
oa2.[Total # of Parts],
([dbo].[fn_getFactoryStdCost](ap.[SourcePartID])) AS [Factory Std Cost],
oa2.[# of Docs],
oa2.[# of Software],
oa2.[# of Std Cost Parts],
oa2.[# of HR Devices],
oa2.[Sum HR Devices],
oa2.[# of 3rd Party Devices],
oa2.[Sum 3rd Party Devices],
oa2.[# of Robots],
oa2.[Sum of Robots],
oa2.[# of Buy Parts],
oa2.[# of Make Parts],
oa2.[# of Ref Parts],
bom.[Make/Buy]
INTO bomSummary
FROM AllPartsList ap
LEFT JOIN visuser.EN_PART p
ON p.[EN_Part_ID] = ap.[SourcePartID]
LEFT JOIN bomBreakdown bom
ON bom.[SourcePartID] = ap.[SourcePartID]
OUTER APPLY (
SELECT
[Part Count] = COUNT( DISTINCT IIF( bom.[Qty] = 0, null, bom.[Component Part #]) ),
[Total # of Parts] = SUM(bom.[Qty]),
[# of Std Cost Parts] = COUNT( IIF( ( [dbo].[fn_getFactoryStdCost]([ComponentPartID]) > 0 ), bom.[Component Part #], null) ),
[# of Docs] = COUNT( IIF( bom.[Commodity Code] IN ('009', '072', '073', '075', '079', '082'), bom.[Component Part #], null) ), -- Commodity Codes: 009, 072, 073, 075, 079, 082 : Commodity ID: 15, 84, 85, 87, 81, 92
[# of Software] = COUNT( IIF( bom.[Commodity Code] IN ('034'), bom.[Component Part #], null) ), -- Commodity Code 034 : Commodity ID: 28
[# of HR Devices] = COUNT( IIF( ( bom.[Commodity Code] IN ('002') AND [dbo].[fn_getFactoryStdCost]([ComponentPartID]) > 0 AND bom.[Qty] > 0), bom.[Component Part #], null) ), -- Commodity Code 002 : Commodity ID: 11
[Sum HR Devices] = SUM( IIF( ( bom.[Commodity Code] IN ('002') AND [dbo].[fn_getFactoryStdCost]([ComponentPartID]) > 0 AND bom.[Qty] > 0), bom.[Qty], null) ), -- Commodity Code 002 : Commodity ID: 11
[# of 3rd Party Devices] = COUNT( IIF( bom.[Commodity Code] IN ('007'), bom.[Component Part #], null) ), -- Commodity Code 007 : Commodity ID: 5
[Sum 3rd Party Devices] = SUM( IIF( ( bom.[Commodity Code] IN ('007') AND bom.[Qty] > 0), bom.[Qty], null) ), -- Commodity Code 007 : Commodity ID: 5
[# of Robots] = COUNT( IIF( ( bom.[Commodity Code] IN ('005') AND bom.[Make/Buy] = 'B' ), bom.[Component Part #], null) ), -- Commodity Code 005 : Commodity ID: 13
[Sum of Robots] = SUM( IIF( ( bom.[Commodity Code] IN ('005') AND bom.[Make/Buy] = 'B' ), bom.[Qty], null) ), -- Commodity Code 005 : Commodity ID: 13
[# of Make Parts] = COUNT( IIF( ( bom.[Make/Buy] = 'M' AND [dbo].[fn_getFactoryStdCost]([ComponentPartID]) > 0 AND bom.[Qty] > 0 ), bom.[Component Part #], null) ),
[# of Buy Parts] = COUNT( IIF( ( bom.[Make/Buy] = 'B' AND [dbo].[fn_getFactoryStdCost]([ComponentPartID]) > 0 AND bom.[Qty] > 0), bom.[Component Part #], null) ),
[# of Ref Parts] = COUNT( IIF( ( bom.[Make/Buy] = 'B' AND [dbo].[fn_getFactoryStdCost]([ComponentPartID]) = 0 ), bom.[Component Part #], null) )
FROM bomBreakdown bom
WHERE
bom.[ComponentPartID] IS NOT NULL AND
bom.[SourcePartID] = ap.[SourcePartID]
GROUP BY bom.[SourcePartID]
) oa2
ORDER BY p.[PART_X]
- AllPartsList has 1.3m Records
- E_Part has 20,500 Records
- bomBreakdown has 1.3m Records
Previously, in the Outer Apply all the queries except the first two had DISTINCT but I removed them. With the DISTINCT the query ran fine, the numbers were just off.
I don't believe this is a data/table layout issue, I think there may be a cross join or something that I'm missing.