SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Training
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
SQL Azure
Developer
General DBA
ASP.NET / ADO.NET
SQL Azure

USEFUL SITES :

ASP.NET Tutorials
Windows and SQL Azure Tutorials
Cloud Hosting Magazine
SharePoint Tutorials
Windows Server Help

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server

More     
 
Latest FAQ's

Add Node to A SQL Server failover Cluster failed with invalid ...
SQL Server Destination remote server error
Setting Up Data And Log Files For SQL Server
Will Check Constraints Improve Database Performance?

More     
   
Latest Software Reviews

dbForge Review
Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Doc 2008

More     

articles >> performance tuning >> Speeding up the Performance of Table Counts ...

Speeding up the Performance of Table Counts in SQL Server 2005

By : Mohamed Hassan
Sep 10, 2008

Introduction
When we want to get the row count of a table, we use the transact-SQL function count (*).  In the case of large table which may contain millions of rows, it can take a long time to return the row count of the whole table, and that leads to slow performance for the query.

Count () function
Every DBA know how to use count (*) and know how suffering the performance.  SQL server makes a full index/ table scan to return the total table's row count. There was advice for the DBA to avoid using the aggregate function count () for the whole table because it affects the database performance.  When the following query is executed in query analyzer for the demo database AdventureWorks:

use AdventureWorks
go
select count (*) from Sales.SalesOrderDetail

The Query analyzer returns 121317 rows.

When we click the icon "display estimated execution plan" in the toolbar of the management studio for SQL server 2005, we get the following graph:


 
As we see in the figure from right to left, the following actions are applied:  

  • Index scan for the whole table, which is time consuming process.
  • Apply stream aggregate in the next step.

The New Method row_count ()
The information included in Object Catalog Views of SQL Server 2005: sys.partitions and  sys.allocation_units  are used to get the count of the rows of the whole table. This function can be used in SQL Server 2005. 

sys.partitions  View
sys.partitions View contains a row for each partition of all the tables and indexes in the database.  All tables and indexes in SQL Server 2005 contain at least one partition in that view, even if they are not explicitly partitioned.

The View includes the following fields which are used in the new method:

Field Name

Data Type

Description

partition_id bigint ID of the partition. Is unique within a database.
object_id int ID of the table to which this partition belongs. Every table is composed from  one partition at least.
index_id   ID of the index within the object to which this partition belongs.
0 : heap table
1: with clustered index
rows     bigint number of rows in the partition.

 


sys.allocation_units
The sys.allocation_units view contains a row for each allocation unit in the database.

The View includes the following fields which are used in the new method:

Field Name

Data Type

Description

container_id bigint The container_id = sys.partitions.partition_id.
Type tinyint Type of allocation unit:
0 = Dropped
1 = In-row data (all data types, except LOB data types)
2 = Large object (LOB) data (text, ntext, image, xml)
3 = Row-overflow data

In the new UDF row_count,  [sys.partitions] view is joined with [sys.allocation_units] view. The filter of selection based on the following criteria: 

 

 

  • [sys.allocation_units].type = 1 which retrieve only the in-row data except the large object (LOB) of type like text, ntext, image.
  • [sys.partitions].index_id in (0,1): 0 is the heap table , 1 : Clustered table
  • [sys.partitions].rows is not null

The UDF row_count is executed in every database , and is given a permission public. The UDF row_count is described in the next section.


    Next Page>>    








C# Help and Tutorials | PHP MySQL Tutorial | Sharepoint Tutorial | Azure Tutorial | Cloud Hosting Magazine | ASP.NET Tutorials | Windows Server Help | Windows Phone Pro | Silverlight Ace | Visual Studio Tutorials | Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 2010 Jude O'Kelly. All rights reserved