1、 优化前的视图(执行select * from Query_NoEmployRegist用时127s)
代码 1 SET ANSI_NULLS ON 2 GO 3 SET QUOTED_IDENTIFIER ON 4 GO 5 ALTER VIEW [dbo].[Query_NoEmployRegist] 6 AS 7 SELECT dbo.Person_BasicInfo.*, dbo.Graduater_GraduaterRegist.RegistNO AS RegistNO, 8 dbo.Graduater_GraduaterRegist.RegistTime AS BaoDaoTime, 9 dbo.Graduater_GraduaterRegist.RegistMan AS RegistMan, 10 dbo.Graduater_Business.ComeFrom AS ComeFrom, 11 dbo.Graduater_Business.Code AS Code, dbo.Graduater_Business.Status AS Status, 12 dbo.Graduater_Business.ApPRoveResult AS ApproveResult, 13 dbo.Graduater_Business.NewCorp AS NewCorp, 14 dbo.Graduater_Business.CommendNumber AS CommendNumber, 15 dbo.Graduater_Business.EmployStatus AS EmployStatus, 16 dbo.Graduater_Business.NewCommendTime AS NewCommendTime, 17 dbo.Graduater_Business.GetSource AS GetSource, 18 dbo.Graduater_Business.EmployTime AS EmployTime, 19 dbo.Graduater_Business.Job AS Job, dbo.Graduater_Business.FillMan AS FillMan, 20 dbo.Graduater_Business.FillTime AS FillTime, 21 dbo.Graduater_Business.IsCommendOK AS IsCommendOK, 22 dbo.Graduater_Business.ApproveUser AS ApproveUser, 23 dbo.Graduater_Business.ApproveTime AS ApproveTime, 24 dbo.Graduater_Business.RegistTime AS RegistTime, 25 dbo.Graduater_Business.EmployCorp AS EmployCorp, 26 dbo.Graduater_Business.JobRemark AS JobRemark, 31 dbo.Person_Contact.Address AS Address, dbo.Person_Contact.Zip AS Zip, 32 dbo.Person_Contact.Telephone AS Telephone, dbo.Person_Contact.Mobile AS Mobile, 33 dbo.Person_Contact.Email AS Email, dbo.Person_Contact.IM AS IM, 34 dbo.Person_Skill.ForeignLanguage AS ForeignLanguage, 35 dbo.Person_Skill.ForeignLanguageLevel AS ForeignLanguageLevel, 36 dbo.Person_Skill.CantoneseLevel AS CantoneseLevel, 37 dbo.Person_Skill.MandarinLevel AS MandarinLevel, 38 dbo.Person_Skill.Language AS Language, 39 dbo.Person_Skill.TechnicalTitle AS TechnicalTitle, 40 dbo.Person_Skill.ComputerLevel AS ComputerLevel, 41 dbo.Person_EmployPurpose.JobType AS JobType, 42 dbo.Person_EmployPurpose.Vocation AS Vocation, 43 dbo.Person_EmployPurpose.JobPlace AS JobPlace, 44 dbo.Person_EmployPurpose.Salary AS Salary, 45 dbo.Person_EmployPurpose.OnJobDate AS OnJobDate, 46 dbo.Person_EmployPurpose.CorpType AS CorpType, 49 dbo.Graduater_Business.EmployType AS EmployType, 50 dbo.Graduater_Business.EmployTypeCode AS EmployTypeCode, 51 dbo.Graduater_Business.EmployCorpType AS EmployCorpType 56 FROM dbo.Person_BasicInfo INNER JOIN 57 dbo.Graduater_Business ON 58 dbo.Person_BasicInfo.PersonID = dbo.Graduater_Business.PersonID LEFT OUTER JOIN 59 dbo.Graduater_GraduaterRegist ON 60 dbo.Graduater_Business.GradBusinessID = dbo.Graduater_GraduaterRegist.GraduaterGUID 61 INNER JOIN 62 dbo.Person_Contact ON 63 dbo.Person_BasicInfo.PersonID = dbo.Person_Contact.PersonID INNER JOIN 64 dbo.Person_Skill ON 65 dbo.Person_BasicInfo.PersonID = dbo.Person_Skill.PersonID INNER JOIN 66 dbo.Person_EmployPurpose ON 67 dbo.Person_BasicInfo.PersonID = dbo.Person_EmployPurpose.PersonID 68 GO 69 SET ANSI_NULLS OFF 70 GO 71 SET QUOTED_IDENTIFIER OFF 72 GO 2、 优化后的视图(执行select * from Query_NoEmployRegist用时98s)
代码 1 SET ANSI_NULLS on 2 GO 3 SET QUOTED_IDENTIFIER on 4 GO 5 ALTER VIEW [dbo].[Query_NoEmployRegist] 6 AS 7 SELECT 8 dbo.Person_BasicInfo.PersonID, 9 dbo.Person_BasicInfo.IdentityID, 10 dbo.Person_BasicInfo.Name, 11 dbo.Person_BasicInfo.Sex, 12 dbo.Person_BasicInfo.Folk, 13 dbo.Person_BasicInfo.Politics, 14 dbo.Person_BasicInfo.Birthday, 15 dbo.Person_BasicInfo.StudentSource, 16 dbo.Person_BasicInfo.StudentSourceCode, 17 dbo.Person_BasicInfo.EduLevel, 18 dbo.Person_BasicInfo.EduLevelCode, 19 dbo.Person_BasicInfo.EduNumber, 20 dbo.Person_BasicInfo.Stature, 21 dbo.Person_BasicInfo.Avoirdupois, 22 dbo.Person_BasicInfo.MarriageStatus, 23 dbo.Person_BasicInfo.College, 24 dbo.Person_BasicInfo.GraduatedDate, 25 dbo.Person_BasicInfo.Train, 26 dbo.Person_BasicInfo.Major, 27 dbo.Person_BasicInfo.Degree, 28 dbo.Person_BasicInfo.DegreeCertificate, 29 dbo.Person_BasicInfo.StudyMode, 30 dbo.Graduater_GraduaterRegist.RegistNO AS RegistNO, 31 dbo.Graduater_GraduaterRegist.RegistTime AS BaoDaoTime, 32 dbo.Graduater_GraduaterRegist.RegistMan AS RegistMan, 33 dbo.Graduater_Business.ComeFrom AS ComeFrom, 34 dbo.Graduater_Business.Code AS Code, dbo.Graduater_Business.Status AS Status, 35 dbo.Graduater_Business.ApproveResult AS ApproveResult, 36 dbo.Graduater_Business.NewCorp AS NewCorp, 37 dbo.Graduater_Business.CommendNumber AS CommendNumber, 38 dbo.Graduater_Business.EmployStatus AS EmployStatus, 39 dbo.Graduater_Business.NewCommendTime AS NewCommendTime, 40 dbo.Graduater_Business.GetSource AS GetSource, 41 dbo.Graduater_Business.EmployTime AS EmployTime, 42 dbo.Graduater_Business.Job AS Job, dbo.Graduater_Business.FillMan AS FillMan, 43 dbo.Graduater_Business.FillTime AS FillTime, 44 dbo.Graduater_Business.IsCommendOK AS IsCommendOK, 45 dbo.Graduater_Business.ApproveUser AS ApproveUser, 46 dbo.Graduater_Business.ApproveTime AS ApproveTime, 47 dbo.Graduater_Business.RegistTime AS RegistTime, 48 dbo.Graduater_Business.EmployCorp AS EmployCorp, 49 dbo.Graduater_Business.JobRemark AS JobRemark, 54 dbo.Person_Contact.Address AS Address, dbo.Person_Contact.Zip AS Zip, 55 dbo.Person_Contact.Telephone AS Telephone, dbo.Person_Contact.Mobile AS Mobile, 56 dbo.Person_Contact.Email AS Email, dbo.Person_Contact.IM AS IM, 57 dbo.Person_Skill.ForeignLanguage AS ForeignLanguage, 58 dbo.Person_Skill.ForeignLanguageLevel AS ForeignLanguageLevel, 59 dbo.Person_Skill.CantoneseLevel AS CantoneseLevel, 60 dbo.Person_Skill.MandarinLevel AS MandarinLevel, 61 dbo.Person_Skill.Language AS Language, 62 dbo.Person_Skill.TechnicalTitle AS TechnicalTitle, 63 dbo.Person_Skill.ComputerLevel AS ComputerLevel, 64 dbo.Person_EmployPurpose.JobType AS JobType, 65 dbo.Person_EmployPurpose.Vocation AS Vocation, 66 dbo.Person_EmployPurpose.JobPlace AS JobPlace, 67 dbo.Person_EmployPurpose.Salary AS Salary, 68 dbo.Person_EmployPurpose.OnJobDate AS OnJobDate, 69 dbo.Person_EmployPurpose.CorpType AS CorpType, 72 dbo.Graduater_Business.EmployType AS EmployType, 73 dbo.Graduater_Business.EmployTypeCode AS EmployTypeCode, 74 dbo.Graduater_Business.EmployCorpType AS EmployCorpType, 79 FROM dbo.Person_BasicInfo INNER JOIN 80 dbo.Graduater_Business ON 81 dbo.Person_BasicInfo.PersonID = dbo.Graduater_Business.PersonID LEFT OUTER JOIN 82 dbo.Graduater_GraduaterRegist ON 83 dbo.Graduater_Business.GradBusinessID = dbo.Graduater_GraduaterRegist.GraduaterGUID 84 INNER JOIN 85 dbo.Person_Contact ON 86 dbo.Person_BasicInfo.PersonID = dbo.Person_Contact.PersonID INNER JOIN 87 dbo.Person_Skill ON 88 dbo.Person_BasicInfo.PersonID = dbo.Person_Skill.PersonID INNER JOIN 89 dbo.Person_EmployPurpose ON 90 dbo.Person_BasicInfo.PersonID = dbo.Person_EmployPurpose.PersonID 91 GO 92 SET ANSI_NULLS OFF 93 GO 94 SET QUOTED_IDENTIFIER OFF 95 GO 3、 创建索引的视图(执行select * from Query_NoEmployRegist用时51s)
代码 1 SET ANSI_NULLS on 2 GO 3 SET QUOTED_IDENTIFIER on 4 GO 5 6 ALTER VIEW [dbo].[Query_NoEmployRegist] 7 WITH SCHEMABINDING AS 8 SELECT 9 dbo.Person_BasicInfo.PersonID, 10 dbo.Person_BasicInfo.IdentityID, 11 dbo.Person_BasicInfo.Name, 12 dbo.Person_BasicInfo.Sex, 13 dbo.Person_BasicInfo.Folk, 14 dbo.Person_BasicInfo.Politics, 15 dbo.Person_BasicInfo.Birthday, 16 dbo.Person_BasicInfo.StudentSource, 17 dbo.Person_BasicInfo.StudentSourceCode, 18 dbo.Person_BasicInfo.EduLevel, 19 dbo.Person_BasicInfo.EduLevelCode, 20 dbo.Person_BasicInfo.EduNumber, 21 dbo.Person_BasicInfo.Stature, 22 dbo.Person_BasicInfo.Avoirdupois, 23 dbo.Person_BasicInfo.MarriageStatus, 24 dbo.Person_BasicInfo.College, 25 dbo.Person_BasicInfo.GraduatedDate, 26 dbo.Person_BasicInfo.Train, 27 dbo.Person_BasicInfo.Major, 28 dbo.Person_BasicInfo.Degree, 29 dbo.Person_BasicInfo.DegreeCertificate, 30 dbo.Person_BasicInfo.StudyMode, 31 dbo.Graduater_GraduaterRegist.RegistNO AS RegistNO, 32 dbo.Graduater_GraduaterRegist.RegistTime AS BaoDaoTime, 33 dbo.Graduater_GraduaterRegist.RegistMan AS RegistMan, 34 dbo.Graduater_Business.ComeFrom AS ComeFrom, 35 dbo.Graduater_Business.Code AS Code, dbo.Graduater_Business.Status AS Status, 36 dbo.Graduater_Business.ApproveResult AS ApproveResult, 37 dbo.Graduater_Business.NewCorp AS NewCorp, 38 dbo.Graduater_Business.CommendNumber AS CommendNumber, 39 dbo.Graduater_Business.EmployStatus AS EmployStatus, 40 dbo.Graduater_Business.NewCommendTime AS NewCommendTime, 41 dbo.Graduater_Business.GetSource AS GetSource, 42 dbo.Graduater_Business.EmployTime AS EmployTime, 43 dbo.Graduater_Business.Job AS Job, dbo.Graduater_Business.FillMan AS FillMan, 44 dbo.Graduater_Business.FillTime AS FillTime, 45 dbo.Graduater_Business.IsCommendOK AS IsCommendOK, 46 dbo.Graduater_Business.ApproveUser AS ApproveUser, 47 dbo.Graduater_Business.ApproveTime AS ApproveTime, 48 dbo.Graduater_Business.RegistTime AS RegistTime, 49 dbo.Graduater_Business.EmployCorp AS EmployCorp, 50 dbo.Graduater_Business.JobRemark AS JobRemark, 51 dbo.Person_Contact.Address AS Address, dbo.Person_Contact.Zip AS Zip, 52 dbo.Person_Contact.Telephone AS Telephone, dbo.Person_Contact.Mobile AS Mobile, 53 dbo.Person_Contact.Email AS Email, dbo.Person_Contact.IM AS IM, 54 dbo.Person_Skill.ForeignLanguage AS ForeignLanguage, 55 dbo.Person_Skill.ForeignLanguageLevel AS ForeignLanguageLevel, 56 dbo.Person_Skill.CantoneseLevel AS CantoneseLevel, 57 dbo.Person_Skill.MandarinLevel AS MandarinLevel, 58 dbo.Person_Skill.Language AS Language, 59 dbo.Person_Skill.TechnicalTitle AS TechnicalTitle, 60 dbo.Person_Skill.ComputerLevel AS ComputerLevel, 61 dbo.Person_EmployPurpose.JobType AS JobType, 62 dbo.Person_EmployPurpose.Vocation AS Vocation, 63 dbo.Person_EmployPurpose.JobPlace AS JobPlace, 64 dbo.Person_EmployPurpose.Salary AS Salary, 65 dbo.Person_EmployPurpose.OnJobDate AS OnJobDate, 66 dbo.Person_EmployPurpose.CorpType AS CorpType, 67 dbo.Person_EmployPurpose.Job AS RequireJob, 68 dbo.Graduater_Business.EmployType AS EmployType, 69 dbo.Graduater_Business.EmployTypeCode AS EmployTypeCode, 70 dbo.Graduater_Business.EmployCorpType AS EmployCorpType, 71 FROM dbo.Person_BasicInfo INNER JOIN 72 dbo.Graduater_Business ON 73 dbo.Person_BasicInfo.PersonID = dbo.Graduater_Business.PersonID INNER JOIN 74 dbo.Graduater_GraduaterRegist ON 75 dbo.Graduater_Business.GradBusinessID = dbo.Graduater_GraduaterRegist.GraduaterGUID 76 INNER JOIN 77 dbo.Person_Contact ON 78 dbo.Person_BasicInfo.PersonID = dbo.Person_Contact.PersonID INNER JOIN 79 dbo.Person_Skill ON 80 dbo.Person_BasicInfo.PersonID = dbo.Person_Skill.PersonID INNER JOIN 81 dbo.Person_EmployPurpose ON 82 dbo.Person_BasicInfo.PersonID = dbo.Person_EmployPurpose.PersonID 83 GO 84 CREATE UNIQUE CLUSTERED INDEX Query_NoEmployRegist_Ind 85 ON Query_NoEmployRegist(GraduatedDate, StudentSourceCode,RegistTime,ApproveTime,PrintTime,ComeFrom) 86 SET ANSI_NULLS ON 87 GO 88 SET QUOTED_IDENTIFIER ON 89 GO
看来还得优化,希望各位博友指点一下!
二、索引视图的学习总结
1、什么是索引视图?
在视图上创建唯一的聚集索引及非聚集索引,来提高最复杂的查询的数据访问性能。具有唯一的聚集索引的视图即为索引视图。从数据库管理系统 (DBMS) 的角度看来,视图是对数据(一种元数据类型)的一种描述。当创建了一个典型视图时,通过封装一个 SELECT 语句(定义一个结果集来表示为虚拟表)来定义元数据。当在另一个查询的 FROM 子句中引用视图时,将从系统目录检索该元数据,并替代该视图的引用扩展元数据。视图扩展之后,SQL Server 查询优化器会为执行查询编译一个执行计划。查询优化器会搜索针对某个查询的一组可能的执行计划,并根据对执行每个查询计划所需的实际时间的估计,选择所能找到的成本最低的计划。
SQL Server 查询优化器自动决定何时对给定的查询执行使用索引视图。不必在查询中直接引用视图以供优化器在查询执行计划中使用。所以,现有的应用程序可运用索引视图,而不用更改应用程序本身;只是必须创建索引视图。
优化器考虑事项
查询优化器通过考虑几个条件来决定索引视图能否涵盖整个或部分查询。这些条件对应查询中的一个 FROM 子句并由下列这几个部分组成:
• 查询 FROM 子句中的表必须是索引视图 FROM 子句中的表的超集。
• 查询中的联接条件必须是视图中的联接条件的超集。
• 查询中的聚合列必须可从视图中的聚合列的子集派生。
• 查询选择列表中的所有表达式必须可从视图选择列表或未包含在视图定义中的表派生。
• 如果与其他谓词所匹配的行的超集相匹配,那么该谓词将归入另一个谓词。例如,“T.a=10”归入“T.a=10 and T.b=20”。任何谓词都可归入其自身。视图中限 制表值的那部分谓词必须归入查询中限制相同表的那部分谓词。此外,必须以 SQL Server 可验证的方式实现这一点。
• 属于视图定义中的表的查询搜索条件谓词的所有列必须出现在下列视图定义的一项或多项中:
(1) 一个 GROUP BY 列表。
(2) 视图选择列表(如不存在 GROUP BY)。
(3) 视图定义中相同或等价的谓词。
情况 (1) 和 (2) 允许 SQL Server 对视图的列应用查询谓词,以便进一步限制视图的列。情况 (3) 比较特殊。在这种情况下,不需要对列进行筛选,因此该列不必出现在视图中。 如果查询不止包含一个 FROM 子句(子查询、派生表、UNION),优化器可能选择几个索引视图来处理查询,并将它们应用到不同 FROM 子句。