• SQL Server
  • Log Shipping Tricks Demo
  • SQLCruise Alaska 2012 Pics
SQLSoldier News From the Frontlines

Getting Fast Counts of Large Service Broker Queues

January 18, 2011 4:15 pm / 1 Comment / SQLSoldier


Getting Fast Counts of Large Service Broker Queues

This question regarding getting a fast count from a service broker queue came in through a discussion group.

Question: Fast way to get a count of records from an SSB queue

Subject: Fast way to get a count of records from an SSB queue


I’m looking for tips or pointers on how to optimize for reading the count of records in an SSB queue. We’re seeing as the queue grows to 1M records a simple SELECT COUNT(*) WITH(NOLOCK) is taking an inordinate amount of time – i.e. 30-50 minutes under load.

Any pointers or suggestions?

My Solution

Service broker queues are stored as internal tables. The queue is listed as the parent object of the hidden table. By linking the internal table to sys.partitions, you can use the sys.partitions.rows column as a quick count bearing in mind that it may not always be 100%. The accuracy is probably not an issue for you if you are using the nolock already as that may count records that have not been committed yet.

Select rows
From sys.partitions P
Inner Join sys.internal_tables IT On IT.object_id = P.object_id
Where IT.parent_object_id = object_id('<SB Queue Name>')
And P.index_id In (1, 0)

Edit: Another Version of Query

In follow-up conversation, I wrote a version of the query that can be used to query the count of service broker queues in a different database. Since the OBJECT_ID funtion does not accept a database ID parameter like OBJECT_NAME does, we instead need to join to the sys.service_queues view to link the queue to the internal table. For the below example query, I am querying for a service broker queue in msdb.

Select P.rows
From msdb.sys.partitions P
Inner Join msdb.sys.internal_tables IT On IT.object_id = P.object_id
Inner Join msdb.sys.service_queues SQ On SQ.object_id = IT.parent_object_id
Where SQ.name = '<Queue name>'
And P.index_id In (1, 0)
Posted in: SQL Server / Tagged: Internals, T-SQL, Tips & Tricks

One Thought on “Getting Fast Counts of Large Service Broker Queues”

  1. Pingback: Getting Fast Counts of Large Service Broker Queues

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation

← Previous Post
Next Post →
<

Remote DBA Services
- serious SQL Server expertise for less than a full-time DBA
My Articles
 
My Book
Check out my interview on

Extreme Data Recovery (with Argenis Fernandez)
10 Things all BI System Administrators Should Know
Upcoming Events
    All events shown in Pacific Time

    No events to show

RSS My SQL Server Magazine Articles

  • Database Mirroring for Disaster Recovery September 16, 2011
  • Comparative Review: Database Schema Comparison Tools August 24, 2011
  • 3 Log Shipping Techniques June 22, 2011
  • Hardening SQL Server June 20, 2011
  • Review: ScriptLogic Security Explorer for SQL Server February 8, 2011

Tags

31 Days of Disaster Recovery Architecture Automation CDC & Change Tracking Data Architecture VC Database Mirroring DBCC Denali Disaster Recovery Dynamic Management Views Extended Events Gamers & Geeks General Discussion High Availability How do I ... ? Humor Idera ACE Program Internals MCM Meme Monday Performance & Optimization PowerShell Professional Development Replication Security SQLBits SQL PASS SQL PASS Summit SQLRally SQL Saturday SQL Server Magazine SQL University SSAS & BI SSIS SSMS SSRS T-SQL T-SQL Tuesday tempDB Tips & Tricks Travel Troubleshooting Undocumented Stuff Whitepapers XML in SQL

News

Download my Powershell Scripts

The following scripts can be downloaded as text files. You will need to change the file extension to .ps1 in order to execute them.

Backup a database
Restore a database
Scan a server to find a free port
Query DNS to get the FQDN of a server


To see some examples of my other forms of writing, please visit my page on WritersCafe.org. It is almost exclusively horror fiction, but I sometimes throw other things in there too from time to time. There's one science fiction story, a couple of poems, and quite a few humor pieces as well.


Look for me in the SQL Q&A section of the August, 2007 issue of TechNet Magazine.
August issue of TechNet Magazine's SQL Q&A column

Protect our Heroes

© Copyright 2012 - Robert L Davis
Infinity Theme by DesignCoral / WordPress

Twitter Twitter 
LinkedIn LinkedIn 
TLF TLF RSS RSS 
WritersCafe WritersCafe 
SQLPASS SQLPASS 
Facebook Facebook
grab this