gpt4 book ai didi

sql - 我可以提高 SQL Server 2016 中此 JSON 转换的性能吗?

转载 作者:行者123 更新时间:2023-12-03 02:09:02 24 4
gpt4 key购买 nike

我有一个类似于以下的表格(以下代码创建一个名为 #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 秒。无论如何,我可以在此处重新设计查询以缩短查询时间吗?我确实尝试使用游标来遍历每个员工 ID 并以这种方式生成 json 字符串,但它仍然很糟糕。

最佳答案

Read "Performance Surprises and Assumptions : GROUP BY vs. DISTINCT" by Aaron Bertrand

尝试使用GROUP BY而不是DISTINCTDISTINCT 在创建结果集之后丢弃重复项,从而比需要的更频繁地调用 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

关于sql - 我可以提高 SQL Server 2016 中此 JSON 转换的性能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49122031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com