1、循环语句的插入效率问题
为循环插入语句的外围增加一个显示事务,会大幅度的提高循环插入的效率
eg1:
DECLARE @bdate DATETIME
DECLARE @edate DATETIME
SET NOCOUNT ON
DECLARE @i INT=1
DECLARE @q INT=500000
SET @bdate=GETDATE()
CREATE TABLE TEST_20190218
(
ID INT PRIMARY KEY,
NAME UNIQUEIDENTIFIER,
CREATEDATE DATETIME
)
WHILE @i<=@q
BEGIN
INSERT TEST_20190218
SELECT @i,NEWID(),GETDATE()
SET @i=@i+1
END
SET @edate =GETDATE()
SELECT '花费时间:'+CONVERT(NVARCHAR(20),DATEDIFF(MILLISECOND,@bdate,@edate))+'ms'
与
eg2:
DECLARE @bdate DATETIME
DECLARE @edate DATETIME