Sql execution speed very slow

懵懂的女人 提交于 2019-12-31 07:06:49

问题


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

thanks in advance... rgs tharindu


回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/5535304/sql-execution-speed-very-slow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!