SQL INSERT performance omitting field names?

后端 未结 3 1691
南方客
南方客 2021-02-20 05:13

Does anyone knows if removing the field names from an INSERT query results in some performance improvements?

I mean is this:

INSERT INTO table1 VALUES (v         


        
相关标签:
3条回答
  • 2021-02-20 05:50

    No, it's not faster. The database has to check which fields are in the table and match against the values anyway.

    You should always specify the fields in the query to make the code more robust. If someone changes the order of the fields in the table, it stops working (or worse writes the data in the wrong field) if you haven't specified the fields.

    0 讨论(0)
  • 2021-02-20 06:02

    I don't know of any performance improvement by omitting the field names but I would advise against it for anything other than testing/once-only inserts.

    Readability will suffer (you have to check the table to see what is being inserted where) and your code will break if the table schema is changed

    0 讨论(0)
  • 2021-02-20 06:09

    No, actually the contrary! At least for Microsoft SQL Server - you didn't specify what database you're talking about.....

    If you don't specify the fields, either in a SELECT or an INSERT, then SQL Server's query processor must first go inspect the system catalogs to find out what fields are indeed available.

    So I would always recommend to explicitly list the fields you want - on SELECTs as much as on INSERTs.

    0 讨论(0)
提交回复
热议问题