简体   繁体   English

Sql执行速度很慢

[英]Sql execution speed very slow

I have for loop and within that loop I have used INSERT INTO command for 75000+ values. 我有循环,在该循环内我使用INSERT INTO命令获得75000+值。 when i am running it consumes more time. 当我运行它消耗更多的时间。 how can I improve insertion speed,... 如何提高插入速度,......

thanks in advance... rgs tharindu 提前谢谢... rgs tharindu

If you are using SQL Server (was not specified): Rather than many individual INSERT calls, use a Bulk method such as 如果您使用的是SQL Server(未指定):使用Bulk方法,而不是许多单独的INSERT调用

If you have a loop with 75k inserts, you're doing it wrong 如果你有一个75k插入的循环,你做错了

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. 有更好的方法来生成行,但这将比循环75k次更好。

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 编辑:同样的想法适用于大多数RDBMS,除了没有窗口函数的MySQL ......在这种情况下,我有一个填充1-100000的Numbers表用于示例

One solution could be to write the data to insert to file, then use LOAD DATA INFILE in mysql to load it in batch. 一种解决方案是将数据写入插入文件,然后在mysql中使用LOAD DATA INFILE批量加载。 It should speed things up considerably compared to having 75000 separate inserts. 与拥有75000个独立插件相比,它应该可以大大加快速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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