SQL 视图效率和连接超时设置

2016-01-29 15:51 72 1 收藏

SQL 视图效率和连接超时设置,SQL 视图效率和连接超时设置

【 tulaoshi.com - SQLServer 】

在使用视图的过程中碰到了很多问题,其实试图并不能真正提高效率,只是更加方便的给用户提供了操作,使用户有了更加条理的思路,使用视图时能用连接就用连接,而子查询效率明显差很远。

以下是我的对比:

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/sqlserver/)

CREATE VIEW DCLSVIEW AS SELECT ksjbxx.zkzh, ksjbxx.xm, ksjbxx.sfzh, ksjbxx.xb, ksjbxx.csny,ksjbxx.szxx, isnull((SELECT sfbhb.mc   FROM sfbhb  WHERE sfbhb.bh = ksjbxx.szsf),  '') AS sfmc,isnull((SELECT mc FROM kdxx WHERE kdxx.bh = ksjbxx.ksdd), '') AS ksdd, ksjbxx.ksh,isnull((SELECT kslbbhb.mc  FROM kslbbhb  WHERE kslbbhb.bh = ksjbxx.kslb), '') AS kslb,isnull((SELECT zyfxbhb.mc  FROM zyfxbhb  WHERE zyfxbhb.bh = ksjbxx.zyfx), '') AS zyfx,ksjbxx.txdz,ksjbxx.cf AS cf , isnull((select jf from zyfs where zkzh = ksjbxx.zkzh and kmbh =001 ),'0') as zyf001 , isnull((select jf from zyfs where zkzh = ksjbxx.zkzh and kmbh =002 ),'0') as zyf002 , isnull((select jf from zyfs where zkzh = ksjbxx.zkzh and kmbh =003 ),'0') as zyf003 , isnull((select jf from zyfs where zkzh = ksjbxx.zkzh and kmbh =004 ),'0') as zyf004 , isnull((select jf from zyfs where zkzh = ksjbxx.zkzh and kmbh =005 ),'0') as zyf005 , isnull((select jf from zyfs where zkzh = ksjbxx.zkzh and kmbh =006 ),'0') as zyf006 , isnull((select jf from zyfs where zkzh = ksjbxx.zkzh and kmbh =007 ),'0') as zyf007 , isnull((select jf from zyfs where zkzh = ksjbxx.zkzh and kmbh =008 ),'0') as zyf008 , isnull((select jf from zyfs where zkzh = ksjbxx.zkzh and kmbh =009 ),'0') as zyf009 , isnull((select jf from zyfs where zkzh = ksjbxx.zkzh and kmbh =010 ),'0') as zyf010 , isnull((select hdf from whfslrb where whfslrb.zkzh = ksjbxx.zkzh and whfslrb.whkm =01 ),'0') as whf01, isnull((select hdf from whfslrb where whfslrb.zkzh = ksjbxx.zkzh and whfslrb.whkm =02 ),'0') as whf02, isnull((select hdf from whfslrb where whfslrb.zkzh = ksjbxx.zkzh and whfslrb.whkm =03 ),'0') as whf03, isnull((select hdf from whfslrb where whfslrb.zkzh = ksjbxx.zkzh and whfslrb.whkm =04 ),'0') as whf04, isnull((select hdf from whfslrb where whfslrb.zkzh = ksjbxx.zkzh and whfslrb.whkm =05 ),'0') as whf05, isnull((select hdf from whfslrb where whfslrb.zkzh = ksjbxx.zkzh and whfslrb.whkm =06 ),'0') as whf06, isnull((select hdf from whfslrb where whfslrb.zkzh = ksjbxx.zkzh and whfslrb.whkm =07 ),'0') as whf07, isnull((select hdf from whfslrb where whfslrb.zkzh = ksjbxx.zkzh and whfslrb.whkm =08 ),'0') as whf08, isnull((select hdf from whfslrb where whfslrb.zkzh = ksjbxx.zkzh and whfslrb.whkm =09 ),'0') as whf09 , isnull((select jzzf from zyfzfb where zkzh =ksjbxx.zkzh),'0') as zyfjzzf,isnull((select place from zyfzfb where zkzh =ksjbxx.zkzh),'0') as place ,isnull((select sfplace from zyfzfb where zkzh =ksjbxx.zkzh),'0') as sfplace ,isnull((select zyzysx from syb where syb.zkzh = ksjbxx.zkzh and syb.zyzysx = 1),'') as zysx1 ,isnull(( select mc from whklbbhb where whklbbhb.bh in (select whkslb from whfs where whfs.zkzh = ksjbxx.zkzh)),'') as whkslb,isnull((select whfzf from whfs where whfs.zkzh = ksjbxx.zkzh ),'') as whfzf ,isnull((select whfzfdl from whfs where whfs.zkzh = ksjbxx.zkzh),'') as whfzfdl , isnull((select sfgsk from whfs where whfs.zkzh = ksjbxx.zkzh),'') as sfgsk  ,isnull((select case lqzt when '1' then '是' when '0' then '否' else null end  from ylqxx where ylqxx.zkzh = ksjbxx.zkzh ),'') as ylqzt  ,isnull((select case sfzzlq when '1' then '是' when '0' then '否' else null end  from zzlqxx where zzlqxx.zkzh = ksjbxx.zkzh ),'') as zzlqzt  from ksjbxx

