Recently, while I was looking at DatabaseLog table in
AdventureWorks database, I found something wired. Before going further, let us
look at some important information about this table. I publish only the information
of my concern.
First of all, this table does not have a clustered index. It
has DatabaseLogID which is an identity column which is also the primary key,
non-clustered. The next point is that it has variable length columns. The wired thing came, when I executed SELCT *
from DatabaseLog statement.
To my surprise, the data didn’t come in ascending order of databaseLogID. In fact it has no order. This wake my curiosity and I started digging further.
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
CREATE TABLE dbo.DatabaseLog(
DatabaseLogID int IDENTITY(1,1) NOT NULL,
PostTime datetime NOT NULL,
DatabaseUser sysname NOT NULL,
Event sysname
NOT NULL,
Schema sysname
NULL,
Object sysname
NULL,
TSQL nvarchar(max) NOT NULL,
XmlEvent xml NOT NULL,
CONSTRAINT PK_DatabaseLog_DatabaseLogID PRIMARY
KEY NONCLUSTERED
(
DatabaseLogID ASC
)
To my surprise, the data didn’t come in ascending order of databaseLogID. In fact it has no order. This wake my curiosity and I started digging further.
When the table does not have a clustered index, the table is considered a heap. It is not just the name. Consider a heap of stones stored along the roadside for some repair. There is no order, and any stone can be considered as the first. In database world, heap table also considered as the same.
But my question was different. Okay, there is no order, but I am getting the results in some order. Not the primary key order, but some other order. What is that order?


