Skip to content Skip to sidebar Skip to footer

Sql Execution Speed Very Slow

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 adv

Solution 1:

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


Solution 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


Solution 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.


Post a Comment for "Sql Execution Speed Very Slow"