views:

39

answers:

2

Which one of below query has better performance? What's the upside/downside for both query? I want to discuss the scenario for inserting 1000 rows and 100k rows Thanks.

1st

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

2nd

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
+2  A: 

I think you need to use Bulk Insert. Here is example in .net http://www.dotnetcurry.com/ShowArticle.aspx?ID=323&AspxAutoDetectCookieSupport=1

mmcteam.com.ua
+2  A: 

The second query will perform better because the DB engine has to analyze and execute only one statement.But the difference will be insignificant and will matter only for large inserts (more than 100) But the best approach would be

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

but your syntax for query 2 should be INSERT INTO table_name SELECT col1,col2 FROM ( SELECT value1 as col1, value2 as col2 UNION ALL SELECT value3 as col1, value4 as col4 ) A

josephj1989
But when I ran this query in SQL server 2000, it complains stack overflow while compiling. I only tried to insert 300 rows.
Stan
The syntax given by josephj1989 is only valid in SQL Server 2008.
Joe Stefanelli