Skip to main content

SQL DMV history as in Oracle

Oracle has historical table that you can query to find out active session history etc, sqlserver does not have that, the script below build the history table in sqlserver and create procedure that can be scheduled to run by sqlserver agent to collect the snapshot information.

The historical tables are focusing on wait events and cpu scheduler, you can use the following query to check the blocking situation which last more than 10 seconds.

select timeofsnapshot,wait_duration_ms,wait_type,session_id,blocking_session_id,login_name,wait_resource,command,nt_user_name,blocking_nt_user_name,text
from dm_os_waiting_tasks where wait_duration_ms>10000 and wait_type like 'LCK%'
order by wait_duration_ms
--order by snapshot_id desc

the result is something like the screenshot below


you can schedule sqlserver agent to run every 10 seconds to get the snapshot, to collect the snapshot, run procedure “exec collect_dmv_all”

Below are the DDLs to create the database objects

USE [dba]
GO
/****** Object:  StoredProcedure [dbo].[collect_dm_sysprocesses_v2]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[collect_dm_sysprocesses_v2] @snapshot_id int, @timeofsnapshot datetime
as
 insert into dm_sysprocesses
select top 8  @snapshot_id,@timeofsnapshot,last_batch, stmt_end,spid,blocked,waittype,waittime,lastwaittype,dbid,hostname,program_name,cmd,nt_username,sql_handle,stmt_start
 from sys.sysprocesses order by stmt_end,last_batch desc

GO
/****** Object:  StoredProcedure [dbo].[collect_dmosschedulers_v2]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[collect_dmosschedulers_v2]
@snapshot_id int, @timeofsnapshot datetime
 as
 insert into dm_os_schedulers
select @snapshot_id,@timeofsnapshot, t1.runnable_tasks_count,t1.is_idle, t1.load_factor,t1.pending_disk_io_count,t1.active_workers_count,t1.work_queue_count
 from sys.dm_os_schedulers t1


GO
/****** Object:  StoredProcedure [dbo].[collect_dmoswaitingtasks_v2]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE procedure [dbo].[collect_dmoswaitingtasks_v2] @snapshot_id int, @timeofsnapshot datetime as
insert into dm_os_waiting_tasks
SELECT @snapshot_id ,
@timeofsnapshot,
dm_ws.wait_duration_ms,
dm_ws.wait_type,
dm_es.status,
dm_t.TEXT,
dm_qp.query_plan,
dm_ws.session_ID,
dm_es.cpu_time,
dm_es.memory_usage,
dm_es.logical_reads,
dm_es.total_elapsed_time,
dm_es.program_name,
DB_NAME(dm_r.database_id) DatabaseName,
-- Optional columns
dm_ws.blocking_session_id,
dm_r.wait_resource,
dm_es.login_name,
dm_r.command,
dm_r.last_wait_type,
dm_es.nt_user_name,
dm_es.host_name,
dm_es2.login_name blocking_login_name,
dm_es2.host_name blocking_host_name,
dm_es2.nt_user_name blocking_nt_user_name
FROM sys.dm_os_waiting_tasks dm_ws
INNER JOIN sys.dm_exec_requests dm_r ON dm_ws.session_id = dm_r.session_id
INNER JOIN sys.dm_exec_sessions dm_es ON dm_es.session_id = dm_r.session_id
left outer join sys.dm_exec_sessions dm_es2 ON dm_ws.blocking_session_id = dm_es2.session_id
CROSS APPLY sys.dm_exec_sql_text (dm_r.sql_handle) dm_t
CROSS APPLY sys.dm_exec_query_plan (dm_r.plan_handle) dm_qp
WHERE dm_es.is_user_process = 1


GO
/****** Object:  StoredProcedure [dbo].[collect_dmoswaitstats_v2]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[collect_dmoswaitstats_v2] @snapshot_id int, @timeofsnapshot datetime as
insert into DM_OS_WAIT_STATS select @snapshot_id,@timeofsnapshot, wait_type,waiting_tasks_count,wait_time_ms,max_wait_time_ms,signal_wait_time_ms
from sys.dm_os_wait_stats


GO
/****** Object:  StoredProcedure [dbo].[collect_dmv_all]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[collect_dmv_all] as
declare @snapshot_id int
declare @timeofsnapshot datetime
select @snapshot_id=isnull(max(snapshot_id)+1,1) from snapshot
select @timeofsnapshot=getdate()
insert into snapshot values (@snapshot_id,@timeofsnapshot)
exec collect_dm_sysprocesses_v2 @snapshot_id,@timeofsnapshot
exec collect_dmosschedulers_v2  @snapshot_id,@timeofsnapshot
exec collect_dmoswaitingtasks_v2  @snapshot_id,@timeofsnapshot
exec collect_dmoswaitstats_v2  @snapshot_id,@timeofsnapshot

GO
/****** Object:  StoredProcedure [dbo].[compare_dmoswaitstas]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[compare_dmoswaitstas] @begin_snapshotid int, @end_snapshotid int
as
 select l.wait_type,(l.wait_time_ms-o.wait_time_ms) accum_wait_ms from dm_os_wait_stats o
 inner join dm_os_wait_stats l on o.wait_type=l.wait_type
 where o.snapshot_id=@begin_snapshotid and l.snapshot_id=@end_snapshotid
 and l.wait_time_ms > o.wait_time_ms
 order by accum_wait_ms desc

GO
/****** Object:  StoredProcedure [dbo].[purge_dm]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[purge_dm] @retention_days int as
declare @snapshot_id int
select @snapshot_id=isnull(max(snapshot_id),1) from snapshot where timeofsnapshot< getdate()-@retention_days
delete from snapshot where snapshot_id< @snapshot_id
delete from dm_os_schedulers where snapshot_id< @snapshot_id
delete from dm_os_wait_stats where snapshot_id< @snapshot_id
delete from dm_os_waiting_tasks where snapshot_id< @snapshot_id
delete from dm_sysprocesses where snapshot_id< @snapshot_id

GO
/****** Object:  Table [dbo].[dm_os_schedulers]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[dm_os_schedulers](
[snapshot_id] [int] NOT NULL,
[timeofsnapshot] [datetime] NOT NULL,
[runnable_tasks_count] [int] NOT NULL,
[is_idle] [bit] NOT NULL,
[load_factor] [int] NOT NULL,
[pending_disk_io_count] [int] NOT NULL,
[active_workers_count] [int] NOT NULL,
[work_queue_count] [bigint] NOT NULL
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[DM_OS_WAIT_STATS]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DM_OS_WAIT_STATS](
[snapshot_id] [int] NOT NULL,
[timeofsnapshot] [datetime] NOT NULL,
[wait_type] [nvarchar](60) NOT NULL,
[waiting_tasks_count] [bigint] NOT NULL,
[wait_time_ms] [bigint] NOT NULL,
[max_wait_time_ms] [bigint] NOT NULL,
[signal_wait_time_ms] [bigint] NOT NULL
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[dm_os_waiting_tasks]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[dm_os_waiting_tasks](
[snapshot_id] [int] NOT NULL,
[timeofsnapshot] [datetime] NULL,
[wait_duration_ms] [bigint] NULL,
[wait_type] [nvarchar](60) NULL,
[status] [nvarchar](30) NOT NULL,
[TEXT] [nvarchar](max) NULL,
[query_plan] [xml] NULL,
[session_ID] [smallint] NULL,
[cpu_time] [int] NOT NULL,
[memory_usage] [int] NOT NULL,
[logical_reads] [bigint] NOT NULL,
[total_elapsed_time] [int] NOT NULL,
[program_name] [nvarchar](128) NULL,
[DatabaseName] [nvarchar](128) NULL,
[blocking_session_id] [smallint] NULL,
[wait_resource] [nvarchar](256) NOT NULL,
[login_name] [nvarchar](128) NOT NULL,
[command] [nvarchar](16) NOT NULL,
[last_wait_type] [nvarchar](60) NOT NULL,
[nt_user_name] [nvarchar](256) NULL,
[host_name] [nvarchar](256) NULL,
[blocking_login_name] [nvarchar](256) NULL,
[blocking_host_name] [nvarchar](256) NULL,
[blocking_nt_user_name] [nvarchar](256) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
/****** Object:  Table [dbo].[dm_sysprocesses]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[dm_sysprocesses](
[snapshot_id] [int] NOT NULL,
[timeofsnapshot] [datetime] NOT NULL,
[last_batch] [datetime] NOT NULL,
[stmt_end] [int] NOT NULL,
[spid] [smallint] NOT NULL,
[blocked] [smallint] NOT NULL,
[waittype] [binary](2) NOT NULL,
[waittime] [bigint] NOT NULL,
[lastwaittype] [nchar](32) NOT NULL,
[dbid] [smallint] NOT NULL,
[hostname] [nchar](128) NOT NULL,
[program_name] [nchar](128) NOT NULL,
[cmd] [nchar](16) NOT NULL,
[nt_username] [nchar](128) NOT NULL,
[sql_handle] [binary](20) NOT NULL,
[stmt_start] [int] NOT NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[snapshot]    Script Date: 29/10/2013 9:38:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[snapshot](
[snapshot_id] [int] NOT NULL,
[timeofsnapshot] [datetime] NOT NULL
) ON [PRIMARY]

GO
/****** Object:  Index [dm_osschedulers_idx]    Script Date: 29/10/2013 9:38:40 AM ******/
CREATE NONCLUSTERED INDEX [dm_osschedulers_idx] ON [dbo].[dm_os_schedulers]
(
[snapshot_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [snapshotid_idx]    Script Date: 29/10/2013 9:38:40 AM ******/
CREATE NONCLUSTERED INDEX [snapshotid_idx] ON [dbo].[DM_OS_WAIT_STATS]
(
[snapshot_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [dm_oswaitingtasks_idx]    Script Date: 29/10/2013 9:38:40 AM ******/
CREATE NONCLUSTERED INDEX [dm_oswaitingtasks_idx] ON [dbo].[dm_os_waiting_tasks]
(
[snapshot_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [dm_sysprocesses_idx]    Script Date: 29/10/2013 9:38:40 AM ******/
CREATE NONCLUSTERED INDEX [dm_sysprocesses_idx] ON [dbo].[dm_sysprocesses]
(
[snapshot_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [snapshot_idx]    Script Date: 29/10/2013 9:38:40 AM ******/
CREATE NONCLUSTERED INDEX [snapshot_idx] ON [dbo].[snapshot]
(
[snapshot_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO


Comments

Popular posts from this blog

Opatch apply/lsinventory error: oneoff is corrupted or does not exist

I am applying the quarterly patch for 19c RDBMS, I tried using napply but failed, but somehow it corrupted the inventory though nothing applied. further apply and lsinventory command ran into error like this: $ ./OPatch/opatch lsinventory Oracle Interim Patch Installer version 12.2.0.1.21 Copyright (c) 2020, Oracle Corporation.  All rights reserved. Oracle Home       : /u02/app/oracle/19.0.0 Central Inventory : /u01/app/oraInventory    from           : /u02/app/oracle/19.0.0/oraInst.loc OPatch version    : 12.2.0.1.21 OUI version       : 12.2.0.7.0 Log file location : /u02/app/oracle/19.0.0/cfgtoollogs/opatch/opatch2020-09-08_13-35-59PM_1.log Lsinventory Output file location : /u02/app/oracle/19.0.0/cfgtoollogs/opatch/lsinv/lsinventory2020-09-08_13-35-59PM.txt -------------------------------------------------------------------------------- Inventory load failed... OPatch cannot load inventory ...

oracle dba_hist_sysmetric_summary

found this blog is helpful to get CPU and IO statistics on oracle database. http://shob-dbadmin.blogspot.ca/2012/12/how-to-find-total-io-of-database.html courtesy to  Shomil Bansal , below are hist writing, not mine. How to find total IO of the database instance Total IO of database instance is sum of the physical reads, physical writes and redo writes. There are several views to find these values. v$sysmetric  - Reports metric values for only the most current time sample 60 secs. v$sysmetric_summary  - Reports metric values for time sample of 1 hour. v$sysmetric_history  - Reports metric values every 60 sec from the time instance is up. Better way to analyse IO using this view to take deltas between two time periods. dba_hist_sysmetric_history  - All the above views are refreshed when the instance is restarted. This view, part of AWR, stores the historical stats. I have used this view for my report. Query: ====== set lines 350...

ORA_RMAN_SGA_TARGET

assume that we lost all the files of oracle database but we do have rman backup, when trying to bring up a dummy database before restore start, I get this error. RMAN> startup nomount force; WARNING: cannot translate ORA_RMAN_SGA_TARGET value startup failed: ORA-01078: failure in processing system parameters ORA-01565: error in identifying file '+DATA/PROD/spfilePROD.ora' ORA-17503: ksfdopn:2 Failed to open file +DATA/PROD/spfilePROD.ora ORA-15056: additional error message ORA-17503: ksfdopn:DGOpenFile05 Failed to open file +DATA/prod/spfileprod.ora ORA-17503: ksfdopn:2 Failed to open file +DATA/prod/spfileprod.ora ORA-15173: entry 'spfileprod.ora' does not exist in directory 'prod' ORA-06512: at line 4 starting Oracle instance without parameter file for retrival of spfile RMAN-00571: =========================================================== RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS =============== RMAN-00571: =================================...