Solved

Pivot

Posted on 2014-12-04
11
118 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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, Vitor - didn't see your post until afterwards.
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
:-)
0
 

Author Comment

by:shahin37
Comment Utility
Hi gues,

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

Regards
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

10 Experts available now in Live!

Get 1:1 Help Now