我之前发布过类似的内容,但现在我正在从一个不同的方向来处理这个问题,所以我提出了一个新问题。我希望这是好的。CTE加入时速度很慢
我一直在使用CTE创建基于父费用的费用总和。在SQL和细节可以看这里:
CTE Index recommendations on multiple keyed table
我不认为我缺少的CTE什么,但是当我用数据的一大桌(350万使用它我得到一个问题行)。
表tblChargeShare
包含我需要的一些其他信息,例如InvoiceID
,所以我将CTE放在视图vwChargeShareSubCharges
中,并将它加入到表中。
查询:
Select t.* from vwChargeShareSubCharges t
inner join
tblChargeShare s
on t.CustomerID = s.CustomerID
and t.MasterChargeID = s.ChargeID
Where s.ChargeID = 1291094
返回几毫秒的结果。
查询:
Select ChargeID from tblChargeShare Where InvoiceID = 1045854
返回1行:
1291094
但查询:
Select t.* from vwChargeShareSubCharges t
inner join
tblChargeShare s
on t.CustomerID = s.CustomerID
and t.MasterChargeID = s.ChargeID
Where InvoiceID = 1045854
需要2-3分钟才能运行。
我保存了执行计划并将它们加载到SQL Sentry中。为快速查询树看起来是这样的:
从慢查询的计划是:
我试图重建索引,贯穿优化顾问以及各种组合查询的子查询。只要联接包含PK以外的任何内容,查询就会很慢。
我也有类似的问题在这里:
SQL Server Query time out depending on Where Clause
其中用函数来做子行,而不是一个CTE的summimg。这是使用CTE重写以避免我现在遇到的同样问题。我已经阅读了答案中的答案,但我不明智 - 我阅读了关于提示和参数的一些信息,但我无法完成工作。我曾经认为使用CTE重写会解决我的问题。在具有几千行的tblCharge上运行时查询速度很快。
测试两个SQL 2008 R2和SQL 2012
编辑:
我都凝结着查询到一个单一的声明,但同样的问题仍然存在:
WITH RCTE AS
(
SELECT ParentChargeId, s.ChargeID, 1 AS Lvl, ISNULL(TotalAmount, 0) as TotalAmount, ISNULL(s.TaxAmount, 0) as TaxAmount,
ISNULL(s.DiscountAmount, 0) as DiscountAmount, s.CustomerID, c.ChargeID as MasterChargeID
from tblCharge c inner join tblChargeShare s
on c.ChargeID = s.ChargeID Where s.ChargeShareStatusID < 3 and ParentChargeID is NULL
UNION ALL
SELECT c.ParentChargeID, c.ChargeID, Lvl+1 AS Lvl, ISNULL(s.TotalAmount, 0), ISNULL(s.TaxAmount, 0), ISNULL(s.DiscountAmount, 0) , s.CustomerID
, rc.MasterChargeID
from tblCharge c inner join tblChargeShare s
on c.ChargeID = s.ChargeID
INNER JOIN RCTE rc ON c.PArentChargeID = rc.ChargeID and s.CustomerID = rc.CustomerID Where s.ChargeShareStatusID < 3
)
Select MasterChargeID as ChargeID, rcte.CustomerID, Sum(rcte.TotalAmount) as TotalCharged, Sum(rcte.TaxAmount) as TotalTax, Sum(rcte.DiscountAmount) as TotalDiscount
from RCTE inner join tblChargeShare s on rcte.ChargeID = s.ChargeID and RCTE.CustomerID = s.CustomerID
Where InvoiceID = 1045854
Group by MasterChargeID, rcte.CustomerID
GO
编辑: 更多玩耍,我只是不明白这一点。
该查询即时(2MS):
Select t.* from
vwChargeShareSubCharges t
Where t.MasterChargeID = 1291094
而这需要3分:
DECLARE @ChargeID int = 1291094
Select t.* from
vwChargeShareSubCharges t
Where t.MasterChargeID = @ChargeID
即使我把号堆在 “在”,查询还是瞬间:
Where t.MasterChargeID in (1291090, 1291091, 1291092, 1291093, 1291094, 1291095, 1291096, 1291097, 1291098, 1291099, 129109)
编辑2:
我可以从头开始使用这个例子中的数据复制这样的:
我创建了一些虚拟数据复制的问题。它不是那么显著,因为我只加了100,000行,但坏的执行计划仍然发生(在SQLCMD模式下运行):
CREATE TABLE [tblChargeTest](
[ChargeID] [int] IDENTITY(1,1) NOT NULL,
[ParentChargeID] [int] NULL,
[TotalAmount] [money] NULL,
[TaxAmount] [money] NULL,
[DiscountAmount] [money] NULL,
[InvoiceID] [int] NULL,
CONSTRAINT [PK_tblChargeTest] PRIMARY KEY CLUSTERED
(
[ChargeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
Insert into tblChargeTest
(discountAmount, TotalAmount, TaxAmount)
Select ABS(CHECKSUM(NewId())) % 10, ABS(CHECKSUM(NewId())) % 100, ABS(CHECKSUM(NewId())) % 10
GO 100000
Update tblChargeTest
Set ParentChargeID = (ABS(CHECKSUM(NewId())) % 60000) + 20000
Where ChargeID = (ABS(CHECKSUM(NewId())) % 20000)
GO 5000
CREATE VIEW [vwChargeShareSubCharges] AS
WITH RCTE AS
(
SELECT ParentChargeId, ChargeID, 1 AS Lvl, ISNULL(TotalAmount, 0) as TotalAmount, ISNULL(TaxAmount, 0) as TaxAmount,
ISNULL(DiscountAmount, 0) as DiscountAmount, ChargeID as MasterChargeID
FROM tblChargeTest Where ParentChargeID is NULL
UNION ALL
SELECT rh.ParentChargeID, rh.ChargeID, Lvl+1 AS Lvl, ISNULL(rh.TotalAmount, 0), ISNULL(rh.TaxAmount, 0), ISNULL(rh.DiscountAmount, 0)
, rc.MasterChargeID
FROM tblChargeTest rh
INNER JOIN RCTE rc ON rh.PArentChargeID = rc.ChargeID --and rh.CustomerID = rc.CustomerID
)
Select MasterChargeID, ParentChargeID, ChargeID, TotalAmount, TaxAmount, DiscountAmount , Lvl
FROM RCTE r
GO
然后运行这两个查询:
--Slow Query:
Declare @ChargeID int = 60900
Select *
from [vwChargeShareSubCharges]
Where MasterChargeID = @ChargeID
--Fast Query:
Select *
from [vwChargeShareSubCharges]
Where MasterChargeID = 60900
只是快速的想法...如果你把你的查询'从vwChargeShareSubCharges t'选择吨。*,其实际TSQL定义替换视图'vwChargeShareSubCharges',加入到其他表酌情和运行查询,它有什么更快? – DMason
完全相同,如果我粘贴在完整的CTE。 – Molloch