SQL Server 子查询/CTE 建议
Posted
技术标签:
【中文标题】SQL Server 子查询/CTE 建议【英文标题】:SQL Server subquery/CTE advice 【发布时间】:2021-02-22 10:23:52 【问题描述】:我有一个运行不到 1 秒的 SQL 查询:
SELECT
GB_BR_NAME AS 'Branch',
VM_VT_DESC AS 'VType',
S.VM_SK_VEHICLE AS 'Vehicle',
VM_SK_SEQUENCE AS 'Vehicle Sequence',
CASE
WHEN VM_VT_NU = 'N' THEN VM_SK_CONSTOTAL
WHEN VM_VT_NU = 'U' THEN VM_SK_ADJSTOCK
END AS 'Stock Value',
VM_SK_CONSTOTAL AS 'Cons Total',
VM_SK_ADJSTOCK AS 'Adj Total',
DD_MA_DESC AS 'Manufacturer',
DD_CL_DESCRIPTION AS 'Variant Class',
VM_VT_NU AS 'New/Used',
CASE
WHEN VM_EN_PROGRESS = 'Inv' THEN 'Y'
WHEN VM_EN_PROGRESS = 'Con' THEN 'Y'
ELSE 'N'
END AS 'Sold',
VM_SK_INTFREEDATE AS 'Interest Free Date',
CASE
WHEN VM_SK_INTFREEDATE <= GETDATE() AND DD_MA_DESC NOT IN ('Toyota', 'Skoda', 'Lexus', 'Honda') THEN 1
ELSE 0
END AS 'Interest Bearing',
VM_SK_STATUS AS 'Status'
FROM
VM_01_STOCK S
LEFT OUTER JOIN GB_00_BRANC B ON S.VM_SK_BRANCH = B.GB_BR_BRANCH
LEFT OUTER JOIN VM_01_VTYPE V ON S.VM_SK_VTYPE = V.VM_VT_VTYPE
LEFT OUTER JOIN VM_00_VEHICLE VE ON S.VM_SK_VEHICLE = VE.VM_VE_VEHICLE
LEFT OUTER JOIN DD_00_MANUF DDM ON VE.VM_VE_MANUFACT = DDM.DD_MA_MANUFACT
LEFT OUTER JOIN VM_01_SORDER SO ON S.VM_SK_VEHICLE = SO.VM_SO_VEHICLE AND S.VM_SK_SEQUENCE = SO.VM_SO_SEQUENCE
LEFT OUTER JOIN DD_00_VARCLASS CL ON VE.VM_VE_VARCLASS = CL.DD_CL_CODE
LEFT OUTER JOIN VM_01_ENQUIRY E ON S.VM_SK_ENQUIRY = E.VM_EN_ENQUIRY
WHERE
(VM_SK_STATUS = 'A' OR VM_SK_STATUS = 'C')
这会输出 ~8000 行。
然后我有一个输出约 40 行的 CTE。这也运行不到 1 秒 -
WITH cte (VM_SK_VEHICLE) AS
(SELECT VM_SK_VEHICLE
FROM VM_01_STOCK SS LEFT OUTER JOIN VM_01_VTYPE V ON SS.VM_SK_VTYPE = V.VM_VT_VTYPE LEFT OUTER JOIN VM_00_VEHICLE VE ON SS.VM_SK_VEHICLE = VE.VM_VE_VEHICLE LEFT OUTER JOIN DD_00_VARCLASS CL ON VE.VM_VE_VARCLASS = CL.DD_CL_CODE LEFT OUTER JOIN NL_01_TRANS T ON LTRIM(RTRIM(str(VM_SK_VEHICLE))) + LTRIM(RTRIM(str(VM_SK_SEQUENCE))) = NL_TR_SUFFIX LEFT OUTER JOIN PL_01_TRANS PT ON NL_TR_DAYBOOK = PL_TX_DBNUM AND NL_TR_ITEM = PL_TX_DBITEM LEFT OUTER JOIN VM_01_VSPEC VP ON VM_SK_VEHICLE = VM_VS_VEHICLE
WHERE (VM_SK_STATUS = 'A' OR VM_SK_STATUS = 'C') AND VM_VT_NU = 'N' AND DD_CL_CODE IN ('CAR', 'LCV', 'HGV') AND VM_VS_DESC = 'Vehicle Price' AND VM_VS_ACTCOST <> 0.00
GROUP BY VM_SK_VEHICLE, VM_SK_SEQUENCE, VM_SK_CONSTOTAL
HAVING SUM(PL_TX_BASEVAL - PL_TX_BASEPAID) < (VM_SK_CONSTOTAL/2))
当我添加到我的主查询时
CASE
WHEN S.VM_SK_VEHICLE IN (SELECT cte.VM_SK_VEHICLE FROM cte) THEN 1
ELSE 0
END AS 'Fully Paid',
开始显示结果大约需要 15 分钟。我正在执行 LEFT JOIN 以将 CTE 加入我的主查询。
我还尝试使用子查询进行此操作,大约需要 37 分钟才能开始显示结果。
我需要做什么才能让它以适当的速度运行?
谢谢。
【问题讨论】:
检查您的执行计划并查看花费的时间以及是否缺少索引。然而,有时可以通过转换为将执行计划拆分为 2 的临时表来优化 CTE。 【参考方案1】:这个谓词似乎出现了问题:
LTRIM(RTRIM(STR(VM_SK_VEHICLE))) + LTRIM(RTRIM(STR(VM_SK_SEQUENCE))) = NL_TR_SUFFIX
这根本不是 sargable。
用这个表达式创建一个计算的持久列:
ALTER TABLE ??? ADD VM_SK_VEHICLE_SEQUENCE
AS LTRIM(RTRIM(STR(VM_SK_VEHICLE))) + LTRIM(RTRIM(STR(VM_SK_SEQUENCE))) PERSISTED
如果您使用的是最新版本的 SQL Server,请将“LTRIM(RTRIM(”) 替换为“TRIM(”
另外,为连接部分创建一个新索引...
【讨论】:
您好 SQLpro,我已将修剪后的 concat 添加为计算列,并为这个新列和 NL_TR_SUFFIX 编制索引,并将执行时间减少到 ~1min30!这是一个了不起的结果,谢谢!现在“执行计划”中“成本最高”的部分是 Row Count Spool (Lazy Spool),占 37%。我还可以实施其他优化吗? 要做到这一点,我需要所有表的完整 DDL 和索引的完整列表...当然查询是对的....但是今天我没有时间备用吧!以上是关于SQL Server 子查询/CTE 建议的主要内容,如果未能解决你的问题,请参考以下文章
sql server使用cte递归查询获取树形的父节点/子节点