继续调优,今天上午分析了以下一条处理时间达40秒的SQL语句
select *
  from table
 where T_table_ID in  
  (
   select  distinct s.t_table_id
     from
     (  
      select distinct a.t_table_id,a.bt
        from   
        (select left(bt,4) as bbt,* from table where fsrq>getdate()-1 and gkbz=1 and scbz=0) a,  
        (select distinct left(bt,4) as bbt,t_table_id from table where fsrq>getdate()-1 and gkbz=1 and scbz=0) b  
       where b.bbt like a.bbt and a.t_table_id<>b.t_table_id  
         and a.bbt not like '%aaaa%' and a.bbt not like '%bbbb%' and a.bbt not like '%cccc%'
         and a.bbt not like '%dddd%' and a.bbt not like '%eeee%' and a.bbt not like '%ffff%'
         and a.bbt not like '%aaaa%' and a.bbt not like '%bbbb%' and a.bbt not like '%cccc%'
         and a.bbt not like '%dddd%' and a.bbt not like '%eeee%' and a.bbt not like '%ffff%'
         -- order by a.bt  
      union all  
      select distinct a.t_table_id,a.bt
        from   
        (select right(bt,5) as bbt,* from table where fsrq>getdate()-1 and gkbz=1 and scbz=0) a,  
        (select distinct right(bt,5) as bbt,t_table_id from table where fsrq>getdate()-1 and gkbz=1 and scbz=0) b  
       where b.bbt like a.bbt and a.t_table_id<>b.t_table_id   
         and a.bbt not like '%aaaa%' and a.bbt not like '%bbbb%' and a.bbt not like '%cccc%'
         and a.bbt not like '%dddd%' and a.bbt not like '%eeee%' and a.bbt not like '%ffff%'
         and a.bbt not like '%aaaa%' and a.bbt not like '%bbbb%' and a.bbt not like '%cccc%'
         and a.bbt not like '%dddd%' and a.bbt not like '%eeee%' and a.bbt not like '%ffff%'
         and a.bbt not like '%'+(select right(convert(varchar(10),getdate()-1,20),2)+')') +'%'  
         and b.bbt not like '%'+(select right(convert(varchar(10),getdate()-1,20),2)+')') +'%'  
       ) s   
   )order by bt  
基本上可以认为是对同一张表的反复操作,而且语句中夹杂了太多的全表扫描
SQLServer的执行计划我个人认为图形化界面固然是好,但是有些时候对于量化的I/O,CPU,COST输出却很不直观,此外像该SQL这样的执行计划,估计1600*1200的整个屏幕都无法显示,可以认为基本是没法看的

只能将SQL分解成若干小SQL,逐步找到瓶颈所在,例如
select left(bt,4) as bbt,* from table where fsrq>getdate()-1 and gkbz=1 and scbz=0
select distinct left(bt,4) as bbt,t_table_id from table where fsrq>getdate()-1 and gkbz=1 and scbz=0
这两个语句的执行都非常快,并且结果集也比较小,但是两条语句合并后并加上相关条件就非常缓慢。
干脆直接构建两个临时表,反正都是全表扫描,用两个临时表做相互的join,测试之后发现只需要1秒
再构建下面的两个SQL临时表,也做同样的测试
最后再全部合并到一起进行测试,发现也就是2~3秒
实际上还可以再优化一些临时表的构建,但效果达到了也就不愿意尝试了

也尝试过用CTE,不过似乎效果不佳
以下为优化后的SQL样例
/*
with temp1 as
(select left(bt,4) as bbt,* from table where fsrq>getdate()-1 and gkbz=1 and scbz=0),
temp2 as
(select distinct left(bt,4) as bbt,t_table_id from table where fsrq>getdate()-1 and gkbz=1 and scbz=0),
temp3 as
(select left(bt,5) as bbt,* from table where fsrq>getdate()-1 and gkbz=1 and scbz=0),
temp4 as
(select distinct left(bt,5) as bbt,t_table_id from table where fsrq>getdate()-1 and gkbz=1 and scbz=0)
*/
print convert(varchar,getdate(),9)
select left(bt,4) as bbt,* into #temp1 from table where fsrq>getdate()-1 and gkbz=1 and scbz=0
select distinct left(bt,4) as bbt,t_table_id into #temp2 from table where fsrq>getdate()-1 and gkbz=1 and scbz=0
select right(bt,5) as bbt,* into #temp3 from table where fsrq>getdate()-1 and gkbz=1 and scbz=0
select distinct right(bt,5) as bbt,t_table_id into #temp4 from table where fsrq>getdate()-1 and gkbz=1 and scbz=0
select
(select ms from xtclb where dm=lmxz and lb in (130,131) ) as '栏目选择',
 bt,mtly,czy
  from table
 where T_table_ID in  
  (
   select  distinct s.t_table_id
     from
     (  
      select distinct a.t_table_id,a.bt
        from   
        #temp1 a,  
        #temp2 b  
       where b.bbt like a.bbt and a.t_table_id<>b.t_table_id  
         and a.bbt not in ('aaaa','bbbb','cccc','dddd','eeee','ffff')
         and b.bbt not in ('aaaa','bbbb','cccc','dddd','eeee','ffff')
      union all  
      select distinct a.t_table_id,a.bt
        from   
        #temp3 a,  
        #temp4 b  
       where b.bbt like a.bbt and a.t_table_id<>b.t_table_id   
         and a.bbt not like '%aaaa%' and a.bbt not like '%bbbb%' and a.bbt not like '%cccc%'
         and a.bbt not like '%dddd%' and a.bbt not like '%eeee%' and a.bbt not like '%ffff%'
         and a.bbt not like '%'+(select right(convert(varchar(10),getdate()-1,20),2)+')') +'%'  
         and a.bbt not like '%aaaa%' and a.bbt not like '%bbbb%' and a.bbt not like '%cccc%'
         and a.bbt not like '%dddd%' and a.bbt not like '%eeee%' and a.bbt not like '%ffff%'
         and b.bbt not like '%'+(select right(convert(varchar(10),getdate()-1,20),2)+')') +'%'  
       ) s   
   )order by bt  
   --OPTION (loop join);
   --34
