Quantcast
Channel: THWACK: All Content - All Communities
Viewing all articles
Browse latest Browse all 20518

Custom Report - Top SQL for Database User

$
0
0

Description

This procedure, when executed, will return all SQL statements for the given date range and database user ordered by the metric of choice. See comments inside the procedure for more details on the parameters and methods for calling. Install the stored procedure into the Ignite repository database.

 

Stored Procedure

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

DROP PROC TopSQLforDBUser

GO

 

-- TopSQLforDBUser PROC

--

-- PARMS '@FROM_DATE', '@TO_DATE', '@DBNAME' , '@DBUSER'

-- The @DBNAME can be found from the following query: Select NAME from ignite.cond

-- EXAMPLE:

--

-- EXEC TopSQLforDBUser '2014-2-23 00:00','2014-12-30 00:00','DBNAMEHERE','sa'

 

create PROC TopSQLforDBUser

(@FROM_DATE VARCHAR(16), @TO_DATE VARCHAR(16), @DBNAME VARCHAR(50), @DBUSER VARCHAR(50))

AS

DECLARE

@SQL VARCHAR(4000),

@DBID VARCHAR(3)

BEGIN

 

SELECT @DBID = ID FROM COND WHERE NAME = @DBNAME

 

SELECT @SQL = 'SELECT ISNULL(N.NAME, SW.IZHO) HASH_OR_NAME, SUM(SW.QP/100) TIMESECS, ST.ST SQLTEXT ' +

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

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

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

   'WHERE SW.D BETWEEN ' +

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

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

   'AND UPPER(U.NAME) = UPPER(' + '''' + @DBUSER + '''' + ') ' +

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

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

EXEC (@SQL)

END

GO


Viewing all articles
Browse latest Browse all 20518

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>