News for hackers. Stuff that matters.
Re:Why SELECT * is bad for SQL performance (2020) by whack on Monday February 15, 2021 @08:42AM (#26142644)
#1 comment:

> Instead we get a wildcards are bad argument; especially when that wildcard fetches data that you don't need etc; which I guess everyone agrees with.

#2 comment:

> Nevertheless in reality most of the stated reasons have almost no real practicability... So in most cases fetching to many columns would be no problem

This happens surprisingly frequently in a lot of tech discussions. The most popular comment accuses the author of saying something so obviously correct, that it's a waste of time to even bring it up. And the second most popular comment is someone telling the author that he's wrong.

Re:Why SELECT * is bad for SQL performance (2020) by Semaphor on Monday February 15, 2021 @09:05AM (#26142851)
Both comments can work together. Yes, doing more work than necessary is bad (#1). The amount of "bad" is very low to barely measurable in most real-life scenarios (#2).
Re:Why SELECT * is bad for SQL performance (2020) by vp8989 on Monday February 15, 2021 @11:04AM (#26143959)
"The amount of "bad" is very low to barely measurable in most real-life scenarios (#2)."

When you constantly apply this "only things bigger than X matter" reasoning you still end up with slow, shitty software but you just died by a thousand cuts instead of 1.

Re:Why SELECT * is bad for SQL performance (2020) by VBprogrammer on Monday February 15, 2021 @12:06PM (#26144584)
And the rub is if you fail to apply it liberally enough you end up with no completed software or difficult to maintain software because someone went around applying every micro-optimisation in the book regardless of how nessasary or appropriate it might be.
Re:Why SELECT * is bad for SQL performance (2020) by vp8989 on Monday February 15, 2021 @02:14PM (#26146047)
"someone went around applying every micro-optimisation in the book regardless of how nessasary or appropriate it might be"

This feels like a strawman. I have yet to see an instance of this in my 10 years. The much, much, much more common case is people justifying half-assed shoddy work with YAGNI.

Re:Why SELECT * is bad for SQL performance (2020) by randallsquared on Monday February 15, 2021 @02:58PM (#26146565)
Survivorship bias suggests we're more likely to see projects that resulted in released software...
Re:Why SELECT * is bad for SQL performance (2020) by sombremesa on Monday February 15, 2021 @02:44PM (#26146392)
See: solo indie gamedevs that build their own engines for years and subsequently fail to release anything.
Re:Why SELECT * is bad for SQL performance (2020) by autra on Monday February 15, 2021 @04:24PM (#26147637)
Fortunately, stars in sql are also really bad for maintenance ;-) So I guess there is indeed no reason to use them!
Re:Why SELECT * is bad for SQL performance (2020) by BurningFrog on Monday February 15, 2021 @02:37PM (#26146311)
I work that way, and I don't end up slow, shitty software.

I do end up shipping working software much sooner than if I optimized every statement everywhere.

Re:Why SELECT * is bad for SQL performance (2020) by pfarrell on Monday February 15, 2021 @10:43AM (#26143746)
The correct answer to almost every db question is: “it depends”.

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.

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.
Re:Why SELECT * is bad for SQL performance (2020) by lolinder on Monday February 15, 2021 @10:13AM (#26143426)
Building software is making a series of trade-offs, but most tech articles present themselves as though they have the answer. It's not too surprising, then, that the top comments take opposing stances on an issue that is definitely more complicated than the article made it sound.
Re:Why SELECT * is bad for SQL performance (2020) by skeeter2020 on Monday February 15, 2021 @11:56AM (#26144473)
We have trade-offs but they are not all equal. It is both legitimate and encouraged to argue about which are the correct trade-offs IMO.
Re:Why SELECT * is bad for SQL performance (2020) by lolinder on Monday February 15, 2021 @05:55PM (#26148707)
Agreed! The fact that the top two comments usually disagree is one of the primary reasons I read comments on HN. The dialectic allows me to make an informed decision later.
Re:Why SELECT * is bad for SQL performance (2020) by bjarneh on Monday February 15, 2021 @10:38AM (#26143694)
> accuses the author of saying something so obviously correct

Perhaps I do; but my main "accusation" here is that the title is pure click bait, if it was replaced by this:

     SQL - Don't fetch data you don't need!
Would anyone here bothered reading it?
Re:Why SELECT * is bad for SQL performance (2020) by CobrastanJorji on Monday February 15, 2021 @01:53PM (#26145827)
This reminds me of a chilling short story I read a while back, but I cannot remember its name. It was about a machine learning algorithm trained on social network archives to take as input a body of dialogue between people and communities and to output statements which would strongly divide them. As a joke, the developers ran the algorithm on their internal corporate email and read the output sentences, laughing that anybody at their company could possibly come down on the other side of any of the resulting statements before realizing that they were talking about different sides. No longer being able to stand each other, the company dissolves. It was a good story if anyone recalls its name.
Re:Why SELECT * is bad for SQL performance (2020) by function_seven on Monday February 15, 2021 @01:59PM (#26145896)
I just read it again recently. Shiri’s Scissor.

https://slatestarcodex.com/2018/10/30/sort-by-controversial/

Re:Why SELECT * is bad for SQL performance (2020) by erulabs on Monday February 15, 2021 @02:35PM (#26146287)
That was a hell of a read. Thanks for the link!
Re:Why SELECT * is bad for SQL performance (2020) by CobrastanJorji on Monday February 15, 2021 @02:08PM (#26145986)
That's it! Thanks so much.
Re:Why SELECT * is bad for SQL performance (2020) by AmericanChopper on Monday February 15, 2021 @01:30PM (#26145561)
Using select * can cause some correctness issues, but from a performance perspective, it’s basically an archetypal topic for bike shedding. In the sense that using the smallest projection you actually need is an optimization, but for most use cases it will end up being an irrelevant optimization. So select * is bad for performance, but you don’t need to care about it unless you’re experiencing one of the problems it case cause, which most people probably don’t. But you can have a back and forth discussions about that forever.
Re:Why SELECT * is bad for SQL performance (2020) by DiggyJohnson on Monday February 15, 2021 @11:54AM (#26144445)
Well, the good news is that this comment cracked me up, thus demonstrating that this thread does provide some value to HN.

Cheers

Re:Why SELECT * is bad for SQL performance (2020) by rodgerd on Monday February 15, 2021 @02:42PM (#26146367)
This is because "strong opinions, lightly held" creates a culture where people with little to no insight or experience feel empowered to create an unceasing high-volume firehose of worthless opinions that drown out anything useful.
Re:Why SELECT * is bad for SQL performance (2020) by RobRivera on Monday February 15, 2021 @12:23PM (#26144770)
it does not invalidate his/her observation