Solved

Pivot

Posted on 2014-12-04
11
127 Views
Last Modified: 2014-12-07
Hi Experts

I need a pivot syntaxt to have the performance counters in rows from sys.dm_os_performance_counters

I attached the figure

 counters.jpg
Regards
0
Comment
Question by:shahin37
  • 6
  • 4
11 Comments
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40482549
Like this?
SELECT *
FROM (SELECT object_name, instance_name, counter_name, cntr_value
	FROM sys.dm_os_performance_counters) AS os_pc
PIVOT (SUM(cntr_value)
FOR counter_name IN ([Page life expectancy],[Buffer cache hit ratio],[Lock Waits/sec])) as pvt

Open in new window

0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40482575
There is, but you will have to be a bit more specific.

Here's the basic code:

With MyTable as (
select * from sys.dm_os_performance_counters
where [counter_name] in ('Page life expectancy','Buffer cache hit ratio','Lock Waits/sec') and [cntr_value] is not null)
, myPivot as
(select * from MyTable 
pivot(sum([cntr_value]) for [counter_name] in ([Page life expectancy],[Buffer cache hit ratio],[Lock Waits/sec])) as pvt)
Select object_name, instance_name, cntr_type, isnull([Page life expectancy],0) as [Page life expectancy], 
isnull([Buffer cache hit ratio],0) as [Buffer cache hit ratio], 
isnull([Lock Waits/sec],0) as [Lock Waits/sec] from myPivot

Open in new window


This gives you this:

object_name	instance_name	cntr_type	Page life expectancy	Buffer cache hit ratio	Lock Waits/sec
SQLServer:Buffer Manager    512	0	0
SQLServer:Buffer Manager    0	22	0
SQLServer:Buffer Node      	000                                                                                                                             	65792	512	0	0
SQLServer:Locks _Total                                                                                                                          	272696576	0	0	43
SQLServer:Locks AllocUnit                                                                                                                       	272696576	0	0	0
SQLServer:Locks Application                                                                                                                     	272696576	0	0	0
SQLServer:Locks Database                                                                                                                        	272696576	0	0	24
SQLServer:Locks Extent                                                                                                                          	272696576	0	0	0
SQLServer:Locks File                                                                                                                            	272696576	0	0	0
SQLServer:Locks HoBT                                                                                                                            	272696576	0	0	0
SQLServer:Locks Key                                                                                                                             	272696576	0	0	3
SQLServer:Locks Metadata                                                                                                                        	272696576	0	0	10
SQLServer:Locks Object                                                                                                                          	272696576	0	0	6
SQLServer:Locks OibTrackTbl                                                                                                                   	272696576	0	0	0
SQLServer:Locks Page                                                                                                                            	272696576	0	0	0
SQLServer:Locks RID                                                                                                                             	272696576	0	0	0

Open in new window


As you can see, there is no such thing as a single "Lock Waits/sec", so I have listed all of them. Equally, the Page life expectancy is in both the Buffer Manager and Buffer Node.

If all you want is the Buffer Manager and the _Total, then here is your code:

With MyTable as (
select [counter_name],[cntr_value] from sys.dm_os_performance_counters
where [counter_name] in ('Page life expectancy','Buffer cache hit ratio','Lock Waits/sec') and [cntr_value] is not null
and ([object_name] = 'SQLServer:Buffer Manager' or instance_name = '_Total'))
, myPivot as
(select * from MyTable 
pivot(sum([cntr_value]) for [counter_name] in ([Page life expectancy],[Buffer cache hit ratio],[Lock Waits/sec])) as pvt)
Select isnull([Page life expectancy],0) as [Page life expectancy], 
isnull([Buffer cache hit ratio],0) as [Buffer cache hit ratio], 
isnull([Lock Waits/sec],0) as [Lock Waits/sec] from myPivot

Open in new window


This gives:

Page life expectancy      Buffer cache hit ratio      Lock Waits/sec
591                                      92                                        43
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40482579
Sorry, Vitor - didn't see your post until afterwards.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40482604
As you can see, there is no such thing as a single "Lock Waits/sec",
Phillip, which version are you running? In SQL Server 2008R2 there's in SQLServer:Locks.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40482614
Vitor, I've got :

SQLServer:Locks _Total                                                                                                                          	272696576	0	0	43
SQLServer:Locks AllocUnit                                                                                                                       	272696576	0	0	0
SQLServer:Locks Application                                                                                                                     	272696576	0	0	0
SQLServer:Locks Database                                                                                                                        	272696576	0	0	24
SQLServer:Locks Extent                                                                                                                          	272696576	0	0	0
SQLServer:Locks File                                                                                                                            	272696576	0	0	0
SQLServer:Locks HoBT                                                                                                                            	272696576	0	0	0
SQLServer:Locks Key                                                                                                                             	272696576	0	0	3
SQLServer:Locks Metadata                                                                                                                        	272696576	0	0	10
SQLServer:Locks Object                                                                                                                          	272696576	0	0	6
SQLServer:Locks OibTrackTbl                                                                                                                   	272696576	0	0	0
SQLServer:Locks Page                                                                                                                            	272696576	0	0	0
SQLServer:Locks RID  

Open in new window

0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40482622
If shahin37 adds all that up, he'll get to double the right answer.

I'm trying to explain to shahin37 that, if you want a one row result, you have to understand that the answer is a bit more...nuanced.
0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40482648
shahin37 need to add a WHERE clause for filtering the instance_name or else he won't has the right values.
And this is what it looks like in my MSSQL 2008R2 instance:
Capture.PNG
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40482658
Exactly - so it's probably lines 1, 2 and 15 he's interested in, as opposed to lines 3-14; but he needs to know about them and then decide.
0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40482669
Well, if he don't need the instance name then MAX() will solve the problem:
SELECT *
FROM (SELECT object_name, counter_name, MAX(cntr_value) max_value
	FROM sys.dm_os_performance_counters
	GROUP BY object_name, counter_name) AS os_pc
PIVOT (SUM(max_value)
FOR counter_name IN ([Page life expectancy],[Buffer cache hit ratio],[Lock Waits/sec])) as pvt

Open in new window

0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40482686
:-)
0
 

Author Comment

by:shahin37
ID: 40486074
Hi gues,

thanks for your prompt responces
I think I can use philip Syntax  by changing the sum to max .

Regards
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question