我有一个类似于下面的表(下面的代码创建一个名为#Temp的表。这有160,000行,这与我在真实数据集中使用的行数大致相同,但真实数据集中有更多列):
/* Create dummy employees*/
;WITH employeeNumbers
AS ( SELECT 1 AS employeeId
UNION ALL
SELECT employeeNumbers.employeeId + 1
FROM employeeNumbers
WHERE employeeNumbers.employeeId < 16000 )
SELECT *
INTO #employeeId
FROM employeeNumbers
OPTION ( MAXRECURSION 16000 )
/*Create saleItems*/
CREATE TABLE #SalesItems
(
category VARCHAR(100)
, subCategory VARCHAR(100)
, productName VARCHAR(1000)
)
INSERT INTO #SalesItems ( category
, subCategory
, productName )
VALUES ( 'Furniture', 'Bookcases', 'Bush Somerset Collection Bookcase' )
, ( 'Furniture', 'Chairs', 'Hon Deluxe Fabric Upholstered Stacking Chairs, Rounded Back' )
, ( 'Office Supplies', 'Labels', 'Self-Adhesive Address Labels for Typewriters by Universal' )
, ( 'Furniture', 'Tables', 'Bretford CR4500 Series Slim Rectangular Table' )
, ( 'Office Supplies', 'Storage', 'Eldon Fold n Roll Cart System' )
, ( 'Furniture', 'Furnishings', 'Eldon Expressions Wood and Plastic Desk Accessories, Cherry Wood' )
, ( 'Office Supplies', 'Art', 'Newell 322' )
, ( 'Technology', 'Phones', 'Mitel 5320 IP Phone VoIP phone' )
, ( 'Office Supplies', 'Binders', 'DXL Angle-View Binders with Locking Rings by Samsill' )
, ( 'Technology', 'Phones', 'Samsung Galaxy S8' )
-- Create some random sales figures between 10 and 100
SELECT employeeId
, category
, subCategory
, productName
, CONVERT(DECIMAL(13, 2), 10 + ( 100 - 10 ) * RAND(CHECKSUM(NEWID()))) [Jul 2017]
, CONVERT(DECIMAL(13, 2), 10 + ( 100 - 10 ) * RAND(CHECKSUM(NEWID()))) [Aug 2017]
, CONVERT(DECIMAL(13, 2), 10 + ( 100 - 10 ) * RAND(CHECKSUM(NEWID()))) [Sep 2017]
, CONVERT(DECIMAL(13, 2), 10 + ( 100 - 10 ) * RAND(CHECKSUM(NEWID()))) [Oct 2017]
, CONVERT(DECIMAL(13, 2), 10 + ( 100 - 10 ) * RAND(CHECKSUM(NEWID()))) [Nov 2017]
, CONVERT(DECIMAL(13, 2), 10 + ( 100 - 10 ) * RAND(CHECKSUM(NEWID()))) [Dec 2017]
INTO #Temp
FROM #employeeId
JOIN #SalesItems ON 1 = 1
CREATE INDEX empId
ON #Temp ( employeeId )
SELECT *
FROM #Temp
我正在做的是将这些结果转换为表中每个员工ID的单个json字符串。我的查询如下:
SELECT DISTINCT x.employeeId
, ( SELECT y.category
, y.subCategory
, y.productName
, [Jul 2017] AS 'salesAmounts.Jul 2017'
, [Aug 2017] AS 'salesAmounts.Aug 2017'
, [Sep 2017] AS 'salesAmounts.Sep 2017'
, [Oct 2017] AS 'salesAmounts.Oct 2017'
, [Nov 2017] AS 'salesAmounts.Nov 2017'
, [Dec 2017] AS 'salesAmounts.Dec 2017'
FROM #Temp y
WHERE y.employeeId = x.employeeId
FOR JSON PATH, INCLUDE_NULL_VALUES ) data
FROM #Temp x
哪个有效,但它的表现并不好。在此示例中,执行此操作需要25秒,但在我的真实数据集中需要更长时间。返回#Temp表中的所有结果需要1秒钟。无论如何我可以在这里重新设计我的查询以改善查询时间吗?我确实尝试使用游标迭代每个employeeId并以这种方式生成json字符串,但它仍然很糟糕。
Read "Performance Surprises and Assumptions : GROUP BY vs. DISTINCT" by Aaron Bertrand
尝试使用GROUP BY
而不是DISTINCT
。在创建结果集之后,DISTINCT
会抛出重复项,从而比需要更频繁地调用JSON。 GROUP BY
应该首先将设置减少到不同的employeeId
值,并且每次只执行一次JSON。
目前无法测试,但这应该做同样的,只是更快:
SELECT x.employeeId
, ( SELECT y.category
, y.subCategory
, y.productName
, [Jul 2017] AS 'salesAmounts.Jul 2017'
, [Aug 2017] AS 'salesAmounts.Aug 2017'
, [Sep 2017] AS 'salesAmounts.Sep 2017'
, [Oct 2017] AS 'salesAmounts.Oct 2017'
, [Nov 2017] AS 'salesAmounts.Nov 2017'
, [Dec 2017] AS 'salesAmounts.Dec 2017'
FROM #Temp y
WHERE y.employeeId = x.employeeId
FOR JSON PATH, INCLUDE_NULL_VALUES ) data
FROM #Temp x
GROUP BY x.EmployeeId