SQL data Insert, delete big Data

Source: Internet
Author: User
Tags bulk insert getdate rowcount

--Test table CREATE TABLE [dbo]. [Employee] (    [employeeno]INTPRIMARYKEY ,     [employeename] [nvarchar] (NULL,    [CreateUser] [  nvarchar](NULL,    [createdatetime ][datetime]NULL);
--1,Loop InsertSET STATISTICSTime on;DECLARE @Index INT = 1;DECLARE @Timer DATETIME = GETDATE(); while @Index <= 100000BEGIN    INSERT [dbo].[Employee](Employeeno, EmployeeName, CreateUser, Createdatetime)VALUES(@Index,'Employee_' + CAST(@Index  as CHAR(6)),'system',GETDATE()); SET @Index = @Index + 1;ENDSELECT DATEDIFF(MS,@Timer,GETDATE()) as [execution Time (milliseconds)];SET STATISTICSTimeOFF;
--2,Transaction LoopsBEGIN TRAN;SET STATISTICSTime on;DECLARE @Index INT = 1;DECLARE @Timer DATETIME = GETDATE(); while @Index <= 100000BEGIN    INSERT [dbo].[Employee](Employeeno, EmployeeName, CreateUser, Createdatetime)VALUES(@Index,'Employee_' + CAST(@Index  as CHAR(6)),'system',GETDATE()); SET @Index = @Index + 1;ENDSELECT DATEDIFF(MS,@Timer,GETDATE()) as [execution Time (milliseconds)];SET STATISTICSTimeOFF;COMMIT;
--3, BULK Insert
SET STATISTICSTime on;DECLARE @Timer DATETIME = GETDATE();INSERT [dbo].[Employee](Employeeno, EmployeeName, CreateUser, Createdatetime)SELECT TOP(100000) Employeeno=Row_number () Over(ORDER  byC1.[object_id]),'Employee_','system',GETDATE() fromSYS. COLUMNS asC1 Cross JOINSYS. COLUMNS asC2ORDER  byC1.[object_id]SELECT DATEDIFF(MS,@Timer,GETDATE()) as [execution Time (milliseconds)];SET STATISTICSTimeOFF;
--4, CET insertion

SET STATISTICSTime on;DECLARE @Timer DATETIME = GETDATE();; withCTE (Employeeno, EmployeeName, CreateUser, Createdatetime) as(    SELECT TOP(100000) Employeeno=Row_number () Over(ORDER  byC1.[object_id]),'Employee_','system',GETDATE()     fromSYS. COLUMNS asC1 Cross JOINSYS. COLUMNS asC2ORDER  byC1.[object_id])INSERT [dbo].[Employee] SELECTEmployeeno, EmployeeName, CreateUser, Createdatetime fromCTE;SELECT DATEDIFF(MS,@Timer,GETDATE()) as [execution Time (milliseconds)];SET STATISTICSTimeOFF;
--5, loop Delete
SET STATISTICS  on ; DECLARE @Timer DATETIME = GETDATE (); DELETE  from [dbo]. [Employee] ; SELECT DATEDIFF @Timer GETDATE  as [ Execution Time (milliseconds)] ; SET STATISTICS OFF;
--6, Bulk Delete
SET STATISTICSTime on;DECLARE @Timer DATETIME = GETDATE();SET ROWCOUNT 100000; while 1 = 1BEGIN    BEGIN TRAN    DELETE  from [dbo].[Employee]; COMMIT    IF @ @ROWCOUNT = 0         Break;ENDSET ROWCOUNT 0;SELECT DATEDIFF(MS,@Timer,GETDATE()) as [execution Time (milliseconds)];SET STATISTICSTimeOFF;
--6, truncate delete
  SET   STATISTICS  time  on  ;   DECLARE   @Timer   DATETIME   =   GETDATE   ();   TRUNCATE   TABLE   [  dbo  ] .  [  Employee  ]  ;   SELECT   DATEDIFF  (MS,  @Timer ,  GETDATE  ())  as   [  Execution Time (milliseconds)  ]  ;   SET   STATISTICS  Time  OFF ; 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.