索引视图可用性
Posted
技术标签:
【中文标题】索引视图可用性【英文标题】:Indexed view usability 【发布时间】:2015-04-01 11:43:06 【问题描述】:我有一个索引视图:
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 会生成此查询,而我不想编辑多维模型。有人知道怎么做吗?也许优化查询的唯一方法是删除派生表(意味着我必须编辑多维模型)?任何建议(查看编辑和最终查询编辑)将不胜感激。
【问题讨论】:
您是否考虑过仅在派生表查询上创建索引视图? 我试过了..没用 您运行的是企业版吗? 是的。它是企业版:基于内核的许可(64 位) 我继续并发布了一个使用视图索引的复制脚本的答案。重要的是,年份是视图索引的第一列,以方便查询中的JOIN
子句。
【参考方案1】:
下面的示例在我的测试中使用了索引视图。如果这在您的环境中不起作用,则您发布的代码中可能存在不明显的差异。
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
【讨论】:
是的,我在尝试创建可用的索引视图时犯了一个错误。可以,谢谢。以上是关于索引视图可用性的主要内容,如果未能解决你的问题,请参考以下文章
数据库2生成txt/xml文件,ftp,oracle安装/表操作/虚表/日期/序列,索引/视图/链路/同义词/高可用性,mysql,文件入库/清理/结构设计/交换/收集