SQLServer中使用索引视图(物化视图)

物化视图:以前用的普通的视图,普通视图就是一段逻辑语句,对性能没有任何的提升,也不能创建索引,而物化视图会把视图里查询出来的数据在数据库上建立快照,它和物理表一样,可以创建 索引,主键约束等等,性能会有质的提升,但是其有缺点,会占用,可以设置它定时自动更新一次,也可以手动更新,当然也是可以设置及时更新的,但是会拉慢基表的增删改查操作,在这里我只讲思路,具体的话大家可以自己去研究。

--创建物化视图,每天晚上22:00:00自动更新
create materialized view VM_PSNPERSONINFO 
refresh force on demand start with sysdate next to_date( concat( to_char( sysdate+1,'dd-mm-yyyy'),' 22:00:00'),'dd-mm-yyyy hh24:mi:ss')
as
SELECT dt.personid, c.employeeid,d.unitcode,d.unitname,d.label,
       dt.unitid,dt.startdate,dt.enddate
  FROM (
         SELECT DISTINCT b.personnelid personid,SUBSTR (a.effectdate, 1, 10) startdate,
         MIN (CASE WHEN a.effectdate < b.effectdate THEN b.effectdate ELSE N'9999-99-99' END) AS enddate,
         MIN (a.adjustresult) unitid
         FROM psnadjust a LEFT JOIN psnadjust b
         ON a.personnelid = b.personnelid
         WHERE a.adjusttype = '2' AND b.adjusttype = '2'
         GROUP BY b.personnelid, a.effectdate
        ) dt
       INNER JOIN psnaccessioninfo c
           ON c.personid = dt.personid
       INNER JOIN orgstdstruct d
           ON d.unitid = dt.unitid
ORDER BY employeeid, unitcode, startdate;

  

http://www.cnblogs.com/zhxhdean/archive/2011/05/16/2047795.html

http://blog.163.com/ybhjj2005@126/blog/static/561082842007235010598/

http://www.cnblogs.com/liuzhendong/archive/2011/10/10/2205744.html

posted @ 2014-05-11 12:48  跟着阿笨一起玩.NET  阅读(9814)  评论(1编辑  收藏  举报