Wednesday, January 2, 2019

Query SQL Server transactions - per day, hour and minute

This post will help you query your SQL Server for transaction count at both the server level and for each of the databases.  The query returns transaction statistics per day, hour and minute since the time of the last service restart.


/* SQL Server transactions per day/hour/minute using sys.dm_os_performance_counters */

       -- declarations
       DECLARE      
              @Days SMALLINT,
              @Hours INT,
              @Minutes BIGINT,
              @LastRestart DATETIME;

       -- get last restart date
       SELECT 
              @Days = DATEDIFF(D, create_date, GETDATE()),@LastRestart = create_date
       FROM   
              sys.databases
       WHERE  
              database_id = 2;

       -- collect days/hours since last restart
       SELECT @Days = CASE WHEN @Days = 0 THEN 1 ELSE @Days END;
       SELECT @Hours = @Days * 24;
       SELECT @Minutes = @Hours * 60;


       -- trans since last restart
       SELECT 
              @LastRestart [LastRestart],
              @@servername [Instance],
              cntr_value [TotalTransSinceLastRestart],
              cntr_value / @Days   [AvgTransPerDay],
              cntr_value / @Hours  [AvgTransPerHour],
              cntr_value / @Minutes [AvgTransPerMinute]
       FROM
              sys.dm_os_performance_counters
       WHERE  
              counter_name = 'Transactions/sec'
              AND instance_name = '_Total';


       -- trans since last restart per database
       SELECT 
              @LastRestart [LastRestart],
              @@servername [Instance],
              instance_name [Database_Name],
              cntr_value [TotalTransSinceLastRestart],
              cntr_value / @Days [AvgTransPerDay],
              cntr_value / @Hours  [AvgTransPerHour],
              cntr_value / @Minutes [AvgTransPerMinute]
       FROM  
              sys.dm_os_performance_counters
       WHERE  
              counter_name = 'Transactions/sec'
              AND instance_name <> '_Total'
       ORDER BY
              cntr_value DESC;


Your output will be similar to this:

















Take a look at this for more details on the dm_os_performance_counters DMV:     sys.dm_os_performance_counters

23 comments:

  1. You have provided a nice article, Thank you very much for this one. And I hope this will be useful for many people. And I am waiting for your next post keep on updating these kinds of knowledgeable things
    SEO Training in Chennai
    SEO Course in Chennai
    Cloud Computing Training in Chennai
    Data Science Course in Chennai
    Devops Training in Chennai
    Digital Marketing Course in Chennai
    Selenium Training in Chennai
    SEO Training in Tnagar

    ReplyDelete
  2. Thank you both for reading. I'm glad to have helped!

    ReplyDelete
  3. Thank you! I am very glad to have helped.

    ReplyDelete
  4. I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it.. שרת וירטואלי

    ReplyDelete
  5. very interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post! mélybölcsős szállítás Europa-Road Kft

    ReplyDelete
  6. Really wonderful blog! Thanks for taking your valuable time to share this with us. Keep us updated with more such blogs.
    AWS Course in Chennai
    AWS Online Course
    AWS Course in Coimbatore

    ReplyDelete
  7. Great post. It is really helpful for me.keep sharing such a worthy information..
    Web Development courses in Chennai

    ReplyDelete
  8. I was basically inspecting through the web filtering for certain data and ran over your blog. I am flabbergasted by the data that you have on this blog. It shows how well you welcome this subject. Bookmarked this page, will return for extra. data science course in jaipur

    ReplyDelete
  9. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing. python course in delhi

    ReplyDelete
  10. Amazingly by and large very interesting post. I was looking for such an information and thoroughly enjoyed examining this one.
    Keep posting. An obligation of appreciation is all together for sharing.
    business analytics course in gwalior


    ReplyDelete
  11. Hi there, You have done a fantastic job. I’ll certainly digg it and personally suggest to my friends. I am sure they will be benefited from this site.|data science course in jodhpur

    ReplyDelete
  12. if i need it for a full month?

    ReplyDelete
  13. Though I knew a bit of data science already, this post has been so enlightening for me, as I got to understand some of the latest trends in this field through this. I had seen many data science and analytics-based courses online when I was looking for a course for my friend; however, this post guided me in the right direction, and I was able to help my friend with the right suggestion, thanks to this informative post.
    best training institute for data analytics in pune

    ReplyDelete
  14. Though I knew a bit of data science already, this post has been so enlightening for me, as I got to understand some of the latest trends in this field through this. I had seen many data science and analytics-based courses online when I was looking for a course for my friend; however, this post guided me in the right direction, and I was able to help my friend with the right suggestion, thanks to this informative post.
    best training institute for data analytics in pune

    ReplyDelete
  15. excellent post. Keep up the good work.
    SQL Course in Pune

    ReplyDelete
  16. Its really helpful. Thanks for such an amazing article.

    ReplyDelete
  17. Excellent blog! Appreciate you sharing such a wonderful piece of content. Please continue to share valuable information aligning with my interests.

    best software training institute in chennai

    ReplyDelete
  18. Thank you all for the great comments! I am glad to so many readers!

    ReplyDelete