我有一个索引视图:
CREATE VIEW ptv.vw_mokiniai_2
WITH SCHEMABINDING
AS
SELECT
T1.year_name,
T2.person_id,
T2.year,
T2.person_position,
COUNT_BIG(*) AS [aggregated number]
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.year = T2.year
GROUP BY T1.year_name,
T2.person_id,
T2.year,
T2.person_position
列上有唯一的聚簇索引
T1.year_name, T2.person_id, T2.year, T2.person_position
我需要查询优化器在查询
上使用此索引视图SELECT
T1.year_name,
COUNT_BIG(DISTINCT T3.person_id) AS [persons_per_year]
FROM Table1 AS T1
INNER JOIN (
SELECT
T2.person_id,
T2.year,
T2.person_position
FROM Table2 AS T2
GROUP BY T2.person_id, T2.year, T2.person_position
) AS T3
ON T1.year = T3.year
GROUP BY T1.year_name
查询必须是派生表,因为COGNOS生成此查询,我不想编辑多维模型。有人知道怎么办?也许优化查询的唯一方法是删除派生表(意味着我必须编辑多维模型)?任何建议(视图编辑和最终查询编辑)将不胜感激。
答案 0 :(得分:0)
以下示例使用我的测试中的索引视图。如果这在您的环境中不起作用,那么您发布的代码中可能存在差异。
CREATE TABLE dbo.Table1
(
year varchar(10)
, year_name varchar(10)
);
CREATE TABLE dbo.Table2
(
year varchar(10)
, person_id varchar(10)
, person_position varchar(10)
);
GO
CREATE VIEW dbo.vw_mokiniai_2
WITH SCHEMABINDING
AS
SELECT T2.person_id
, T2.year
, T2.person_position
, COUNT_BIG(*) AS [aggregated number]
FROM dbo.Table2 AS T2
GROUP BY T2.person_id
, T2.year
, T2.person_position
GO
CREATE UNIQUE CLUSTERED INDEX cdx ON vw_mokiniai_2(year, person_id, person_position)
GO
SELECT
T1.year_name,
COUNT_BIG(DISTINCT T3.person_id) AS [persons_per_year]
FROM Table1 AS T1
INNER JOIN (
SELECT
T2.person_id,
T2.year,
T2.person_position
FROM Table2 AS T2
GROUP BY T2.person_id, T2.year, T2.person_position
) AS T3
ON T1.year = T3.year
GROUP BY T1.year_name
GO