日期:2014-05-18  浏览次数:20388 次

能在视图中建立索引的吧,如何创建?
能在视图中建立索引的吧,如何创建?

CREATE   VIEW   dbo.V_MemberLists  

AS
SELECT   dbo.t_Member_Member.ID,   dbo.t_Member_Member.UserName,  
            dbo.t_Member_Member.Name,   ISNULL(dbo.t_Member_Member.csMoney,   0)  
            AS   csMoney,   ISNULL(dbo.t_Member_Member.getMoney,   0)   AS   getMoney,  
            dbo.t_Member_Member.Code1,   dbo.t_Member_Member.CodeAll   AS   cCode,  
            dbo.CheckStatusName(dbo.t_Member_Info.CheckStatus)   AS   CheckStatus,  
            dbo.MemberStatusName(dbo.t_Member_Info.MemberStatus)   AS   MemberStatus,  
            dbo.t_Member_Info.RegisterTime,   dbo.t_Member_Info.BeginTime,  
            dbo.t_Member_Info.EndTime,   ISNULL(dbo.t_Member_Level.Name,   '无 ')  
            AS   LevelName,   dbo.t_Member_Level.ID   AS   LevelID,  
            dbo.t_Member_Level.ParentID
FROM   dbo.t_Member_Member   INNER   JOIN  
            dbo.t_Member_Info   ON  
            dbo.t_Member_Member.ID   =   dbo.t_Member_Info.MemberID   INNER   JOIN
            dbo.t_Member_Level   ON   dbo.t_Member_Info.LevelID   =   dbo.t_Member_Level.ID


创建索引时
create   unique   clustered   index   vname   on     V_MemberLists

但是提示如下:
无法在视图   'V_MemberLists '   上创建   索引,因为该视图未绑定到架构。


------解决方案--------------------
唯一索引不行,其他的可以,但是需要重新建立试图

CREATE VIEW dbo.V_MemberLists
WITH SCHEMABINDING --必须加这个
AS
SELECT dbo.t_Member_Member.ID, dbo.t_Member_Member.UserName,
dbo.t_Member_Member.Name, ISNULL(dbo.t_Member_Member.csMoney, 0)
AS csMoney, ISNULL(dbo.t_Member_Member.getMoney, 0) AS getMoney,
dbo.t_Member_Member.Code1, dbo.t_Member_Member.CodeAll AS cCode,
dbo.CheckStatusName(dbo.t_Member_Info.CheckStatus) AS CheckStatus,
dbo.MemberStatusName(dbo.t_Member_Info.MemberStatus) AS MemberStatus,
dbo.t_Member_Info.RegisterTime, dbo.t_Member_Info.BeginTime,
dbo.t_Member_Info.EndTime, ISNULL(dbo.t_Member_Level.Name, '无 ')
AS LevelName, dbo.t_Member_Level.ID AS LevelID,
dbo.t_Member_Level.ParentID
FROM dbo.t_Member_Member INNER JOIN
dbo.t_Member_Info ON
dbo.t_Member_Member.ID = dbo.t_Member_Info.MemberID INNER JOIN
dbo.t_Member_Level ON dbo.t_Member_Info.LevelID = dbo.t_Member_Level.ID


go

create index vname on V_MemberLists(字段列表)
go

------解决方案--------------------
帮助的文字,参考一下:

要建立索引的视图的名称。必须使用 SCHEMABINDING 定义视图才能在视图上创建索引。视图定义也必须具有确定性。如果选择列表中的所有表达式、WHERE 和 GROUP BY 子句都具有确定性,则视图也具有确定性。而且,所有键列必须是精确的。只有视图的非键列可能包含浮点表达式(使用 float 数据类型的表达式),而且 float 表达式不能在视图定义的其它任何位置使用。

若要在确定性视图中查找列,请使用 COLUMNPROPERTY 函数(IsDeterministic 属性)。该函数的 IsPrecise 属性可用来确定键列是否精确。

必须先为视图创建唯一的聚集索引,才能为该视图创建非聚集索引。

在 SQL Server 企业版或开发版中,查询优化器可使用索引视图加快查询的执行速度。要使优化程序考虑将该视图作为替换,并不需要在查询中引用该视图。