print convert(varchar,getdate(),9)   
/*
drop table #temp1   
drop table #temp2
drop table #temp3
drop table #temp4
*/

SQLServer性能优化之活用临时表相关推荐

  1. 做好DBA,做好SQLServer性能优化

    怎样查出SQLServer的性能瓶颈 --王成辉翻译整理,转贴请注明出自微软BI开拓者[url]www.windbi.com[/url] --原帖地址 如果你曾经做了很长时间的DBA,那么你会了解到S ...

  2. sqlserver 性能优化思路

    通常我们会依照下面的顺序进行分析: 硬件能力 系统规模 数据库内部因素 软件环境 顺序可以有所调整或者交换,但是系统的性能优化一定要从全局出发.切勿一来就深入到某一个SQL语句的优化.可能你花费大量的 ...

  3. mysql sqlserver 性能优化_SQLServer性能优化之---数据库级日记监控

    4.6.6.SQLServer监控 PS:这些脚本都是我以前用SQLServer手写的,参考即可(现在用MySQL,下次也整理一下) 之前写SQLServer监控系列文章因为换环境断篇了,只是简单演示 ...

  4. mysql sqlserver 性能优化_SQLSERVER SQL性能优化技巧

    1.选择最有效率的表名顺序(只在基于规则的优化器中有效) SQLSERVER的解析器按照从右到左的顺序处理FROM子句中的表名,因此FROM子句中写在最后的表(基础表driving table)将被最 ...

  5. SQLServer性能优化之查询提示

    数据库于周日被重启了,刚好看看优化后的效果,顺便再找找新的需要优化的SQL 刚好找到了类似的几条语句,如下 select * from tableA where id not in (select i ...

  6. 关于Net开发中一些SQLServer性能优化的建议

    一. ExecuteNonQuery和ExecuteScalar 对数据的更新不需要返回结果集,建议使用ExecuteNonQuery.由于不返回结果集可省掉网络数据传输.它仅仅返回受影响的行数.如果 ...

  7. 06.SQLServer性能优化之---数据库级日记监控

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 之前说了一下数据库怎么发邮件:http://www.cnblogs.com/duniti ...

  8. SqlServer性能优化 自定义动化性能收集(四)

    配置数据收集器: 1.创建登录名并映射角色 2.配置管理数据仓库 3.创建收集组.收集项----MSDB数据存储   sp_syscollector_create... 4.自动配置相关job 具体步 ...

  9. SQLServer性能优化一则小实例(2010-07-21)

    首先找到 最耗CPU的top50 SQL SELECT       total_cpu_time,       total_execution_count,       number_of_state ...

最新文章

  1. mysql-interview
  2. iOS学习笔记14-网络(三)WebView
  3. Pandas的学习(1.pandas的介绍以及pandas中的Series的创建)
  4. 蓝桥杯哈夫曼树java_Java实现蓝桥杯3n+1问题
  5. cm0中断优先级_转:第13章 FreeRTOS任务优先级修改及其分配方案
  6. Windows Phone 7 自定义弹出窗口
  7. 通俗易懂,王者荣耀是用什么编程语言开发的?
  8. python第四天 组合数据类型 文件与数据格式化
  9. java中求平均数怎么写,java求平均数函数
  10. Linux系统基于MobaXterm的下载及使用
  11. Java后端开发之JSON入门
  12. 国际标准化比率 INR
  13. 一起学CC3200系列教程之跑马灯---库
  14. 如何ssh连接本地的虚拟机
  15. python3 项目开发-中级篇(二)
  16. php程序员要不要转java
  17. 上班划水神器:一个可以在控制台玩斗地主项目!
  18. 数据读取与数据扩增理解
  19. “字面量”和“符号引用”
  20. EDIUS调音台的使用

热门文章

  1. memsql 多节点部署
  2. SpringCloud之Zuul网关
  3. 微软加入反 Flash 阵营,新版 Edge 默认屏蔽 Flash
  4. 【Trie】【HDU1247】【Hat’s Wordsfd2】
  5. 解决listview addheader EditText焦点问题
  6. 有效利用番茄工作法提高效率--XorTime的使用方法
  7. MyGui 3.2.0(OpenGL平台)的编译
  8. 数据库联接字符串大全
  9. SSM整合配置文件总结
  10. 一定要知道的,那些Linux基本操作命令