principlesqlModerate
Recommended way: "SELECT *" vs "SELECT COLUMN_LIST" from same table, multiple times
Viewed 0 times
fromsamerecommendedwaytimesmultipleselectcolumn_listtable
Problem
This question is very much near to what I wanted to ask, but both question and answers are focusing more on selecting random rows.
Since there is general rule that, "SELECT COLUMN_LIST" is always recommended over "SELECT *", I wanted to know whether the recommendation changes with the below scenario.
Scenario:
From a table having about 5 columns, if I need information of those 5 columns, but at different steps,
Eg: Like in a Java function, at Step 1, first 2 columns will be used and at Step 4, column 3 and column 4 will be used and at Step 10, 5th column will be used.
There are 2 ways to get these information,
Which of the 2 ways is recommended for the above scenario?
Since there is general rule that, "SELECT COLUMN_LIST" is always recommended over "SELECT *", I wanted to know whether the recommendation changes with the below scenario.
Scenario:
From a table having about 5 columns, if I need information of those 5 columns, but at different steps,
Eg: Like in a Java function, at Step 1, first 2 columns will be used and at Step 4, column 3 and column 4 will be used and at Step 10, 5th column will be used.
There are 2 ways to get these information,
- Make 1 DB call, with "SELECT *" and extract required information at respective steps.
- Make multiple DB calls, with "SELECT COLUMN_LIST" at Step 1, Step4 and Step 10, fetching only required column's data, in each call.
Which of the 2 ways is recommended for the above scenario?
Solution
There's a third option you forgot to mention, make one DB call, with
The reason
And even if no future schema changes ever break the application, you still can run into performance issues as well with using
On top of all of that, as mentioned earlier in the comments, it's also cleaner code to specify the column list as opposed to using
There are a multitude of other reasons as well, but those are the few important ones that come to mind. Conversely, there are a few edge cases where it is OK to use
SELECT COLUMN_LIST. The reason I mention this is because the comparison of when to use SELECT * vs SELECT COLUMN_LIST isn't really about how many database calls one makes, rather it's about schema consistency.The reason
SELECT * is recommended against is because the schema of the dataset you're selecting from is liable to change over time, which could result in unexpected outcomes and errors, especially on your application as the consumer which is always expecting Column1 to be Column1, and Column3 to be the third column, etc.And even if no future schema changes ever break the application, you still can run into performance issues as well with using
SELECT *, for two reasons. One being that there may have been additional columns added to the end of the dataset which you're now needlessly bringing back extra data for. The second reason is you'll potentially cause a less than optimal query plan to be generated (e.g. an index that normally could be seeked on may not be applicable now).On top of all of that, as mentioned earlier in the comments, it's also cleaner code to specify the column list as opposed to using
SELECT *. This is because it explicitly communicates which fields from the database are being consumed and helps establish the intent of the code, especially for developers who may not have access to the database itself.There are a multitude of other reasons as well, but those are the few important ones that come to mind. Conversely, there are a few edge cases where it is OK to use
SELECT * as opposed to an explicit column list, and you can find some of those examples in a similar question I asked here.Context
StackExchange Database Administrators Q#295423, answer score: 19
Revisions (0)
No revisions yet.