简体   繁体   中英

Poor performance with sql server on azure vm when I write a lot of data to disk

I need to move an existing application to windows azure VMs. This application write a lot of data (insert into) to DB.

So, I created a SQL Server machine on Azure VM, and configured the environment according the Microsoft best practice for SQL Server on azure VM ( http://msdn.microsoft.com/en-us/library/azure/dn133149.aspx ).

I tested this SQL query on my computer and on my SQL Server on azure VM:

WHILE @i <= 100000 BEGIN
INSERT INTO sales (id, created) VALUES (@i, GETDATE());
SET @i = @i + 1;
END

When I run this query on my machine, its finished after 43 seconds. When I run this query on the azure VM its take over 10 minutes!!! (I try this with single disk and striped with 2 and 4 disks).

I try to test more scenario code, with transaction:

WHILE @i <= 100000 BEGIN

-- Start a transaction
IF @i % 10000 = 1 
   BEGIN TRANSACTION;

 INSERT INTO sales (id) VALUES (@i);
 SET @i = @i + 1;

-- Commit after each 10,000 row
IF @i % 10000 = 0
  COMMIT;
END

this scenario works OK on both environments (my pc & azure VM).

When I ran SQLIO on the sql azure VM server, I got results that fit to the second query results (with transaction).

My application must run without transactions.

Anybody know how to resolve that? Thank you.

100.000 inserts one by one will be slow, because of the many inserts within the same transaction. Try this instead. It only has 1 insert:

;WITH d as
(
  SELECT rn
  FROM (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(0)) x(rn)
), numbers as
(
  SELECT 
    row_number() over (order by (select 1)) rn
  FROM d d1,d d2,d d3,d d4,d d5
)
INSERT sales (id, created)
SELECT 
  rn,
  getdate()
FROM
  numbers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM