News for hackers. Stuff that matters.
Re:Databases are the endgame for data-oriented design by fifilura on Wednesday December 06, 2023 @01:04PM (#38547410)
My colleagues hate me, but I also found that SQL is The way to write business logic.

Lots of caveats about difficulty to test and weird syntax.

But it is just that SQL is the most terse and standard way so express logic.

And that in itself is the most important factor to avoid bugs. Not what testing strategy you choose.

Re:Databases are the endgame for data-oriented design by crazygringo on Wednesday December 06, 2023 @01:55PM (#38548016)
> But it is just that SQL is the most terse and standard way so express logic.

As someone who has written a ton of complex SQL... I couldn't disagree more.

Trying to shoehorn things that can trivially and intuitively be expressed in a couple of for-loops with a couple of variables, into SQL expressions making use of joins and aggregate functions and GROUP BY's and (god forbid) correlated subqueries... having to replicate subqueries because their results in one part of the query can't be re-used in another part... teaching people arcane terminology distinctions like between WHERE and HAVING... not having basic functions for calculating percentiles or even a basic median... certain basic kinds of logic operations that simply can't be done... flipping a coin on whether the query engine will use the index and execute in 10 ms, or decide to follow a different execution plan and take 5 minutes to run...

I have never encountered more bugs in business logic than in dealing with SQL. It obfuscates what should be clear. SQL isn't a solution for avoiding bugs, it's what causes so many of them.

Re:Databases are the endgame for data-oriented design by chrsig on Wednesday December 06, 2023 @02:56PM (#38548695)
The types of issues you're enumerating are really part of a learning curve that every language and environment will have.

> not having basic functions for calculating percentiles or even a basic median

I don't think this is really true anymore; windowing functions are pretty prolific, and I think every major database will have some percentile functions

Re:Databases are the endgame for data-oriented design by crazygringo on Wednesday December 06, 2023 @03:17PM (#38548922)
> The types of issues you're enumerating are really part of a learning curve that every language and environment will have.

They're not though. That's why excellent coders often despise having to learn and use SQL, and basically just refuse to. SQL is uniquely terrible -- and I say this as someone with a career spanning from Win32 C++ to every major web technology.

> every major database will have some percentile functions

MySQL still doesn't. MariaDB only added it in 2018, and I've been writing queries for a lot longer than that. (And MySQL only added window functions in 2018 as well.)

And remember that, for various reasons, you're often (usually?) stuck using a database engine in production that's several years old, since upgrading a database engine is not something to be taken lightly.

Re:Databases are the endgame for data-oriented design by paulddraper on Wednesday December 06, 2023 @03:43PM (#38549232)
How many application languages have percentile functions?
Re:Databases are the endgame for data-oriented design by setr on Wednesday December 06, 2023 @07:20PM (#38551294)
Nearly all of them, because any language designed in the last 30 years permits user defined libraries in trivial fashion, even amazingly letting you write those libraries in the same language you’ll use it. And even more incredibly, the libraries themselves can build on other libraries! (There’s even often a standard allowing libraries to be shared by different implementations … or rather, a standard that actually standardizes things beyond the language aesthetic)

Databases follow the COBOL model of extensibility — tis for me and not for thee. It is only to be done by the vendor, and maybe some third party specialists

Re:Databases are the endgame for data-oriented design by paulddraper on Wednesday December 06, 2023 @11:40PM (#38552905)
Every database I've ever used is extensible
Re:Databases are the endgame for data-oriented design by chrsig on Wednesday December 06, 2023 @03:29PM (#38549056)
> They're not though. That's why excellent coders often despise having to learn and use SQL, and basically just refuse to. SQL is uniquely terrible -- and I say this as someone with a career spanning from Win32 C++ to every major web technology.

This is basically a strawman + appeal to authority argument. I've no desire to bicker. "Uniquely terrible" is quite subjective, we'll have to agree to disagree on if it applies to sql.

Re:Databases are the endgame for data-oriented design by crazygringo on Wednesday December 06, 2023 @03:35PM (#38549129)
Excuse me, but I'm certainly not "bickering" and I resent the insinuation. And you don't seem to understand what a strawman argument is.

I'm not going to write an article on this for you, but you can certainly see the sentiment frequently expressed on HN that the rise of NoSQL and of ORM's is a direct result of otherwise capable programmers not wanting to deal with SQL and its conceptual building blocks such as joins. Take that for what you will, but you might suspect there's a reason for it, and that it's not merely the learning curve for "every language".

Re:Databases are the endgame for data-oriented design by SahAssar on Wednesday December 06, 2023 @06:00PM (#38550670)
> you can certainly see the sentiment frequently expressed on HN that the rise of NoSQL and of ORM's is a direct result of otherwise capable programmers not wanting to deal with SQL and its conceptual building blocks such as joins

I think I saw that sentiment a lot 5+ years ago. The last 2-3 years have been a lot more for postgresql and writing SQL directly.

Either way I don't think that "excellent coders" "despise" learning SQL, almost every senior coder that has interacted with a medium-to-large database has understood and valued SQL in my experience.

Re:Databases are the endgame for data-oriented design by sgarland on Wednesday December 06, 2023 @05:48PM (#38550561)
I take it as the same reason they don’t want to use boring technology like HAProxy, or why they think infrastructure in general is beneath them: because they are easily influenced by breathless grifters on Medium promoting the New Thing.

SQL isn’t going away any time soon. At its heart, it’s relational algebra. If you can learn DS&A, you can learn relational algebra.

Re:Databases are the endgame for data-oriented design by rglover on Thursday December 07, 2023 @03:13AM (#38554083)
FWIW, I love HAProxy but hate SQL (for the exact reasons the OC suggests). It turns the simple into soup and makes bugs more likely. Every single non-trivial usage of it I've seen in the wild has been a ball of spaghetti. I bought the learning curve argument for a long time until I finally realized: no, SQL is a victim of its own success. It's popular because it's popular, not because it's a superior language. That popularity has encouraged a gambling addict level of doubling down that continues to gaslight less-experienced developers into believing it's "the way."

You're right that it isn't going away, but when it does, I'll be the first to dance on its grave.

Re:Databases are the endgame for data-oriented design by ak39 on Wednesday December 06, 2023 @11:46PM (#38552935)
ORMs and NoSQL were a direct result of the desire to have an object-oriented data worldview in a programming landscape dominated by OO languages. They were not introduced because programmers didn’t want to learn SQL (implying incompetence etc).
Re:Databases are the endgame for data-oriented design by garfij on Thursday December 07, 2023 @05:36PM (#38562750)
I feel like your perspective might be more narrow than you think it is.

I've been coding professionally for 20 years and at every job I've ever had we've made the decision to move away from ORMs or as much as possible given the constraints of our system in preference of directly writing SQL. My previous employer dumped Rail in favor of a Go backend (a language whose userbase almost entirely eschews the idea of ORMs) and my current employer never even had one to start with, choose to write SQL directly in a typescript backend.

I'm not going to pull the stats for you but this very website is FULL of articles talking about the benefits of writing your own SQL and I can't even remember the last time someone tried to pitch me a shiny new ORM.

> teaching people arcane terminology distinctions like between WHERE and HAVING

This is akin to complaining about needing to teach people arcane terminology distinctions between FOR and WHILE. I think it's a crime that many CS degrees I see don't include a course in database and SQL. There are of course some arcade corners of SQL (I sometimes have to look up the different JSONB operators in postgres), but WHERE and HAVING ain't it.

> flipping a coin on whether the query engine will use the index and execute in 10 ms, or decide to follow a different execution plan and take 5 minutes to run

Heavens, if you think using an ORM is going to help you here, have you got another think coming.

Your take on this is so foreign to me I was honestly shocked to see it. That being said, you probably aren't the only one, so perhaps my perspective is more narrow than I had assumed as well.

Re:Databases are the endgame for data-oriented design by fuy on Friday December 08, 2023 @06:40AM (#38567816)
The common sentiment on HN I'd say is the opposite: ORM's are needed for junior devs who didn't have time to learn SQL properly. After NoSQL hype died out (around 5 years ago), the HN attitude towards both ORM's and using NoSQL instead of relational dbs has been mostly negative for sure.
Re:Databases are the endgame for data-oriented design by fifilura on Wednesday December 06, 2023 @02:27PM (#38548359)
I am happy for all the disagreeing posts and that HN is a place for it.

It also helps me understand my colleagues.

It may be how our brains are wired. For me whenever I see a for loop I see bugs.

It is also about that you get the concurrency from map/reduce-type jobs for free without having to think about it.

But yeah there are a couple of places you need to be careful. Avoiding duplicates and handling of NULL.

Re:Databases are the endgame for data-oriented design by deadbabe on Wednesday December 06, 2023 @02:01PM (#38548086)
Are you using Postgres? It seems like you haven’t discovered CTEs or lateral joins.
Re:Databases are the endgame for data-oriented design by crazygringo on Wednesday December 06, 2023 @02:03PM (#38548120)
Oh, I've worked with all the major engines. To which we can add: trying to remember which seemingly basic features are supported by which engines, and which aren't (e.g. percentiles are great in Postgres, but you're SOL in MySQL). That there's virtually nothing "standard" about SQL syntax or functionality at all, except for the simplest of queries and the general basic concepts.
Re:Databases are the endgame for data-oriented design by magicalhippo on Wednesday December 06, 2023 @04:01PM (#38549416)
We're in the process of migrating our on-premise business application from SQLAnywhere to MSSQL.

Imagine the fun we're having...

Re:Databases are the endgame for data-oriented design by eddd-ddde on Wednesday December 06, 2023 @04:19PM (#38549610)
I'm really curious, in the practice, what causes a product to decide "we are moving from abcSQL TO xyzSQL" ?
Re:Databases are the endgame for data-oriented design by magicalhippo on Wednesday December 06, 2023 @05:21PM (#38550306)
For us there's been two major motivations, and another few lesser.

First and most prominent is that Sybase was bought by SAP and SAP has pivoted SQLAnywhere from a general RMDBS to focus on mobile applications (ie more embedded). Apart from product development changing focus we've struggled to buy licenses for our customers, so the writing on the wall was clear.

The seconds is that we've got some big customers who run their own MSSQL servers, and they've been pushing harder and harder for us to use their servers so they can handle administration and access control directly.

A third motivation is that while we've been primarily on-prem, a lot of customers these days don't want that, so we've started offering cloud hosted options. Since we're a Windows shop Azure made sense, and as such the hosted MSSQL also makes sense and is quite convenient.

Fourth is that, while MSSQL is quite anemic in terms of SQL language functionality, it does support transactional schema updates, which makes schema upgrades much less error prone.

Given that MSSQL syntax support is quite limited and we now have developed tooling and library support for multiple databases, we'll probably also add support for PostgreSQL later.

Re:Databases are the endgame for data-oriented design by crazygringo on Wednesday December 06, 2023 @04:35PM (#38549813)
In that direction, it's usually going to be about robustness -- more tooling, less edge cases, a wider developer base, built-in cloud support, and so forth. The more popular a database is, the easier just everything is.

Occasionally it's going to be some very specific aspect of performance limits, or a feature like spatial indexes that work in a special way. And occasionally it's going to be about cost or better integration with a particular cloud or other specific piece of tooling.

Re:Databases are the endgame for data-oriented design by paulddraper on Wednesday December 06, 2023 @03:44PM (#38549242)
Well Java and JavaScript differ a lot too.

Some similarities, but a lot of differences too.

Re:Databases are the endgame for data-oriented design by stvltvs on Wednesday December 06, 2023 @02:32PM (#38548402)
I agree that CTEs are a solution to some of the pain listed above, but CTEs aren't exotic. Lots of the major RDBMSs support them.
Re:Databases are the endgame for data-oriented design by fifilura on Wednesday December 06, 2023 @03:07PM (#38548828)
CTEs are an absolute must to get the right "do-this-then-that" approach.
Re:Databases are the endgame for data-oriented design by bob1029 on Wednesday December 06, 2023 @05:13PM (#38550234)
We've been doing this for a while and it has some pretty severe limitations at scale. The biggest issue is that humans are human and you cannot make everyone else on the team use your elegant SQL DSL business engine in exactly the same way you had envisioned. Eventually, it will grow into a bit of a monster.

We are headed back in the other direction, but with some hedge. Code with a very simple & transparent CI/CD experience seems equivalent to some SQL configurable thing in our minds now, but with way more potential upside.

In our latest code-based abstractions, nothing is stopping you from breaking out a SQL connection and running any arbitrary query. In fact, all of the data is still in SQL and it's still authoritative, you just now have access to way more powerful tooling to work with that data. Code+SQL together is the answer.

For non-expert team members, modern C# is turning out to be way more intuitive than SQL the moment you encounter a 3+ table join scenario.

Re:Databases are the endgame for data-oriented design by devjab on Thursday December 07, 2023 @12:32AM (#38553196)
I think you make some decent points, but at the same time I really don’t understand this part:

> For non-expert team members, modern C# is turning out to be way more intuitive than SQL the moment you encounter a 3+ table join scenario.

Partly because this is so inefficient that it’s never going to work at scale, but also because of how you’re moving the complexity into the realm of “magic”.

I know it’s very easy and intuitive to use one of C#s ORMs, but with that comes an reliance on things like linq and a model builder, both of which may not work the way you think they do. If your developers think about it at all, and having seen so many C# developers use IEnumerable where they really should have been using IQuerable… well if your developers can’t do relatively basic SQL then I’m not sure I’d trust them with the abstraction either.

I don’t think you’re really, wrong, either, but I think it’s much more a question of building developer tools to help your developers handle good data access than it is about picking a particular tech on the consuming side.

As far as data storage goes, however, I think we’re just beginning to see the move away from the classics because the classics just weren’t build to support how we use data in 2023.

I can’t begin to tell you the nightmare it is to update old school SQL “data wells” to be capable of temporality, good BI access as well as being compliant with the various EU legislative rules, and almost none of that can be done above the database. Well I guess you could, but you’d probably go insane. And that’s just path of it, the other part is just how much data we transact now. It used to be that banks were basically the only organisations to move massive amounts of tiny bits of data, and now we all do it. Like, a single solar plant moves a gazillion points of data into your various systems a minute, where 30 years ago the entire data for that place might’ve been a couple of kb a year, it’s now mbs a day.

Re:Databases are the endgame for data-oriented design by buryat on Wednesday December 06, 2023 @01:50PM (#38547977)
that's how you end up with multi-thousand LOC sql files that contain all the historical business logic and edge cases of a company and it just keeps being piled up on and its complexity keeps growing because not everything is expressible in SQL. So you end up having a big monster that someone needs to support and one that's not very testable easily
Re:Databases are the endgame for data-oriented design by ndriscoll on Wednesday December 06, 2023 @04:38PM (#38549851)
Testing SQL is easy. E.g. the JVM has H2, which works for simple stuff, or you can use testcontainers, or just spin up a container and run your tests against that. You just run your migrations, insert mock data, and run your test.

In fact testability is one of the best parts. You can safely test read-only queries against a prod secondary database to see that it gives reasonable results on real data, and use the repl to explore parts of your query to really get a sense that things are working.

Re:Databases are the endgame for data-oriented design by whizzter on Wednesday December 06, 2023 @05:56PM (#38550636)
SQL queries yes, what Bury above probably is talking about is obscure triggers/stored-procs,etc that encodes the business logic inside the database far away from any version control or sane ways to track it.
Re:Databases are the endgame for data-oriented design by fooododododo on Thursday December 07, 2023 @01:24PM (#38559853)
Tracking triggers etc is pretty straightforward, just put them in a repo and apply them as part of a schema change. Testing them can be tricky, as unit tests aren't as likely to capture the locks/time that can be an issue when the database is under load. Tools like pgreplay can be helpful here.
Re:Databases are the endgame for data-oriented design by ttfkam on Thursday December 07, 2023 @08:33PM (#38564292)
It's not like tracking events in application logic is somehow just a bucket of unicorn farts and rainbows. At least triggers are deterministic and tracked within transactions. Can't tell you how often folks update the DB with an ORM in application logic but forget to update dependent records in other tables in their one-off branch of logic.

Sometimes it's a hard problem.

That said, I think triggers need very well-maintained dependency charts in the docs/comments to ensure they don't ever go cyclical. Those are bad days.

(But again, events in app logic are in no way shape or form immune to cyclic runaways.)

Re:Databases are the endgame for data-oriented design by fooododododo on Friday December 08, 2023 @02:56PM (#38573403)
Agree re trigger maintenance

Re orm, I'm strongly biased towards only using them when they (1) are absolutely necessary or provide some extreme benefit (2) are able to output the raw SQL to be executed on the db (3) the SQL to be executed is understood.

I've seen them used as a substitute for understanding SQL/databases and, personally, think that's a pretty bad idea.

Re:Databases are the endgame for data-oriented design by ako on Wednesday December 06, 2023 @03:33PM (#38549103)
And in any other language this wouldn’t result in multi-thousand lines of code with historical business logic and edge cases?
Re:Databases are the endgame for data-oriented design by manicennui on Wednesday December 06, 2023 @04:22PM (#38549637)
The difference is that most people don't write their business logic in a single flat function in most programming languages, and there is a good chance that individual pieces would have tests.
Re:Databases are the endgame for data-oriented design by ako on Wednesday December 06, 2023 @04:28PM (#38549716)
Neither do good SQL developers: you can easily structure database logic in multiple stored functions, stored procedures, packages, and multiple files. Unit test frameworks are also available.
Re:Databases are the endgame for data-oriented design by robertlagrant on Wednesday December 06, 2023 @04:23PM (#38549657)
Testing code is pretty popular in most non-SQL environments.
Re:Databases are the endgame for data-oriented design by ttfkam on Thursday December 07, 2023 @08:35PM (#38564309)
Testing SQL is pretty popular in most pro-SQL environments as well. There are many tools for that even if many developers appear to remain willfully ignorant of them.
Re:Databases are the endgame for data-oriented design by cma256 on Wednesday December 06, 2023 @01:54PM (#38548008)
Glad that doesn't happen any where else!
Re:Databases are the endgame for data-oriented design by nightpool on Wednesday December 06, 2023 @03:06PM (#38548818)
Most other programming languages are more composable than a SQL query. You can make functions, split logic into different classes, extract common logic, etc. Much harder with big sql queries
Re:Databases are the endgame for data-oriented design by camgunz on Wednesday December 06, 2023 @03:56PM (#38549348)
You can define functions in all major databases
Re:Databases are the endgame for data-oriented design by manicennui on Wednesday December 06, 2023 @04:22PM (#38549644)
And how many non-technical users do you know who do this?
Re:Databases are the endgame for data-oriented design by ako on Wednesday December 06, 2023 @04:46PM (#38549958)
And how many non-technical users do you know that write good javascript/java/.net/etc code?
Re:Databases are the endgame for data-oriented design by camgunz on Thursday December 07, 2023 @04:19AM (#38554481)
I think SWEs think of databases only as a kind of generic persistence layer and aren't super interested in a lot of the details (more than one has said to me databases are just an implementation detail) or additional capabilities, which I think is a very limited view. Databases are essentially interpreters, just like Python or Ruby. They come with access to a multiuser persistence (etc) service or engine (super handy!) but offer all the affordances of other interpreters. In fact, plpython on PostgreSQL will just use the Python packages on your system.

Do you have to read about it? Sure, just like all programming languages and platforms. But, interestingly, SQL was designed for non-technical analysts, so (to your point) it's more accessible than those others.

Re:Databases are the endgame for data-oriented design by ttfkam on Thursday December 07, 2023 @08:38PM (#38564325)
Mediocre coders fixate on algorithms. Great coders concentrate on data structures.

SQL schema creation and maintenance is all about choosing the right data structures for the job at hand.

If you neglect attention to your data structures, no algorithms will save you, and you really only have yourself to blame. It's a still to be learned and honed like any other.

Re:Databases are the endgame for data-oriented design by ako on Wednesday December 06, 2023 @03:32PM (#38549093)
Use views and stored procedures if you need composability.
Re:Databases are the endgame for data-oriented design by FridgeSeal on Wednesday December 06, 2023 @03:48PM (#38549280)
Counterpoint: don’t do this. The path to hell is paved with good intentions.

Some co-workers and I inherited a code-base where the authors went down the views and stored procedures route. It was basicallly impossible to untangle; there was no knowing what relied on a view or a proc, so you couldn’t touch them at all, there were no docs, there were duplicates of everything (and again, no way to know what’s used). A good number of them had all kinds of side effects and weird performance characteristics. They weren’t version controlled.

The issues just kept going.

Re:Databases are the endgame for data-oriented design by whstl on Wednesday December 06, 2023 @04:19PM (#38549606)
If your colleagues are not applying basic engineering rules to views and stored procedures, it's not fair to say that this is a problem with views and sprocs.

They can and should most definitely be testable, documentable, trackable and version controlled.

Should one also judge all backend programming by PHP standards circa 1998?

Re:Databases are the endgame for data-oriented design by FridgeSeal on Wednesday December 06, 2023 @04:24PM (#38549664)
I tend to judge things by the average-worst-case that they enable and how easily things go wrong, and IME stored procs specifically, go badly real quick.
Re:Databases are the endgame for data-oriented design by ttfkam on Thursday December 07, 2023 @08:42PM (#38564360)
Data structures can be a hard part of programming. That doesn't mean folks don't chose the wrong data structures all the time in any language.

An array instead of a linked list or vice versa. A dequeue instead of a set or vice versa. A binary tree instead of a B+ tree. You can't just throw column names at a DB schema and hope for the best. There's actual engineering to be done, even if many programmers refuse to admit it when interacting with a relational database engine.

Re:Databases are the endgame for data-oriented design by ako on Wednesday December 06, 2023 @04:24PM (#38549668)
You're blaming the language for the bad coding practices of the developers. You can build maintainable database code, and modern SQL IDEs like Datagrip provide similar tools for SQL like you have for other languages, e.g., refactoring, dependency/usage info, versioning, etc. The downsides you mention are not inherent to SQL.
Re:Databases are the endgame for data-oriented design by nextworddev on Wednesday December 06, 2023 @01:53PM (#38548004)
What you need is a semantic layer on top of SQL
Re:Databases are the endgame for data-oriented design by ttfkam on Thursday December 07, 2023 @08:45PM (#38564383)
SQL is the semantic layer on top of relational algebra. Adding another layer on top is like having Google translate from Spanish to Russian before translating back to English. Each level of indirection just opens the door for more nonsense to creep in.
Re:Databases are the endgame for data-oriented design by ttfkam on Thursday December 07, 2023 @08:28PM (#38564254)
You can write code in any language without defining functions or modularizing in any way. You can write bad code in any language.

For some reason, folks assume SQL must be written badly since they have only written it badly or seen it written so.

It is absolutely possible and preferable to write maintainable SQL logic into user defined functions, stored procedures, views, materialized views, CTEs, temporary tables, etc. If you're looking at one huge pile of monolithic, untestable SQL, the problem isn't the SQL.

One doesn't write O(n^3) algorithms in C++ and then blame C++ for it being slow. For some reason, folks seem pleased with themselves to do as such with SQL though every day and twice on Sunday.

Got subselects in each of the fifteen outer joins with NULLs all over your schema, and now you're upset performance is horrible and inconsistent? PEBKAC.

Re:Databases are the endgame for data-oriented design by Etheryte on Wednesday December 06, 2023 @01:14PM (#38547518)
I feel like you've come to the right conclusion, but partly for the wrong reasons. Terseness in and of itself is not useful per se. Code golfing languages are the tersest there is, but we don't write code that way.
Re:Databases are the endgame for data-oriented design by ndriscoll on Wednesday December 06, 2023 @04:29PM (#38549726)
Or they just used the wrong word. I'd agree with them, but clarify that SQL is not just terse, but concise.

A join or a group by is going to be much clearer than writing the code the query plan is going to generate, creating temporary hash maps, doing nested loops, etc.

Re:Databases are the endgame for data-oriented design by ttfkam on Thursday December 07, 2023 @08:47PM (#38564398)
Yep, because unlike general purpose programming languages, you describe WHAT data you want from SQL, not HOW to get that data as in most application languages.
Re:Databases are the endgame for data-oriented design by fifilura on Sunday December 10, 2023 @03:31PM (#38594748)
Exactly. And yes, maybe concise was the word I meant to use (not native English speaker).
Re:Databases are the endgame for data-oriented design by randomdata on Wednesday December 06, 2023 @01:21PM (#38547605)
> And that in itself is the most important factor to avoid bugs.

I'm not sure about that. SQL was the first language I learned and the language that has always been there throughout the decades, and is also where I make the most mistakes.

Re:Databases are the endgame for data-oriented design by hot_gril on Wednesday December 06, 2023 @02:45PM (#38548553)
If you were to write the equivalent of a SQL query in some other language, you'll probably make a lot more mistakes. Especially if you're trying to achieve the same performance. And I mean a read-only query, not even something with multi-writes and locking.
Re:Databases are the endgame for data-oriented design by randomdata on Thursday December 07, 2023 @08:55AM (#38556298)
I don't think that is true as I could use a better relational calculus that exhibits all the of the same positive attributes that SQL brings to the table. SQL's traps are not all fundamental, just straight up poor language design. SQL's biggest trap is where it grossly deviates from Codd's relational algebra model. A relational system that is more "pure" would solve many bug vectors right out of the gate.
Re:Databases are the endgame for data-oriented design by hot_gril on Thursday December 07, 2023 @12:48PM (#38559408)
What's an example of a SQL query that's misleading but wouldn't be if it stayed more like Codd's model?

The issues I have with SQL are simpler things like `timestamp with time zone` not really storing a time zone and actually being the thing you want for tz-independent timestamps.

Re:Databases are the endgame for data-oriented design by randomdata on Thursday December 07, 2023 @01:04PM (#38559593)
I'm not sure I have a great example in front of me – the bugs get fixed. I can point out that even `SELECT * FROM table` violates the relational model. I don't expect anyone is encountering bugs from that simple query as it is easy enough to reason about, but when you compound the complexity it becomes harder to remember where SQL is non-relational and to remember to account for that.

I suppose it can be avoided with extreme care, like properly managing memory in C, but a good language will help you out. Much like C, SQL is old so those traps may be excusable in a legacy language, but it is shame that we haven't put much effort into building something more modern. Where is the 'Rust' of SQL?

Re:Databases are the endgame for data-oriented design by hot_gril on Thursday December 07, 2023 @03:02PM (#38561112)
Closest thing to the "Rust" of SQL is GoogleSQL, in that it has a little more safety. But only a little.
Re:Databases are the endgame for data-oriented design by randomdata on Thursday December 07, 2023 @04:53PM (#38562335)
I'm not sure GoogleSQL is, for all intents and purposes, anything more than just a particular SQL implementation. Notably, it still breaks from the relational model in the same way and, as such, is prone to the very same bugs of which we speak.

The closest thing to the 'Rust' of SQL is probably Datalog. Even something like QUEL, which Postgres used for the first decade of its life, would have likely left us in a much better place.

But Oracle won the database wars, so we got left with the junk that came with it – for compatibility initially, and now we can't seem to move past SQL because the typical developer somehow has come to think SQL and relational algebra/calculus are the exact same thing...

Re:Databases are the endgame for data-oriented design by hot_gril on Thursday December 07, 2023 @06:11PM (#38563073)
SQL isn't the purest, but I've yet to see a real-life example of some other query language that gracefully handles queries that would be difficult in SQL. Looking at these other languages with an open mind, I still don't see them making things easier. If there's something better, then there should be examples out there showing a stark contrast in usability.

The only real alternative I can think of is the map-reduce pattern (see PySpark), but it's for a different use case with some overlap. I have replaced SQL queries with map-reduce once before, and only because the application really didn't make sense to use SQL for.

Re:Databases are the endgame for data-oriented design by randomdata on Thursday December 07, 2023 @07:18PM (#38563732)
> I've yet to see a real-life example of some other query language that gracefully handles queries that would be difficult in SQL.

That's exactly it. Nothing has come anywhere close to seeing the same kind of effort as SQL put into it. SQL is the best application of relational calculus (or close approximation, at least) we have – but that doesn't make it good. A concerted effort to build a better application of relational calculus would undoubtedly yield a major improvement for developers, on the order of how something like Rust has improved over C, and as I said before it is unfortunate that we as an industry are not showing interest in a better language like we do with regards to general purpose computing.

SQL gets the job done, but we can do better.

> The only real alternative I can think of is the map-reduce pattern

The alternative is to build a better language, not jump to a completely different and unrelated model. Indeed, there is a place for map-reduce for certain problems, but they are outside of where you would use the relational model.

Re:Databases are the endgame for data-oriented design by vlovich123 on Wednesday December 06, 2023 @03:44PM (#38549254)
I’ve heard that datalog (and similar languages in that vein) is far easier and terser to write. What do you think about that?
Re:Databases are the endgame for data-oriented design by hot_gril on Wednesday December 06, 2023 @02:44PM (#38548540)
Same, every backend service I write now has the majority of the business logic in SQL and a little pre/post-processing in regular code. A well-designed schema will mean that your queries don't get messy. If some more complex thing starts feeling forced, I add a bit more non-SQL code to make it reasonable.

When teammates look at my code, and logic is all right there instead of scattered around, and there's a schema file backing it all that makes it clear what all the relations are, they have an easy time making tweaks or adding on. Yes it's very testable too.

This also kinda depends on having a multi-service architecture if your system is large. Separate database for each. That's a good thing anyway.

Re:Databases are the endgame for data-oriented design by whstl on Wednesday December 06, 2023 @04:27PM (#38549697)
The fear of committing SQL to the codebase is one of the most baffling things in modern backends.

To me the most infuriating thing is the "SQL query scattered around multiple files" pattern, where a backend engineer will decompose a perfectly fine SQL query into 3 or 4 files, with multiple functions, often with very artificial separations (for example: a function just for the "select ..." part, another for the joins).

All that in the name of having small files, small functions, small lines. You take complexity away from the "micro" parts and embed it into the invisible parts of your program.

Re:Databases are the endgame for data-oriented design by hot_gril on Wednesday December 06, 2023 @05:55PM (#38550630)
We're using some tool at work that recommends putting SQL into dedicated separate files, one file per query. Or you can use the query builder which is just SQL but with extra steps, limitations, and caveats. It's so annoying.
Re:Databases are the endgame for data-oriented design by icedchai on Wednesday December 06, 2023 @07:39PM (#38551437)
I hate this, also. This was a "design pattern" (antipattern) at a previous company. It was incredibly difficult to navigate SQL built like this. We were also asked to write "unit tests" for each micro-function. This was essentially a pointless exercise that compared each function call to a string, copy-and-pasted from the function itself, with no regard to the correctness of the overall query. At least it was easy to make the tests pass.
Re:Databases are the endgame for data-oriented design by surprisetalk on Wednesday December 06, 2023 @01:48PM (#38547958)
I completely agree!

Transaction-safety is also sublime for business logic.

Re:Databases are the endgame for data-oriented design by devoutsalsa on Wednesday December 06, 2023 @02:59PM (#38548732)
Have you written an ORM in SQL to help make querying easier?