Quantcast
Channel: THWACK: Message List
Viewing all articles
Browse latest Browse all 20396

Re: Custom report: Top SQL per machine

$
0
0

Hi glimbeek,

 

I believe this code should do what you're requesting. The structure for our repository when registering new instances follows the syntax of TABLENAME_DBID. You can potentially script a COALESCE function to do a similar job, but I found the most luck with a cursor and temp table to store the values. You could also play with the type of temp table depending on your environment and alter it from variable to local or global, but may have to add in some drops or truncates depending on how you want to set up the report. Please let us know if this didn't answer your question.

 

IF (SELECT COUNT(*) FROM SYS.OBJECTS WHERE NAME LIKE 'TopSQLforMSSQLMachine') = 1

DROP PROC TopSQLforMSSQLMachine

GO

 

-- TopSQLforMSSQLMachine PROC

--

-- PARMS '@FROM_DATE', '@TO_DATE', '@MACHINENAME'

-- The @MACHINENAME can be found from the following query: Select CONN_HOST from cond

-- EXAMPLE:

--

-- EXEC TopSQLforMSSQLMachine '2014-12-23 00:00','2014-12-30 00:00','MACHINENAMEHERE'

 

 

create PROC TopSQLforMSSQLMachine

(@FROM_DATE VARCHAR(16), @TO_DATE VARCHAR(16), @MACHINENAME VARCHAR(50))

AS

BEGIN

 

 

DECLARE @HostGatheredCursorData table(

tablename nvarchar(100),

hash_or_name nvarchar(100),

timesecs nvarchar(100),

sqltext nvarchar(max)

);

 

 

DECLARE @id nvarchar(1000);

 

 

DECLARE MachineGatherCursor CURSOR FAST_FORWARD FOR

 

 

select id from cond with (nolock)

where CONN_HOST = @MACHINENAME

 

 

OPEN MachineGatherCursor;

 

 

FETCH NEXT FROM MachineGatherCursor INTO @id;

 

 

DECLARE @Sql nvarchar(max);

 

 

WHILE @@FETCH_STATUS = 0

BEGIN

 

 

SET @sql =

 

 

N'SELECT ' +

  @id + N',

  ISNULL(N.NAME, SW.IZHO) HASH_OR_NAME,

  SUM(SW.QP/100) TIMESECS,

  ST.ST SQLTEXT ' +

'FROM CONSW_' + @id + ' SW LEFT OUTER JOIN CON_SQL_NAME N ON SW.IZHO = N.HASH ' +

'LEFT OUTER JOIN CONST_' + @id + ' ST ON SW.IZHO = ST.H ' +

'INNER JOIN CONU_' + @id + ' U ON SW.XCUW = U.ID ' +

'WHERE SW.D BETWEEN ' +

  'CONVERT(DATETIME,' + '''' + @FROM_DATE + '''' + ', 101) AND ' +

  'CONVERT(DATETIME,' + '''' + @TO_DATE + '''' + ', 101) ' +

'GROUP BY ISNULL(N.NAME, SW.IZHO), ST.ST ' +

'ORDER BY SUM(SW.QP/100) DESC '

 

 

insert into @HostGatheredCursorData

 

 

EXEC(@sql)

 

 

FETCH NEXT FROM MachineGatherCursor INTO @id;

END

CLOSE MachineGatherCursor;

DEALLOCATE MachineGatherCursor;

 

 

SELECT

  COND.NAME as "SQLInstance",

  HGCD.Hash_Or_Name,

  HGCD.TimeSecs,

  HGCD.SQLText

FROM @HostGatheredCursorData as HGCD

inner join cond on cond.ID = HGCD.tablename;

 

 

END


Viewing all articles
Browse latest Browse all 20396

Trending Articles