News for hackers. Stuff that matters.
Re:How I Reduced My DB Server Load by 80% by jandrese on Monday October 09, 2017 @03:16PM (#15436599)
Sometimes I see monster queries like that and think that they could have saved themselves several lines of code if they were willing to export the data and run some of the logic in the native language.

Sometimes it seems like people get hung up on doing everything in one query and end up making a Frankenstein's monster that nobody will want to touch when it inevitably breaks on the next database upgrade. Although sometimes the system forces this behavior by ingesting the output of the query automatically.

Re:How I Reduced My DB Server Load by 80% by Amezarak on Monday October 09, 2017 @05:16PM (#15437536)
Well, it depends.

At least in my experience, assuming those queries are written "correctly" (that is, in a set-manner than a C-in-SQL manner), leaving it in the database means is usually vastly more performant and easier to verify it's correct. I've written a lot of many-hundred-line monsters with joins, CTEs, table-valued functions, and a whole lot of other stuff piled into them that were both more clear and much faster than the arcane application logic they once consisted of. Usually if it's used in the application, some management type will also want some kind of report based on pretty much the same query, too. And having it as SQL also makes it much quicker to compare the before-and-after if any changes are made.

Of course, YMMV and what I said isn't true all the time, and would certainly also depend on your database server - if queries did break between upgrades I'd be much more hesitant to rely on the db server.

But either way, I wouldn't dump a monster like that right in source code - that's crazy!

Re:How I Reduced My DB Server Load by 80% by cagmz on Tuesday October 10, 2017 @12:31AM (#15439375)
What do you mean by "set-manner than a C-in-SQL manner"?
Re:How I Reduced My DB Server Load by 80% by jessaustin on Tuesday October 10, 2017 @02:02AM (#15439712)
(Not OP, but I feel like I've seen this phenomenon before.) In C, the natural idiom would be to read in a giant file line by line, doing something with each line and eventually doing something else based on some summary of everything read so far. Lots of dumb SQL code also does something like this, e.g. by generating some ginormous join and then whittling it down to something usable. Better SQL code filters before the join, and often uses indexes to do so.
Re:How I Reduced My DB Server Load by 80% by Amezarak on Tuesday October 10, 2017 @08:43PM (#15446463)
There are people in this world who do things like use cursors in place of joins, for example.
Re:How I Reduced My DB Server Load by 80% by takeda on Tuesday October 10, 2017 @02:16AM (#15439762)
The latency of the network is comparatively very high.

This is why the N+1 is such a big deal and you can get results magnitude faster of you query through the database.