This question has already been answered:

There is a problem when you click on the button to make 5000-6000 entries in the table. I can do it in a cycle, but I worry that I will load the server with such clicks. I read that putting requests into a loop is not correct. Please help with some elegant solution.

Reported as a duplicate by members aleksandr barakin , MihailPw , Kromster , Qwertiy , Sasha Omelchenko Jun 8 '17 at 10:44 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Use prepared statements and transactions. - user239133
  • options: use procedures or background tasks - Mikhail Vaysman
  • So why so many records? - Alex78191
  • The challenge to quickly fill the database - BigBuch
  • one
    What database are we talking about? This volume can be inserted in seconds if you use COPY in postgres or LOAD DATA LOCAL INFILE in mysql - Ordman

2 answers 2

You can insert several lines inside one INSERT and send one request instead of several:

 INSERT INTO table (column1, column2) VALUES (123, "a"), (456, "b") 

How specifically to do this effectively and safely (to avoid SQL injection) depends on the programming language used.

    You can do this in transactions in databases, for example:

     BEGIN INSERT <...>; INSERT <...>; END; 
    • Thank you, but the server will not hang on the number? - BigBuch
    • By and large, 5K inserts are not so many, it will be completed in about 1-2 seconds, it all depends on the size of the table, because with INSERT , the key and field validation must be verified. In order for the database not to check the key and not carry out validation, it is necessary to read the documentation on your particular DBMS - Chloroform
    • @Chloroform Adding 1000 entries over 1000 insert took me 47 seconds - Alex78191
    • @ Alex78191, what database are you using? Such a long insert may be due to a large table or - Chloroform
    • @Chloroform MySQL - Alex78191