sábado, 22 de octubre de 2011

What is wrong with 'SELECT *'?

SELECT * is inefficient, particularly when you are only using a few of the columns in the table. This is because it actually makes TWO queries to the database: before it runs your query, it has to query the system tables to determine the name and datatypes of the columns. It is much more efficient to NAME your columns in the SQL query, and this will also help in having your column names right there... so you don't have to keep flipping back and forth between ASP page and database. In addition, this will prevent ambiguous column names in your resultset (in the case where both or all tables in the JOIN statement have columns with the same name). Further still, your code can break if it relies on ordinal position, and then someone inserts a column at the top of the table (which you can do in Enterprise Manager, or by dropping / re-creating the table). This is also a strong case for always defining columns in the INSERT list.

And finally, here's another reason to avoid SELECT * : Memo/Text columns, as well as columns containing BLOB data. Microsoft recommends to put BLOB/text columns at the end of the SELECT statement, and if there is more than one, in order of appearance in the table. This is also applicable to VARCHAR columns in SQL Server with a length greater than 255 characters.

1 comentario:

  1. Thanks for the best blog.it was very useful for me.keep sharing such ideas in the future as well.this was actually what i was looking for,and i am glad to came here.

    ResponderEliminar