Solved

Pivot

Posted on 2014-12-04
11
121 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 46

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
 
LVL 46

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
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

 
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 46

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 46

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now