Hi Friends,
There may be various cases where you can use the term records and rows interchangeably in SQL Server database world. Can we use the term records and Rows interchangeably in the case of Heap? We will try to find out the answer of this question in this post.
We know that Heap in SQL Server is a table structure without clustered index. Let me create one Heap with some data. After that I’ll try to find out the Row count from heap and Record count on the same heap by using sys.dm_db_index_physical_stats dmv.
use master go IF EXISTS (Select database_id from sys.databases where name='HeapTest') Drop database HeapTest Create database HeapTest go use HeapTest go create table utHeap ( fname char(100), mname char(100), lname char(100), city char(50), qualities varchar(8000) ) go insert into utHeap values ('prince','kumar','rastogi','gurgaon','SQLDBA') go insert into utHeap values ('asheesh','kumar','rastogi','delhi','Teacher') go 20 Select record_count,forwarded_record_count,* from sys.dm_db_index_physical_stats(DB_ID('HeapTest'),OBJECT_ID('utHeap'),NULL,NULL,'DETAILED') go Select count(*) as Row_Count from utHeap go
From the above output you can see the number of records and numbers of rows both are same. Now let’s try to update qualities column with some large data by using replicate function. Then again we will compare number of records and number of rows.
Use HeapTest go update utHeap set qualities=replicate('I am working as SQL Server DBA in a MultiNational Company',25) where fname='prince' go Select record_count,forwarded_record_count,* from sys.dm_db_index_physical_stats(DB_ID('HeapTest'),OBJECT_ID('utHeap'),NULL,NULL,'DETAILED') go Select count(*) as Row_Count from utHeap go
Now in the above output record count and row count both are different. I hope you got the answer for question which is asked in the first block of this post. Now the next question will come in your mind: what is the reason behind this. Reason is also there in both the above outputs. Go to above outputs back and check the value in column forwarded_record_count. First output showing value 0 while second output is showing value 1.
As per BOL, forwarded record count is the Number of records in a heap that have forward pointers to another data location. (This state occurs during an update, when there is not enough room to store the new row in the original location.)
HAPPY LEARNING!
Regards:
Prince Kumar Rastogi
Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook
Follow Prince Rastogi on Twitter | Follow Prince Rastogi on FaceBook
NIce post 🙂