数据性能调校——查看最耗资源的各种SQL
从计划高速缓存中清除查询计划DBCC FREEPROCCACHE 清除缓存中的过程DBCC DROPCLEANBUFFERS清除内存中的数据SELECT DB_ID('你的数据库名')
SELECT TOP 10 total_worker_time/execution_count AS avg_cpu_cost,plan_handle,execution_count,(SELECT SUBSTRING(text,statement_start_offset/2+1,(CASE WHEN statement_end_offset=-1 THEN LEN(CONVERT(nvarchar(max),text))*2ELSE statement_end_offset END -statement_start_offset)/2) FROM sys.dm_exec_sql_text(sql_handle) where dbid = 6 -- koubei = 6 指定数据库ID )AS query_textFROM sys.dm_exec_query_statsORDER BY [avg_cpu_cost] DESC查询SQL改进版select highest_cpu_queries.plan_handle, highest_cpu_queries.total_worker_time,highest_cpu_queries.avg_cpu_cost,highest_cpu_queries.execution_count,q.dbid,q.objectid,q.number,q.encrypted,q.[text]from (select top 50 qs.plan_handle, qs.total_worker_time,qs.total_worker_time/execution_count AS avg_cpu_cost ,qs.execution_countfrom sys.dm_exec_query_stats qsorder by avg_cpu_cost desc) as highest_cpu_queriescross apply sys.dm_exec_sql_text(plan_handle) as qwhere dbid = 6 -- koubei = 6order by highest_cpu_queries.avg_cpu_cost desc
SELECT TOP 10 total_worker_time,plan_handle,execution_count,(SELECT SUBSTRING(text,statement_start_offset/2+1,(CASE WHEN statement_end_offset=-1 THEN LEN(CONVERT(nvarchar(max),text))*2ELSE statement_end_offset END -statement_start_offset)/2) FROM sys.dm_exec_sql_text(sql_handle) where dbid = 6 -- koubei = 6 )AS query_textFROM sys.dm_exec_query_statsORDER BY execution_count DESC改进版本:select highest_execution_count.plan_handle, highest_execution_count.total_worker_time,highest_execution_count.execution_count,q.dbid,q.objectid,q.number,q.encrypted,q.[text]from (select top 50 qs.plan_handle, qs.total_worker_time,qs.execution_countfrom sys.dm_exec_query_stats qsorder by execution_count desc) as highest_execution_countcross apply sys.dm_exec_sql_text(plan_handle) as qwhere dbid = 6 -- koubei = 6order by highest_execution_count.execution_count desc
SELECT TOP 10 plan_generation_num,execution_count,(SELECT SUBSTRING(text,statement_start_offset/2+1,(CASE WHEN statement_end_offset=-1 THEN LEN(CONVERT(nvarchar(max),text))*2ELSE statement_end_offset END -statement_start_offset)/2) FROM sys.dm_exec_sql_text(sql_handle) where dbid = 6 -- koubei = 6 )AS query_textFROM sys.dm_exec_query_stats WHERE plan_generation_num>1 ORDER BY plan_generation_num DESC改进版:select highest_plan_count.plan_handle, highest_plan_count.total_worker_time,highest_plan_count.execution_count,highest_plan_count.plan_generation_num,q.dbid,q.objectid,q.number,q.encrypted,q.[text]from (select top 50 qs.plan_handle, qs.total_worker_time,qs.execution_count,qs.plan_generation_numfrom sys.dm_exec_query_stats qs WHERE plan_generation_num>1order by plan_generation_num desc) as highest_plan_countcross apply sys.dm_exec_sql_text(plan_handle) as qWHERE dbid = 6 -- koubei = 6order by highest_plan_count.plan_generation_num desc
SELECT TOP 10 (total_logical_reads/execution_count)AS avg_logical_reads,(total_logical_writes/execution_count)AS avg_logical_writes,(total_physical_reads/execution_count)AS avg_phys_reads,execution_count,(SELECT SUBSTRING(text,statement_start_offset/2+1,(CASE WHEN statement_end_offset=-1 THEN LEN(CONVERT(nvarchar(max),text))*2ELSE statement_end_offset END -statement_start_offset)/2) FROM sys.dm_exec_sql_text(sql_handle) where dbid = 6 -- koubei = 6 )AS query_text,plan_handleFROM sys.dm_exec_query_stats ORDER BY (total_logical_reads+total_logical_writes)desc改进版:select highest_reads_count.avg_logical_reads, highest_reads_count.avg_logical_writes,highest_reads_count.avg_phys_reads,highest_reads_count.execution_count,highest_reads_count.plan_handle,q.dbid,q.objectid,q.number,q.encrypted,q.[text]from (select top 50 (total_logical_reads/execution_count)AS avg_logical_reads,(total_logical_writes/execution_count)AS avg_logical_writes,(total_physical_reads/execution_count)AS avg_phys_reads,qs.execution_count,qs.plan_handlefrom sys.dm_exec_query_stats qsorder by (total_logical_reads+total_logical_writes) desc) as highest_reads_countcross apply sys.dm_exec_sql_text(plan_handle) as qWHERE dbid = 6 -- koubei = 6order by (highest_reads_count.avg_logical_reads+highest_reads_count.avg_logical_writes) desc
SELECT t1.object_id,t2.user_seeks,t2.user_scans,t1.equality_columns,t1.inequality_columnsFROM sys.dm_db_missing_index_details AS t1,sys.dm_db_missing_index_group_stats AS t2,sys.dm_db_missing_index_groups AS t3WHERE database_id=6 --koubei库ID,查看某个库的ID,可以切换到那个库下面,然后SELECT DB_ID()AND object_id=OBJECT_ID('表名')AND t1.index_handle=t3.index_handleAND t2.group_handle=t3.index_group_handle改进版本:
SELECT mig.*,statement AS tableName,column_id,column_name,column_usage FROM sys.dm_db_missing_index_details AS mid CROSS APPLY sys.dm_db_missing_index_columns(mid.index_handle)INNER JOIN sys.dm_db_missing_index_groups AS mig ON mig.index_handle=mid.index_handleWHERE database_id=6 AND object_id=OBJECT_ID('表名')ORDER BY mig.index_group_handle,mig.index_handle,column_id并发问题,查询阻塞和被阻塞的SQL。注意下面waitingsql和blockingsql两个字段。declare @lockinfo TABLE ([waiting_time_ms] [bigint],[waiting_resource_type] [varchar](60) ,[dbanme] [nvarchar](128),[objectname] [nvarchar](128),[waiting_request_lock_mode] [varchar](60) ,[waiting_session_id] [int] ,[waiting_loginame] [varchar](128) ,[waiting_hostname] [varchar](128) ,[waitingsql] [nvarchar](max),[blocking_session_id] [smallint],[blocking_loginame] [varchar](128) ,[blocking_hostname] [varchar](128) ,[blockingsql] [varchar](max),[createtime] [datetime])DECLARE @count intdeclare @sql nvarchar(max)set @sql = N''select@sql = @sql + N'union allselect '''+convert(nvarchar(100),database_id)+N''' as dbid,object_id,hobt_id from ['+name+N'].sys.partitions(nolock)'from sys.databases(nolock)where name not in ('model','tempdb') and state_desc = 'ONLINE'set @sql = substring(@sql,10,len(@sql))declare @objinfo table(dbid int,object_id int,hobt_id bigint)insert into @objinfo exec(@sql)INSERT INTO @lockinfo(waiting_time_ms,waiting_resource_type,dbanme,objectname,waiting_request_lock_mode,waiting_session_id,waiting_loginame,waiting_hostname,waitingsql,blocking_session_id,blocking_loginame,blocking_hostname,blockingsql)SELECT DISTINCT t2.wait_duration_ms waiting_time_ms,t1.resource_type waiting_resource_type,DB_NAME(t1.resource_database_id) dbanme,CASE t1.resource_typeWHEN 'OBJECT' THEN OBJECT_NAME(t1.resource_associated_entity_id,t1.resource_database_id)WHEN 'DATABASE' THEN 'DATABASE'ELSE(SELECT OBJECT_NAME(object_id, t1.resource_database_id)FROM @objinfoWHERE hobt_id = t1.resource_associated_entity_idand dbid = t1.resource_database_id)END AS ObjectName,t1.request_mode waiting_request_lock_mode,t1.request_session_id waiting_session_id,s1.login_name waiting_loginame,s1.host_name+'-'+r3.client_net_address waiting_hostname,CASE WHEN st1.objectid IS NULL THEN st1.text ELSE OBJECT_NAME(st1.objectid,st1.dbid) END waitingsql,t2.blocking_session_id,s2.login_name blocking_loginame,s2.host_name+'-'+r2.client_net_address blocking_hostname,CASE WHEN st2.objectid IS NULL THEN st2.text ELSE OBJECT_NAME(st2.objectid,st2.dbid) END blockingsqlFROM sys.dm_tran_locks AS t1 with(nolock)INNER JOIN sys.dm_os_waiting_tasks AS t2 with(nolock) ON t1.lock_owner_address = t2.resource_addressINNER JOIN sys.dm_exec_requests r1 with(nolock) ON t1.request_session_id=r1.session_idINNER JOIN sys.dm_exec_sessions s1 with(nolock) ON s1.session_id=r1.session_idCROSS APPLY sys.dm_exec_sql_text(r1.sql_handle) st1INNER JOIN sys.dm_exec_connections r2 with(nolock) ON t2.blocking_session_id=r2.session_idINNER JOIN sys.dm_exec_sessions s2 with(nolock) ON s2.session_id=r2.session_idCROSS APPLY sys.dm_exec_sql_text(r2.most_recent_sql_handle) st2left JOIN sys.dm_exec_connections r3 with(nolock) ON t1.request_session_id=r3.session_idORDER BY t2.wait_duration_ms DESCselect * from @lockinfo
转载于:https://www.cnblogs.com/gered/p/9540887.html
数据性能调校——查看最耗资源的各种SQL相关推荐
- Tile-Based架构下的性能调校
Performance Tunning for Tile-Based Architecture Tile-Based架构下的性能调校 by Bruce Merry GameKnife译 译序 在大概1 ...
- ssis导出数据性能_如何使用SSIS将数据从Excel导出到Azure SQL数据库中的多个表
ssis导出数据性能 In this article, I am going to explain how we can split the data within the excel file an ...
- ssis导出数据性能_使用SSIS Hadoop组件导入和导出数据
ssis导出数据性能 In the previously published article, we talked briefly about Hadoop, and we gave an overv ...
- 《PostgreSQL 9.0性能调校》一一2.1 平衡硬件支出
本节书摘来自异步社区出版社<PostgreSQL 9.0性能调校>一书中的第2章,第2.1节,作者: [美]Gregory Smith,更多章节内容可以访问云栖社区"异步社区&q ...
- (转)Performance Tunning for Tile-Based Architecture Tile-Based架构下的性能调校
转自:http://www.cnblogs.com/gameknife/p/3515714.html#commentform Performance Tunning for Tile-Based Ar ...
- linux的进程/线程/协程系列3:查看linux内核源码——vim+ctags/find+grep
linux的进程/线程/协程系列3:查看linux内核源码--vim+ctags/find+grep 前言 摘要: 1. 下载linux内核源码 2. 打标签方法:vim+ctags 2.1 安装vi ...
- oracle查询耗资源的进程,常用Oracle进程资源查询语句(运维必看)
(一)根据程序名称查找相关信息 select A.process,B.spid,A.sid,A.serial#,A.sql_address,A.username,A.program,A.status, ...
- 世界杯ing~这不来个实时数据可视化?(结尾附源码)
写在前面:博主是一只经过实战开发历练后投身培训事业的"小山猪",昵称取自动画片<狮子王>中的"彭彭",总是以乐观.积极的心态对待周边的事物.本人的技 ...
- JavaGUI:多功能计算器(五)--Swing实现双语数据包+菜单切换(完整源码+EXE下载)
JavaGUI:多功能计算器(五)–Swing实现双语数据包+菜单切换(完整源码+EXE下载) 本文资源下载: 程序源码及可独立运行的EXE文件自解压包(32bit): 多功能计算器v0.41[双语界 ...
最新文章
- 网页制作的中的一些工具代码
- 17.SpringMVC核心技术-拦截器
- 安装lynis_lynis安装和扫描Linux的安全漏洞
- 我在神策做研发 | 码农变身“建筑师”:安逸中离走,责任中成长
- [CF]Codeforces Round #529 (Div. 3)
- 大剑无锋之二分搜索、二分搜索时间复杂度、三分查找呢?
- C++中virtual关键字的用法
- 找出出现次数最多的字母
- nor flash与nand flash启动的简单比较--APPLE的ARM学习笔记一
- 上周热点回顾(2.19-2.25)
- SQL Server数据导入导出的几种方法
- Qt + 运动控制 (固高运动控制卡)【3】运动控制卡几种常用的回零方式
- C程序10 自由落体
- 年终固定资产大盘点的具体步骤
- 最新版gg服务器框架安装器,GG服务框架安装器最新版本
- java 23种设计模式详解
- 使用mysqladmin修改用户密码的正确方法!
- i2c信号的ACK与NACK
- 用TortoiseGit Git clone时Load Putty Key是灰色的
- WIN10桌面图标变成白文件的一种解决方法
热门文章
- python练习册 每天一个小程序 第0013题
- Django 学习第十一天——中间键和上下文处理器
- UVa122-Trees on the level
- 微信小程序-04-详解介绍.json 配置文件
- Firefox 网络调试工具
- 控制结构(1)-判断控制
- Hive常用的SQL命令操作
- 福州大学计算机专业排名2018,软科2018年世界一流学科排名发布 福州大学9个学科上榜...
- 你可以去学python_你是怎么学好Python的?
- 神经网络与机器学习 笔记—反向传播算法(BP)