开发者

Sql execution speed very slow

开发者 https://www.devze.com 2023-02-22 00:57 出处:网络
I have for loop and within that loop I have used INSERT INTO command for 75000+ values. when 开发者_JS百科i am running it consumes more time. how can I improve insertion speed,...

I have for loop and within that loop I have used INSERT INTO command for 75000+ values. when 开发者_JS百科i am running it consumes more time. how can I improve insertion speed,...

thanks in advance... rgs tharindu


If you are using SQL Server (was not specified): Rather than many individual INSERT calls, use a Bulk method such as

  • SQLBulkCopy
  • BULK INSERT
  • bcp Utility


If you have a loop with 75k inserts, you're doing it wrong

Based on your comments, you need something to gererate rows for you.

;WITH cNumbers AS
(
    SELECT ROW_NUMBER() OVER (ORDER BY c.id) - 1 AS rownum
    FROM sys.columns c CROSS JOIN start_number c2 CROSS JOIN start_number c3
)
SELECT
    c.rownum + m.start_number
FROM
    mastertable m
    CROSS JOIN
    cNumbers c
WHERE
    c.rownum <= no_of_items

There are better ways to generate rows but this will be better then looping 75k times.

Edit: the same idea applies to most RDBMS except MySQL which doesn't have Windowing functions... in which case I'd have a Numbers table filled with 1-100000 for examplke


One solution could be to write the data to insert to file, then use LOAD DATA INFILEin mysql to load it in batch. It should speed things up considerably compared to having 75000 separate inserts.

0

精彩评论

暂无评论...
验证码 换一张
取 消