Comparison with insertion into a query is better

Which of the following queries has the best performance? What is the upside / downside for both queries? I want to discuss a script for inserting 1000 lines and 100 thousand lines. Thank.

the first

INSERT INTO table_name (col1,col2) VALUES (value1, value2);
INSERT INTO table_name (col1,col2) VALUES (value3, value4);

second

INSERT INTO table_name
SELECT (col1,col2) 
FROM (
    SELECT value1 as col1, value2 as col2 UNION ALL
    SELECT value3 as col1, value4 as col2
) A
+3
source share
2 answers

The second query will work better, because the database engine must analyze and execute only one statement. But the difference will be insignificant and will only matter for large inserts (more than 100). But the best approach is

INSERT INTO table_name (col1,col2) VALUES (value1, value2),(value3, value4); 

2 INSERT INTO _ SELECT col1, col2 (   SELECT 1 col1, 2 col2 UNION ALL    3 col1, 4 col4 ) A

+2

Source: https://habr.com/ru/post/1749099/


All Articles