Q: What will be the result order of a select (without ORDER BY) from a table - with/without Identity and CLUSTERED INDEX?
And when we will add Identity and/or CLUSTERED INDEX - how will this affect?
A: Follow the next selects:
create table dbo.NoID
( someSTR nvarchar(50),
someDate datetime
) ON [PRIMARY];
insert into NoID (someSTR, someDate) values ('aaa', GETDATE())
insert into NoID (someSTR, someDate) values ('bbb', GETDATE())
insert into NoID (someSTR, someDate) values ('ccc', GETDATE())
insert into NoID (someSTR, someDate) values ('zzz', GETDATE())
insert into NoID (someSTR, someDate) values ('ddd', GETDATE())
insert into NoID (someSTR, someDate) values ('eee', '2017-01-01 00:00:00')
select * from NoID
alter table dbo.NoID add ID int identity(1,1) not null
select * from NoID
create table dbo.NoID_PKClusteredNotOrderedByTime
(
someSTR nvarchar(50),
someDate datetime,
CONSTRAINT [PK_NoID_PKClusteredNotOrderedByTime] PRIMARY KEY CLUSTERED
(
[someSTR] ASC
) ON [PRIMARY]
) ON [PRIMARY];
insert into NoID_PKClusteredNotOrderedByTime (someSTR, someDate) values ('aaa', GETDATE())
insert into NoID_PKClusteredNotOrderedByTime (someSTR, someDate) values ('bbb', GETDATE())
insert into NoID_PKClusteredNotOrderedByTime (someSTR, someDate) values ('ccc', GETDATE())
insert into NoID_PKClusteredNotOrderedByTime (someSTR, someDate) values ('zzz', GETDATE())
insert into NoID_PKClusteredNotOrderedByTime (someSTR, someDate) values ('ddd', GETDATE())
insert into NoID_PKClusteredNotOrderedByTime (someSTR, someDate) values ('eee', '2017-01-01 00:00:00')
select * from NoID_PKClusteredNotOrderedByTime
alter table dbo.NoID_PKClusteredNotOrderedByTime add ID int identity(1,1) not null
select * from NoID_PKClusteredNotOrderedByTime
create table dbo.NoID_ClusteredNotOrderedByTime
(
someSTR nvarchar(50),
someDate datetime
) ON [PRIMARY];
insert into NoID_ClusteredNotOrderedByTime (someSTR, someDate) values ('aaa', GETDATE())
insert into NoID_ClusteredNotOrderedByTime (someSTR, someDate) values ('bbb', GETDATE())
insert into NoID_ClusteredNotOrderedByTime (someSTR, someDate) values ('ccc', GETDATE())
insert into NoID_ClusteredNotOrderedByTime (someSTR, someDate) values ('zzz', GETDATE())
insert into NoID_ClusteredNotOrderedByTime (someSTR, someDate) values ('ddd', GETDATE())
insert into NoID_ClusteredNotOrderedByTime (someSTR, someDate) values ('eee', '2017-01-01 00:00:00')
select * from NoID_ClusteredNotOrderedByTime
CREATE CLUSTERED INDEX [ix_NoID_ClusteredNotOrderedByTime] ON dbo.NoID_ClusteredNotOrderedByTime
(
[someSTR] asc
) ON [PRIMARY]
GO
select * from NoID_ClusteredNotOrderedByTime
alter table NoID_ClusteredNotOrderedByTime add ID int identity(1,1) not null
select * from NoID_ClusteredNotOrderedByTime
drop INDEX [ix_NoID_ClusteredNotOrderedByTime] ON dbo.NoID_ClusteredNotOrderedByTime
alter table NoID_ClusteredNotOrderedByTime drop column ID
alter table NoID_ClusteredNotOrderedByTime add ID int identity(1,1) not null
select * from NoID_ClusteredNotOrderedByTime
create table dbo.NoID_ClusteredNotOrderedByTimeIdentityBeforeCI
(
someSTR nvarchar(50),
someDate datetime
) ON [PRIMARY];
insert into NoID_ClusteredNotOrderedByTimeIdentityBeforeCI (someSTR, someDate) values ('aaa', GETDATE())
insert into NoID_ClusteredNotOrderedByTimeIdentityBeforeCI (someSTR, someDate) values ('bbb', GETDATE())
insert into NoID_ClusteredNotOrderedByTimeIdentityBeforeCI (someSTR, someDate) values ('ccc', GETDATE())
insert into NoID_ClusteredNotOrderedByTimeIdentityBeforeCI (someSTR, someDate) values ('zzz', GETDATE())
insert into NoID_ClusteredNotOrderedByTimeIdentityBeforeCI (someSTR, someDate) values ('ddd', GETDATE())
insert into NoID_ClusteredNotOrderedByTimeIdentityBeforeCI (someSTR, someDate) values ('eee', '2017-01-01 00:00:00')
select * from NoID_ClusteredNotOrderedByTimeIdentityBeforeCI
alter table NoID_ClusteredNotOrderedByTimeIdentityBeforeCI add ID int identity(1,1) not null
select * from NoID_ClusteredNotOrderedByTimeIdentityBeforeCI
CREATE CLUSTERED INDEX [ix_NoID_ClusteredNotOrderedByTimeIdentityBeforeCI] ON dbo.NoID_ClusteredNotOrderedByTimeIdentityBeforeCI
(
[someSTR] asc
) ON [PRIMARY]
GO
select * from NoID_ClusteredNotOrderedByTimeIdentityBeforeCI
/*
drop table dbo.NoID
drop table dbo.NoID_ClusteredNotOrderedByTime
drop table dbo.NoID_PKClusteredNotOrderedByTime
drop table NoID_ClusteredNotOrderedByTimeIdentityBeforeCI
*/