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_ ClusteredNotOrderedByTimeIdent ityBeforeCI
(
someSTR nvarchar(50),
someDate datetime
) ON [PRIMARY];
insert into NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI (someSTR, someDate) values ('aaa', GETDATE())
insert into NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI (someSTR, someDate) values ('bbb', GETDATE())
insert into NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI (someSTR, someDate) values ('ccc', GETDATE())
insert into NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI (someSTR, someDate) values ('zzz', GETDATE())
insert into NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI (someSTR, someDate) values ('ddd', GETDATE())
insert into NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI (someSTR, someDate) values ('eee', '2017-01-01 00:00:00')
select * from NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI
alter table NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI add ID int identity(1,1) not null
select * from NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI
CREATE CLUSTERED INDEX [ix_NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI] ON dbo.NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI
(
[someSTR] asc
) ON [PRIMARY]
GO
select * from NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI
/*
drop table dbo.NoID
drop table dbo.NoID_ ClusteredNotOrderedByTime
drop table dbo.NoID_ PKClusteredNotOrderedByTime
drop table NoID_ ClusteredNotOrderedByTimeIdent ityBeforeCI
*/