DEV Community

Discussion on: You Can Do it in SQL, Stop Writing Extra Code for it

Collapse
 
rhymes profile image
rhymes

Well said :-)

Window functions are my favorite "hidden" super power. The intro of the linked article says it all:

There was SQL before window functions and SQL after window functions: that’s how powerful this tool is.

Collapse
 
geshan profile image
Geshan Manandhar

Not sure of this.

Collapse
 
rhymes profile image
rhymes

Ah ah I think it's just a hyperbole, but they really are useful

Thread Thread
 
geshan profile image
Geshan Manandhar

I hope they are! :)

Thread Thread
 
rhymes profile image
rhymes

They are, you can do a long way to calculate base statistics or setup things like leaderboards where you give rows scores based on some columns

Collapse
 
qm3ster profile image
Mihail Malo

Oh my lord, pagination.
I am so profoundly shocked by the concept of pagination I'm litterally SHYDDING MAISELF right now.

Collapse
 
rhymes profile image
rhymes

Not exactly Mihail, pagination is what you do with LIMIT and OFFSET, and has been there since forever.

Window functions are useful to correlate different rows and building charts or leaderboard, giving them weights, using them as a state machine (since it's a rolling window you can know which row is "previous") and other stuff.

You can obviously do that all of that stuff using a programming language (and sometimes you should) but if you don't need to, why not use them ;) ?

Thread Thread
 
qm3ster profile image
Mihail Malo • Edited

No, I get it, I see how it is both useful and how it might be obscure even to those that write some SQL.
I was just making fun of the hyped up description.

I'm also in the camp that says SQL is over used. The two popular and malignant ways it's overused is when one DB is shared between many if not all microservices (regardless of how well written the stored procedures are), and ORMs when used mixed with procedural app code and not as query builders.

SQL can be an acceptable structured datastore (with logic) for a given service, alone or together with other sources of truth. But not every service would benefit from one of those.

But most importantly, it's a poor object store and an even worse inter-service communication channel. And I've seen it used as both a lot.

Thread Thread
 
rhymes profile image
rhymes • Edited

No, I get it, I see how it is both useful and how it might be obscure even to those that write some SQL.

It took me a while to get it but I was actually writing a basic leaderboard and all of the "in language" solutions I tried were dog slow. Window functions instead use indexes :)

I'm also in the camp that says SQL is over used.

I don't know, it really depends on too many factors.

The two popular and malignant ways it's overused is when one DB is shared between many if not all microservices (regardless of how well written the stored procedures are),

Eh eh yeah, say no to stored procedures 99.99% of the time. The only placed I worked at where we had those was at a financial company and they used procedures written in Python that did math calculations inside the DB. A mess to maintain but insanely fast.

Say no to microservices that synchronize through a shared DB. That's a distributed monolith :D

Thread Thread
 
qm3ster profile image
Mihail Malo

I meant stored procedures in SQL itself. CREATE PROCEDURE.

Thread Thread
 
rhymes profile image
rhymes

Yeah, let's avoid stored procedures if we can. Agreed