Why shouldn't we use Select * in a mysql query on a production server?

mysql, pdo, select

Solution

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.

- With `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.

- Most people that write `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.

- Explicitly naming the columns also guarantees they are always in the same order, while `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.

Problem

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?

Original source