Forwarding Pointers in Heaps

When no clustered index is defined on a table, that table is said to be a Heap. Heaps are not ideal when it comes to performance but there are lots of instances where you have heaps.  

Operations on heaps

INSERT -  New rows can be placed in the first available page with sufficient space. So whenever a new row is inserted most probably it will be added to the last page.

UPDATE – Rows can either remain on the same page if it fits in the page after the update, if not it will be removed from the current page and placed on the first available page with sufficient space. This is where forwarding pointers comes into the picture. We will be discussing this later.

DELETE – Data is not overwritten, space is just flagged as available for reuse.

SELECT – The entire table will need to be read for most queries (ie a table scan is performed) if no indexes are available

Forwarding Pointers

There can be non-clustered indexes in the Heap. So in case a record has to move to new page, at the old location of the row it will leave a forwarding pointer. This mechanism is to stop the updating of non-clustered indexes as it can still point to the old location. So the modification is less costly than if all non-clustered indexes needed to reflect the row move.

Let us try this with sample data as shown below.

CREATE TABLE Test
(ID BIGINT IDENTITY(1,1),
Description VARCHAR(200),
Type int,
sys_datetime datetime default getdate()
)
CREATE INDEX idx_sys_Datetime ON Test(sys_datetime)
CREATE INDEX idx_type ON Test(Type)
DECLARE @i int =0
WHILE @i < 100
BEGIN
   INSERT INTO Test
   (Description,Type)
   VALUES  ( 'Sample Data for ' + CAST(@i as varchar(10)),
   RAND(1) * 100 )
   SET @i = @I + 1
END

After this let us check some information about the Heap using following query.

SELECT
    OBJECT_NAME(ps.object_id) as TableName,
    i.name as IndexName,
    ps.index_type_desc,
    ps.page_count,
    ps.avg_fragmentation_in_percent,
    ps.forwarded_record_count
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, 'DETAILED') AS ps
INNER JOIN sys.indexes AS i
    ON ps.OBJECT_ID = i.OBJECT_ID  AND ps.index_id = i.index_id
WHERE OBJECT_NAME(ps.object_id) = 'test'

You can see there 0 forwarded records for the HEAP.

Then let us add a column so that rows will not be fitting on a page and forces SQL Server to move those rows to another page :

ALTER TABLE Test ALTER COLUMN Description char(400);

Thus, you now you have 85 forwarded records.

Forwarding pointers are common performance problem in Heaps. SQL Server 2008 introduced ALTER TABLE ... REBUILD which removes all forwarding pointers and performs a rebuild:

ALTER TABLE test REBUILD 

Now let us re-examine the forwarded recount again which will be back to zero.


Multiple forwarding pointers

Let us assume a row has to be moved another page and it has to be moved again. Technically there can be two options for this:

  1. Another forwarding pointer is created and updated in the location where the row was prior to the move.
  2. Simply update where the row was originally was.

Whatever the option you select, indexes need not to be updated. SQL Server by default uses the second method which is updating the original forwarding pointer.  By using this mechanism, performance will not be affected by having to follow a chain of forwarding pointers.




Related Articles :

2 Responses to “Forwarding Pointers in Heaps”

  1. Thanks, this is a mechanism I never knew existed!

    The tricky part seems is that this affects performance over time and will not show up in initial performance tests, if not prepared to discover this specifically.

    It also leads me to think that when a heap is the correct structure, make sure it works with fixed width records, all non-nullable, to get more predictable performance and bypass this logic completely.

    • Space for fixed-width records is always reserved on the page regardless of whether the column is nullable or whether or not it is NULL. Having all columns in a heap be declared using fixed-width data types would be a way to avoid forwarding pointers however for staging tables you can bump into the 8K page-size-limit depending on the data.

Software Reviews | Book Reviews | FAQs | Tips | Articles | Performance Tuning | Audit | BI | Clustering | Developer | Reporting | DBA | ASP.NET Ado | Views tips | | Developer FAQs | Replication Tips | OS Tips | Misc Tips | Index Tuning Tips | Hints Tips | High Availability Tips | Hardware Tips | ETL Tips | Components Tips | Configuration Tips | App Dev Tips | OLAP Tips | Admin Tips | Software Reviews | Error | Clustering FAQs | Performance Tuning FAQs | DBA FAQs |