sql - inserting 1 million records with sequence and select max -
i tried insert 1 million rows in test table using sequence , using query (using select max) posted below. surprisingly, query with select max created 1 million rows in 11:11 (11 mins 11 secs)
and query sequence create 1 million rows in 19:34 (19 mins 11 secs). not sure good
table : sitepage
create table [dbo].[sitepage]( [pageid] [bigint] not null, [pagename] [nchar](50) not null, constraint [pk_sitepage] primary key clustered ( [pageid] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary]
here queries
query create sequence
create sequence pagetablesequence start 1 increment 1 no cycle no cache ;
query create 1 million records using select max(id)
declare @intflag int set @intflag = 0 declare @maxrecords bigint set @maxrecords = 0 while(@maxrecords<1000000) begin while (@intflag =0) begin begin try insert sitepage (pageid, pagename) values ((select max(pageid)+1 sitepage),'some page name'); set @intflag = @@rowcount end try begin catch set @intflag=0 end catch end set @maxrecords = @maxrecords+1 set @intflag=0 end go
query using sequence insert 1 million records
declare @maxrecords bigint set @maxrecords = 0 while(@maxrecords<1000000) begin insert sitepage (pageid, pagename) values (next value pagetablesequence, 'some page name'); set @maxrecords = @maxrecords+1 end go
why there such difference in sequence , select max
try not using "no cache", use "cache 100", or 1000 instead. should give reduce io , improve performance. see http://msdn.microsoft.com/en-us/library/ff878091.aspx
Comments
Post a Comment