Question

SQL Server (2005/2008)

Each of the below statements have the same result. Does anyone know if one outperforms the other?

insert into SOMETABLE 
  values ('FieldOneValue','FieldTwoValue',3,4.55,'10/10/2008 16:42:00.000')

insert into SOMETABLE 
  select 'FieldOneValue','FieldTwoValue',3,4.55,'10/10/2008 16:42:00.000'

insert into SOMETALBE
  Select
    Field1 = 'FieldOneValue',
    Field2 = 'FieldTwoValue',
    Field3 = 3,
    Field4 = 4.55,
    Field5 = '10/10/2008 16:42:00.000'

Assuming of course that the data types match the table appropriately...

Was it helpful?

Solution

I just tested this.

5 million iterations of both approaches on two sets of hardware, one a server with 16GB RAM, one a notebook with 1GB.

Result: They appear to be the same.

The query plans for these are the same, and the performance differential is statistically insignificant.

OTHER TIPS

i think, based on this question, you are to the point of premature optimization. i'd stick to the standard insert () values () if you are just inserting 1 record and let the Sql Server team make it the most performant.

I would suspect that if there were a performance difference, it would be in favour of the former, although I doubt there is one.

Nevertheless, this is one of those cases where opting for the clearer version (i.e. with VALUES) provides a readability and maintainability benefit which outweighs the likely negligible performance impact. If you're specifying all the values, then stick to the usual convention, in case someone else reads the code, which at first glance might seem to be doing an INSERT...SELECT from another table, which is a misleading appearance.

For SQL Server, just use this pattern

INSERT INTO TableName (fieldList) SELECT (valueList/columnList) [FROM and so on]

This is the only insert pattern you'll ever need. It does everything. Do specify the fieldlist to protect your statement from future table changes (where optional columns are added).

There are some minor nuances to using INSERT INTO VALUES, which I don't remember because I don't have to.

It looks like you're just mimicking

INSERT into SOMETABLE
(
SELECT * FROM SOMEOTHERTABLE
)

ignore this comment, its wrong. sorry about that :(

I know you can't use INSERT VALUES() when you're entering more than one row.

INSERT INTO Table SELECT 1, 2, 3, (SELECT 4 FROM Table2 WHERE columnA = columnB)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top