Re:Why SELECT * is bad for SQL performance (2020)
by sk5t on Monday February 15, 2021 @11:13AM
(#26144044)
> “Select ” will almost certainly be optimized away by the execution plan
What do you mean? Ask for all the columns, you're gonna get all the columns in a resultset. Only in a subquery could the planner know what is unused.
'select ' is _perhaps_ tolerable for creating a view--under the conditions that the database transforms the query into the unambiguous named columns at view creation time--and that's about it.
Re:Why SELECT * is bad for SQL performance (2020)
by pfarrell on Monday February 15, 2021 @11:23AM
(#26144154)
You’re right. When I wrote that, I was thinking higher up the chain in an orm layer. Sending “select *” to the db can’t be “optimized away” at that layer, it will have to return all the columns. Unless, like you said, the scope of the “select *” contains it in a sub query where the columns used can be known... “it depends :)”
Re:Why SELECT * is bad for SQL performance (2020)
by skeeter2020 on Monday February 15, 2021 @12:02PM
(#26144535)
>> binding issues, in code, when you modify or add columns.
This is very common IME, and it is a very bad code smell. It tends to break things very low in the architecture, so even capturing the error makes the application essentially not work in a fundamental way.
Re:Why SELECT * is bad for SQL performance (2020)
by bitexploder on Monday February 15, 2021 @11:15AM
(#26144065)
And, not to be a curmudgeon, but, most apps don't really need that level of optimization. In 90% of apps it's fine. The 10% that really need the performance are hopefully doing it right with well tuned indices, careful denormalization, and other things. It is unimaginable to me, that someone with a higher performance SQL/RDBMS backed app can't run a query planner and tame ORMs for performance and identify things like wildcard column select. But what do I know :)
Re:Why SELECT * is bad for SQL performance (2020)
by pfarrell on Monday February 15, 2021 @11:19AM
(#26144108)
Very true. Also, along those lines, if you have fewer than 10k rows, it’s likely the dB will keep the whole table in memory after the first access, anyway.
Re:Why SELECT * is bad for SQL performance (2020)
by jimsmart on Monday February 15, 2021 @11:45AM
(#26144349)
In an ideal situation, agreed. But surely that depends entirely on how many tables are being queried (how many tables already exist in the cache), the size of the rows, how much RAM the host has / how big the cache is, plus a few other variables most probably.
What I mean is: like all other things, it depends. And with assumptions like this, it is generally better to measure.
Re:Why SELECT * is bad for SQL performance (2020)
by skeeter2020 on Monday February 15, 2021 @11:59AM
(#26144504)
he's not really talking about the performance of the DB though, focusing more on size of the resultant payload thatr gets sent over the wire. In the actual database which is traditionally very challenging to scale, projections aren't very expensive. The other things you and GP mention are way more likkely to move the performance needle.
For instance. “Select *” can be good for ad-hoc exploration (as long as you include a limit to the rows returned). It’s bad practice for shipped code because it can lead to binding issues, in code, when you modify or add columns.
“Select *” will almost certainly be optimized away by the execution plan but if you have blob data, could introduce a lot of unnecessary disk i/o.
Is “select *” bad? It depends.