Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 November SQL Server: Script to find TOP 20 most used or executed SQL Queries

SQL Server: Script to find TOP 20 most used or executed SQL Queries

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing one SQL Script to find TOP 20 most used or executed query in SQL Server.

Database Administrator always prefers to find most long running queries, but It is also required to check most executed queries.

Because most executed queries might take average 1 second or 2 second for execution, but total number execution count 10 times than few long running queries.

If we find the total number of execution count more than our expectation, we can take the necessary decision like: remove redundant queries from the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SELECT TOP 20
deqs.execution_count AS TotalExecutionTime
,OBJECT_NAME(objectid) AS ObjectName
,QueryText = SUBSTRING(
dest.text,
deqs.statement_start_offset/2,
(CASE WHEN deqs.statement_end_offset = -1
THEN len(CONVERT(nvarchar(MAX), dest.text)) * 2
ELSE deqs.statement_end_offset
END - deqs.statement_start_offset)/2)
,DatabaseName = db_name(dest.dbid)
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.execution_count DESC

You can also access few related articles:

SQL Server: Script to find top 20 running query, which are utilizing more CPU

SQL Server: Script to find top 20 Stored Procedure, which are utilizing more CPU

Nov 23, 2016Anvesh Patel
PostgreSQL: Force on Planner to use Sequential Scan or Index ScanPostgreSQL: Copy Database to another Server in Windows (pg_dump - backup & restore)
Comments: 4
  1. dianl
    May 13, 2018 at 6:43 pm

    great help Anvesh, thanks for this script

  2. AG
    December 5, 2018 at 6:40 am

    Hi please tell me time from which we can count execution_count date time(from that time to which time its showing)

  3. STL News
    May 11, 2020 at 3:30 pm

    Eхceⅼlent post. I was checking continuously this blog and I’m
    impresseԁ! Ⅴery seful information

  4. Louis
    May 18, 2020 at 12:15 am

    Gooⅾ post. I learn something new and challenging

Anvesh Patel
Anvesh Patel

Database Engineer

November 23, 2016 SQL Server, SQL Server DBA ScriptAnvesh Patel, database, database research and development, dbrnd, dm_exec_query_stats, frequent executed queries, most used queries, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....