以上试图的查询可能会用到三分多钟,

如果用下面师徒的创建过程,可能你只需要十秒不到的时间

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/sqlserver/)

SELECT dbo.ksjbxx.zkzh, dbo.ksjbxx.xm, dbo.ksjbxx.sfzh, dbo.ksjbxx.xb, dbo.ksjbxx.csny,
      dbo.ksjbxx.szxx, dbo.sfbhb.mc AS sfmc, dbo.kdxx.mc AS ksdd, dbo.ksjbxx.ksh,
      dbo.kslbbhb.mc AS kslb, dbo.zyfxbhb.mc AS zyfx, dbo.ksjbxx.txdz, dbo.ksjbxx.cf,
      dbo.zyfzfb.zyfzf, dbo.zyfzfb.jzzf AS zyfjzzf, dbo.zyfzfb.place, dbo.zyfzfb.sfplace,
      dbo.whklbbhb.mc

来源:http://www.tulaoshi.com/n/20160129/1496626.html

延伸阅读
新建两张表: 表1:student  截图如下: 表2:course  截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际开发中我们不会这样建表,实际开发中这两个表会有自己不同的主键。) 一、外连接 外连接可分为:左连接、右连接、完全外连接。 1、左连接  left join 或 left outer join SQL语句:select * from...
标签: 手游
WIFI未连接 解决办法:打开WIFI热点,寻找网络连接上后重新联网打开游戏。 设备防护软件限制 解决办法:进入游戏防护软件比如360进行设置,将游戏的限制联网选项勾选掉即可。 信号不稳定 解决办法:移动到信号较为稳定的地方重新连接,或是调整信号源后重联游戏。 区域未开放 解决办法:只需要等待开...
标签: SQLServer
  在SQL Server Enterprise Manager 中点击某个SQL Server的属性,选择connections项,在Maximum concurrent user (connections(0 = unlimited):的框中输入你所要的数字即可.  
最近碰到一个mysql5数据库的问题。就是一个标准的servlet/tomcat网络应用,后台使用mysql数据库。问题是待机一晚上后,第二天早上第一次登录总是失败。察看日志发现如下错误: “com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last packet sent to the server was 0 ms ago.” 经...
数据库操作现在是项目开发的根本,学习Java首先应该学会怎么样连接数据库,用Java连接数据库可不像用Delphi这类工具那样设几个属性就OK,说简单也简单,说复杂,其实也挺复杂的,而且很麻烦,如果是初学,根本不能保证第一次就连接成功,下面以SQL Server 2000为例,说说Java连接数据库的基本方法,也记录一下心得。 1、下载SQL Server 2000 ...

经验教程

690

收藏

18
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部