我有一个表变量,我使用“Insert Into - Select”语句插入一些值。 select是几个连接的组合,当它单独执行时需要3秒。问题是整个代码需要3-4分钟才能执行。我想知道这有什么特别的原因。
这是我的表变量声明:
DECLARE @Result TABLE
(
ProductID NVARCHAR(25) PRIMARY KEY
,ProductName NVARCHAR(100)
,ProductCategoryID TINYINT
,ProductCategory NVARCHAR(50)
,ProductSubCategoryID TINYINT
,ProductSubCategory NVARCHAR(50)
,BrandID TINYINT
,Brand NVARCHAR(50)
)
我有一个其他的表变量,我用一些数据初始化,这是它的结构:
DECLARE @TempTable TABLE
(
ProtoSurveyID INT,
ProductID NVARCHAR(25) PRIMARY KEY
)
以下代码是我的问题陈述(插入 - 选择):
INSERT INTO @Result (ProductID,ProductName,ProductCategoryID,ProductCategory,ProductSubCategoryID,ProductSubCategory,BrandID,Brand)
SELECT
Products.ProductID AS ProductID
,Products.ProductName AS ProductName
,ProductCategories.ProductCategoryID AS ProductCategoryID
,ProductCategories.ProductCategory AS ProductCategory
,ProductSubCategories.ProductSubCategoryID AS ProductSubCategoryID
,ProductSubCategories.ProductSubCategory AS ProductSubCategory
,Brands.BrandID AS BrandID
,Brands.Brand AS Brand
FROM
(
SELECT
CAST(A.Col001 AS tinyint) AS ProductCategoryID
,CAST(A.Col002 AS tinyint) AS BrandID
,CAST(A.Col003 AS nvarchar(25)) AS ProductID
,CAST(A.Col004 AS nvarchar(100)) AS ProductName
,CAST(A.Col006 As tinyint) AS ProductSubCategoryID
,B.ProtoSurveyID
FROM DataSetsMaterializedDataSqlvariant A
INNER JOIN @TempTable B
ON B.ProductID=CAST(A.Col003 AS nvarchar(25))
WHERE DataSetsMaterializedInternalRowsetID = 3
) Products
INNER JOIN
(
SELECT CAST(A.Col001 AS tinyint) AS BrandID
, CAST(A.Col002 AS nvarchar(50)) AS Brand
FROM DataSetsMaterializedDataSqlvariant A
WHERE DataSetsMaterializedInternalRowsetID = 1
)Brands On Products.BrandID=Brands.BrandID
INNER JOIN
(
SELECT CAST(A.Col001 AS tinyint) AS ProductCategoryID
,CAST(A.Col002 AS nvarchar(50)) AS ProductCategory
FROM DataSetsMaterializedDataSqlvariant A
WHERE DataSetsMaterializedInternalRowsetID = 2
) ProductCategories On Products.ProductCategoryID=ProductCategories.ProductCategoryID
INNER JOIN
(
SELECT CAST(A.Col001 AS tinyint) AS ProductSubCategoryID
, CAST(A.Col002 AS nvarchar(50)) AS ProductSubCategory
FROM DataSetsMaterializedDataSqlvariant A
WHERE DataSetsMaterializedInternalRowsetID = 11
) ProductSubCategories on Products.ProductSubCategoryID=ProductSubCategories.ProductSubCategoryID
正如我之前所说,如果我评论插入行,查询需要3秒,否则 - 很长时间。
编辑:这是我的执行计划 - 大部分成本都是表扫描,但是为什么插入生成需要这么多时间,如果没有它就会快速发生?
以下是我的新内联函数:
CREATE FUNCTION [dbo].[fn_XxCustom_RetailAudits_GetProductsForFilter]
(
@SecurityObjectUserID BIGINT
)
RETURNS TABLE
AS
RETURN
WITH CTE(ProtoSurveyID,ProductID) AS
(
SELECT DISTINCT CAST(B.ProtoSurveyID AS INT)
,CAST(A.Col002 AS NVARCHAR(25)) AS ProductID
FROM DataSetsMaterializedDataSqlvariant A
JOIN SurveyInstances B ON A.Col001=B.SurveyInstanceID AND CAST(B.ProtoSurveyID AS INT) IN (SELECT ProtoSurveyID FROM dbo.fn_Filter_GetProtoSurveysAllowedShort(@SecurityObjectUserID, 'CLIENTACCESS',NULL))
WHERE DataSetsMaterializedInternalRowsetID = 5
)
SELECT
Products.ProductID AS ProductID
,Products.ProductName AS ProductName
,ProductCategories.ProductCategoryID AS ProductCategoryID
,ProductCategories.ProductCategory AS ProductCategory
,ProductSubCategories.ProductSubCategoryID AS ProductSubCategoryID
,ProductSubCategories.ProductSubCategory AS ProductSubCategory
,Brands.BrandID AS BrandID
,Brands.Brand AS Brand
FROM
(
SELECT
CAST(A.Col001 AS tinyint) AS ProductCategoryID
,CAST(A.Col002 AS tinyint) AS BrandID
,CAST(A.Col003 AS nvarchar(25)) AS ProductID
,CAST(A.Col004 AS nvarchar(100)) AS ProductName
,CAST(A.Col006 As tinyint) AS ProductSubCategoryID
,B.ProtoSurveyID
FROM CTE B
INNER JOIN DataSetsMaterializedDataSqlvariant A
ON B.ProductID=CAST(A.Col003 AS nvarchar(25))
WHERE DataSetsMaterializedInternalRowsetID = 3
) Products
INNER JOIN
(
SELECT CAST(A.Col001 AS tinyint) AS BrandID
,CAST(A.Col002 AS nvarchar(50)) AS Brand
FROM DataSetsMaterializedDataSqlvariant A
WHERE DataSetsMaterializedInternalRowsetID = 1
)Brands On Products.BrandID=Brands.BrandID
INNER JOIN
(
SELECT CAST(A.Col001 AS tinyint) AS ProductCategoryID
,CAST(A.Col002 AS nvarchar(50)) AS ProductCategory
FROM DataSetsMaterializedDataSqlvariant A
WHERE DataSetsMaterializedInternalRowsetID = 2
) ProductCategories On Products.ProductCategoryID=ProductCategories.ProductCategoryID
INNER JOIN
(
SELECT CAST(A.Col001 AS tinyint) AS ProductSubCategoryID
,CAST(A.Col002 AS nvarchar(50)) AS ProductSubCategory
FROM DataSetsMaterializedDataSqlvariant A
WHERE DataSetsMaterializedInternalRowsetID = 11
) ProductSubCategories on Products.ProductSubCategoryID=ProductSubCategories.ProductSubCategoryID
GO
我又慢慢跑。任何想法如何优化它?
答案 0 :(得分:11)
插入表变量的查询不能有并行计划。
尝试使用#temp
表来允许SELECT
并行化。