设为首页 收藏本站
查看: 824|回复: 0

[经验分享] SQL server分页查询效率低导致web后台增删改问题

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2016-11-7 07:24:57 | 显示全部楼层 |阅读模式
  问题描述:web后台增删改操作很卡,卡的时间长达1个小时左右
  
  环境工具:数据库SQL  server;web服务器apache+tomcat
  
  解决过程:1.  网站上线后,使用后台的编辑人员发现在后台维护数据很卡,如图(1)
DSC0000.jpg

图(1)

  我先检查apache和tomcat发现正常,然后怀疑是数据库的性能问题,就重启了下数据库服务,卡的情况得到解决。
  
  2.  我请教有经验的人,有人说可能是数据库服务器的磁盘阵列是raid5,raid5对SQL server的数据库写操作的性能有很大影响,尤其是数据量大的时候,我通过重装SQL server数据库等各种方式验证,发现不是。
  
  3.  问题再次发生时,我想重启数据库服务器可以解决问题,还可以从web服务器的角度考虑一下,于是重启web服务器,发现也可以解决问题。
  
  4.  根据以上情况,我判断应该是web服务访问数据库服务,处理SQL的session发生异常,于是使用
select request_owner_id

,request_owner_type

,resource_type

,resource_associated_entity_id

,request_session_id spid

,request_lifetime

,request_status

,request_type

,request_mode

,OBJECT_NAME(resource_associated_entity_id) tableName

from  sys.dm_tran_locks

  where resource_type='OBJECT';
  发现好像是进程死锁了,结果如图(2)。于是在网上寻找解决死锁的方法,始终不得要领。
DSC0001.jpg

图(2)

 

          5. 后来仔细研究死锁,想起死锁是两个以上的进程互掐或者环掐,我查到的结果只有一个进程,于是我用
select dtl.request_session_id spid

,spc.spid

,spc.kpid

,spc.blocked

,spc.lastwaittype

,spc.waitresource

,spc.dbid

,spc.uid

,spc.cpu

,spc.status

,spc.cmd

,spc.loginame

,dtl.request_lifetime

,dtl.request_status

,dtl.request_mode

,OBJECT_NAME(dtl.resource_associated_entity_id) tableName

from  sys.dm_tran_locks dtl,sys.sysprocesses spc

where dtl.request_session_id=spc.spid

and spc.loginame='cicrodb'

and dtl.resource_type='OBJECT';
才发现不是死锁,是进程阻塞。那么到底造成阻塞的这个进程到底在什么呢?
我用dbcc inputbuffer(77)找到正在阻塞的进程所执行的语句
select top 20 ci.pre_title,

ci.is_pic,

ci.info_id,

ci.cat_id,

ci.model_id,

ci.description,

ci.from_id,

ci.title,

ci.subtitle,

ci.title_color,

ci.thumb_url,

ci.author,

ci.editor,

ci.source,

ci.content_url,

ci.weight,

ci.hits,

ci.day_hits,

ci.week_hits,

ci.month_hits,

ci.released_dtime,

ci.site_id,

ci.page_count,

ca.cat_cname     

from cs_info ci,cs_info_category ca       

where ci.cat_id = ca.cat_id

and ci.site_id = ca.site_id           

and ca.cat_id ='10260'         

and ci.info_status = 8

and ci.final_status = 0    

and ci.info_id not in     

(select top 111940 ci.info_id    

  from cs_info ci,cs_info_category ca       

  where ci.cat_id = ca.cat_id

  and ci.site_id = ca.site_id    

  and ( ca.cat_id in ( select cat_id from cs_info_category where cat_position like '$0$10258$10260$%' ) )     

  and ci.info_status = 8

  and ci.final_status = 0      

  order by ci.released_dtime desc);
 
       6. 这段SQL是一段分页查询的SQL,采用了top方案,我直接将这段SQL复制到SQL server客户端上执行,发现要1个小时才能执行完,于是印证了为什么客户保存数据,要卡1个多小时。因为目前SQL server分页查询有三种方案:
       A. top方案
       B. max/min方案
       C. rownumber方案
效率:rownumber方案 > max/min方案 > top方案;rownumber方案sql复杂,支持SQL server2000以上;max/min方案sql复杂,不支持非唯一性列排序查询;top方案不支持复合主键查询。
 
       7. 根据项目的需要,选择rownumber方案,改为如下情况:
select top 20 ci.rownum,

ci.pre_title,

ci.is_pic,

ci.info_id,

ci.cat_id,

ci.model_id,

ci.description,

ci.from_id,

ci.title,

ci.subtitle,

ci.title_color,

ci.thumb_url,

ci.author,

ci.editor,

ci.source,

ci.content_url,

ci.weight,

ci.hits,

ci.day_hits,

ci.week_hits,

ci.month_hits,

ci.released_dtime,

ci.site_id,

ci.page_count,

ci.cat_cname     

from (select top 111960 ROW_NUMBER() OVER (ORDER BY ci.info_id) rownum,

ci.pre_title,

ci.is_pic,

ci.info_id,

ci.cat_id,

ci.model_id,

ci.description,

ci.from_id,

ci.title,

ci.subtitle,

ci.title_color,

ci.thumb_url,

ci.author,

ci.editor,

ci.source,

ci.content_url,

ci.weight,

ci.hits,

ci.day_hits,

ci.week_hits,

ci.month_hits,

ci.released_dtime,

ci.site_id,

ci.page_count,

ca.cat_cname    

  from cs_info ci,cs_info_category ca       

  where ci.cat_id = ca.cat_id

  and ci.site_id = ca.site_id    

  and ( ca.cat_id in ( select cat_id from cs_info_category where cat_position like '$0$10258$10260$%' ) )

  and ci.info_status = 8

  and ci.final_status = 0

order by ci.released_dtime desc) ci

where ci.rownum>111940

order by ci.released_dtime desc; 
              

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-296716-1-1.html 上篇帖子: SQL Server Deadlock 下篇帖子: SQL2005_用户_'sa'_登录失败。该用户与可信_SQL_Server_连接无关联解决办法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表