Based on this question here Selecting NOT NULL columns from a table One of the posters said
you shouldn't use SELECT * in production.
My Question: Is it true that we shouldn't use Select * in a mysql query on a production server? If yes, why shouldn't we use select all?
Most people do advise against using SELECT *
in production, because it tends to break things. There are a few exceptions though.
SELECT *
fetches all columns - while most of the times you don't
need them all. This causes the SQL-server to send more columns than
needed, which is a waste and makes the system slower.SELECT *
, when you later add a column, the old query will also
select this new column, while typically it will not need it. Naming
the columns explicitly prevents this.SELECT *
queries also tend to grab the rows
and use column order to get the columns - which WILL break your code
once columns are injected between existing columns.SELECT *
might behave differently when the table column order is modified.But there are exceptions, for example statements like these:
INSERT INTO table_history
SELECT * FROM table
A query like that takes rows from table, and inserts them into table_history. If you want this query to keep working when new rows are added to table AND to table_history, SELECT *
is the way to go.
Remember that your database server isn't necessarily on the same machine as the program querying the database. The database server could be on a network with limited bandwidth; it could even be halfway across the world.
SELECT * FROM table
.SELECT * FROM table
only to throw half the columns away.Other potential reasons it might be good to specify which exact columns you want:
As @Konerak mentioned, naming the columns you want also ensures that the order of the columns in your result is the same, even if the table schema changes (i.e. inserting one column in-between two others.) This is important if you're depending on FirstName
being the [2]
nd element of a result.
(Note: a more robust and self-documenting way of dealing with this is to ask for your database results as a list of key-value pairs, like a PHP associative array, Perl hash or a Python dict
. That way you never need to use a number to index into the result (name = result[2]
) - instead you can use the column name: name = result["FirstName"]
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With