我尝试使用序列并使用我在下面发布的查询(使用select max)在测试表中插入100万行。令人惊讶的是,使用select max的查询在11:11(11分11秒)内创建了100万行
序列查询在19:34(19分11秒)内创建了100万行。现在我不确定哪个好,
表: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]
这是查询
查询以创建序列
create Sequence PageTableSequence
START WITH 1
INCREMENT BY 1
NO CYCLE
NO CACHE
;
使用Select max(id)
查询创建100万条记录DECLARE @intFlag INT
SET @intFlag = 0
Declare @maxrecords bigint
set @maxrecords = 0
while(@maxrecords<1000000)
BEGIN
WHILE (@intFlag =0)
BEGIN
BEGIN TRY
Insert into SitePage (PageID, PageName) values ((Select max(PageID)+1 from 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
使用序列查询以插入100万条记录
Declare @maxrecords bigint
set @maxrecords = 0
while(@maxrecords<1000000)
BEGIN
Insert into SitePage (PageID, PageName) values (next value for PageTableSequence, 'some page name');
set @maxrecords = @maxrecords+1
END
GO
为什么序列中存在这样的差异并选择max
答案 0 :(得分:1)
尽量不要使用&#34; NO CACHE&#34;,而是使用&#34; CACHE 100&#34;,甚至1000。这应该可以减少IO并提高性能。见http://msdn.microsoft.com/en-us/library/ff878091.aspx