无法在架构绑定视图上创建索引
Not able to create index on schema binding view
无法在以下架构绑定上创建索引 view.It 是从另一个视图 (v_prod_manu_sub
) 创建的。显示以下错误消息:
Cannot create index on view "dbo.V_PROD_MANU" because it references derived table "X" (defined by SELECT statement in FROM clause). Consider removing the reference to the derived table
or not indexing the view.
如何更改以下查询以创建 索引?
ALTER VIEW [dbo].[V_PROD_MANU] WITH SCHEMABINDING AS
SELECT X.PRODUCT, CAST(RIGHT(TEXT_CODE,LEN(F_TEXT_CODE)-1) AS VARCHAR(30)) AS TEXT_CODE,
CAST(SUBSTRING(RIGHT(PHRASE,LEN(F_PHRASES)-1),9,LEN(F_PHRASE)-3) AS varchar(700)) AS PHRASE
FROM (
SELECT V1.PRODUCT,
(SELECT ',' + V2.TEXT_CODE FROM dbo.V_PROD_MANU_SUB V2 WHERE V1.PRODUCT = V2.PRODUCT ORDER BY V2.F_COUNTER FOR XML PATH('')) AS TEXT_CODE,
(SELECT ' |par|par ' + V3.F_PHRASE FROM dbo.V_PROD_MANU_SUB V3 WHERE V1.PRODUCT = V3.PRODUCT ORDER BY V3.F_COUNTER FOR XML PATH('')) AS PHRASE
FROM dbo.V_PROD_MANU_SUB V1 GROUP BY V1.PRODUCT)X
输出:
Product TEXT_CODE PHRASE
00-021 MANU0043,MANU0050 Inc |par Pharmaceuticals Group |par 235 East 5nd Street |par usa |par 1-800-123-000
通常人们使用 STUFF()
来删除前导逗号,而不是这些混乱的转换和 LEN()
计算。例如:
SELECT V1.PRODUCT,
TEXT_CODE = STUFF
(
(
(SELECT ',' + V2.TEXT_CODE
FROM dbo.V_PROD_MANU_SUB AS V2
WHERE V1.PRODUCT = V2.PRODUCT
ORDER BY V2.F_COUNTER
FOR XML PATH(''),
TYPE).value('./text()[1]','nvarchar(max)')
),
1,1,N'')
FROM dbo.V_PROD_MANU_SUB AS V1
GROUP BY V1.PRODUCT;
-- much easier in SQL Server 2017 with STRING_AGG()
但这似乎与您首先需要具体化逗号分隔列表的原因无关,无论它是否有前导逗号。
索引视图通常是过早优化的一种形式。本质上你是在说,“查询这些数据的成本将远远大于维护它的成本。”你知道吗?如何?您的工作负载平衡是多少(读:写)?现在查询有多慢?它多久 运行 一次?更新需要多长时间?
如果您确实知道这一点,那么您会更幸运地通过触发器手动将其具体化为您自己的 table。由于各种原因,索引视图很可能成为死胡同。
无法在以下架构绑定上创建索引 view.It 是从另一个视图 (v_prod_manu_sub
) 创建的。显示以下错误消息:
Cannot create index on view "dbo.V_PROD_MANU" because it references derived table "X" (defined by SELECT statement in FROM clause). Consider removing the reference to the derived table or not indexing the view.
如何更改以下查询以创建 索引?
ALTER VIEW [dbo].[V_PROD_MANU] WITH SCHEMABINDING AS
SELECT X.PRODUCT, CAST(RIGHT(TEXT_CODE,LEN(F_TEXT_CODE)-1) AS VARCHAR(30)) AS TEXT_CODE,
CAST(SUBSTRING(RIGHT(PHRASE,LEN(F_PHRASES)-1),9,LEN(F_PHRASE)-3) AS varchar(700)) AS PHRASE
FROM (
SELECT V1.PRODUCT,
(SELECT ',' + V2.TEXT_CODE FROM dbo.V_PROD_MANU_SUB V2 WHERE V1.PRODUCT = V2.PRODUCT ORDER BY V2.F_COUNTER FOR XML PATH('')) AS TEXT_CODE,
(SELECT ' |par|par ' + V3.F_PHRASE FROM dbo.V_PROD_MANU_SUB V3 WHERE V1.PRODUCT = V3.PRODUCT ORDER BY V3.F_COUNTER FOR XML PATH('')) AS PHRASE
FROM dbo.V_PROD_MANU_SUB V1 GROUP BY V1.PRODUCT)X
输出:
Product TEXT_CODE PHRASE
00-021 MANU0043,MANU0050 Inc |par Pharmaceuticals Group |par 235 East 5nd Street |par usa |par 1-800-123-000
通常人们使用 STUFF()
来删除前导逗号,而不是这些混乱的转换和 LEN()
计算。例如:
SELECT V1.PRODUCT,
TEXT_CODE = STUFF
(
(
(SELECT ',' + V2.TEXT_CODE
FROM dbo.V_PROD_MANU_SUB AS V2
WHERE V1.PRODUCT = V2.PRODUCT
ORDER BY V2.F_COUNTER
FOR XML PATH(''),
TYPE).value('./text()[1]','nvarchar(max)')
),
1,1,N'')
FROM dbo.V_PROD_MANU_SUB AS V1
GROUP BY V1.PRODUCT;
-- much easier in SQL Server 2017 with STRING_AGG()
但这似乎与您首先需要具体化逗号分隔列表的原因无关,无论它是否有前导逗号。
索引视图通常是过早优化的一种形式。本质上你是在说,“查询这些数据的成本将远远大于维护它的成本。”你知道吗?如何?您的工作负载平衡是多少(读:写)?现在查询有多慢?它多久 运行 一次?更新需要多长时间?
如果您确实知道这一点,那么您会更幸运地通过触发器手动将其具体化为您自己的 table。由于各种原因,索引视图很可能成为死胡同。