News for hackers. Stuff that matters.
Re:Making Database Systems Usable by pjs_ on Tuesday August 20, 2024 @06:16PM (#41304660)
A huge fraction (not 100%, but maybe 80%) of my frustration in trying to get technical people to use a database is that they have such a hard time understanding JOINs.

People endlessly want hacks, workarounds, and un-normalized data structures, for a single reason - they don't want to have to think about JOIN. It's not actually for performance reasons, it's not actually for any reason other than it's easier to imagine a big table with lots of columns.

I'm actually sympathetic to that reticence but what I am not sympathetic about is this: why, in 2024, can't the computer figure out multi-table joins for me?

Unless your schema is really fucked up, there should only be one or two actually sensible ways to join across multiple tables. Like say I have apples in boxes in houses. I have an apple table, a box table, and a house table. apples have a box_id, boxes have a house_id. Now I want to find all the apples in a house. Do two joins, cool. But literally everyone seems to write this by hand, and it means that crud apps end up with thousands of nearly identical queries which are mostly just boringly spelling out the chain of joins that needs to be applied.

When I started using SQLAlchemy, I naively assumed that a trivial functionality of such a sophisticated ORM would be to implement `apple.join(house)`, automatically figuring out that `box` is the necessary intermediate step. SQLAlchemy even has all the additional relationship information to figure out the details of how those joins need to work. But after weeks of reading the documentation I realized that this is not a supported feature.

In the end I wrote a join tool myself, which can automatically find paths between distant tables in the schema, but it seems ludicrous to have to homebrew something like that.

I'm not a trained software engineer but it seems like this must be a very generic problem -- is there a name for the problem? Are there accepted solutions or no-go-theorems on what is possible? I have searched the internet a lot and mostly just find people saying "oh we just type out all combinatorially-many possible queries"... apologies in advance if I am very ignorant here

Re:Making Database Systems Usable by big_whack on Tuesday August 20, 2024 @06:40PM (#41304833)
It's not really a problem of there being combinatorially many ways to join table A to table B, but rather that unless the join is fully-specified those ways will mostly produce different results. Your tool would need to sniff out these ambiguous cases and either fail or prompt the user to specify what they mean. In either case the user isn't saved from understanding joins.
Re:Making Database Systems Usable by halfcat on Tuesday August 20, 2024 @09:13PM (#41305789)
> ”but rather that unless the join is fully-specified those ways will mostly produce different results.”

As a SQL non-expert, I think this is why we are averse to joins, because it’s easy to end up with more rows in the result than you intended, and it’s not always clear how to verify you haven’t ended up in this scenario.

Re:Making Database Systems Usable by big_whack on Tuesday August 20, 2024 @10:13PM (#41306155)
Sorry, but someone who is averse to joins is not a non-expert in SQL, they are a total novice. The answer is like any other programming language. You simply must learn the language fundamentals in order to use it.
Re:Making Database Systems Usable by halfcat on Wednesday August 21, 2024 @08:56AM (#41309820)
Not sorry, I’ll stick with SQL non-expert as I’ve only worked with databases for a few decades and sometimes run into people who know more.

Working with a database you built or can control is kind of a simplistic example.

In my experience this most often arises when it’s someone else’s database or API you’re interacting with and do not control.

An upstream ERP system doesn’t have unique keys, or it changes the name of a column, or an accounting person adds a custom field, or the accounting system does claim to have unique identifiers but gets upgraded and changes the unique row identifiers that were never supposed to change, or a user deletes a record and recreates the same record with the same name, which now has a different ID so the data in your reporting database no longer has the correct foreign keys, and some of the data has to be cross-referenced from the ERP system with a CRM that only has a flaky API and the only way to get the data is by pulling a CSV report file from an email, which doesn’t have the same field names to reliably correlate the data with the ERP, and worse the CRM makes these user-editable so one of your 200 sales people decides to use their own naming scheme or makes a typo and we have 10 different ways of spelling “New York”, “new york”, “NY”, “newyork2”, “now york”, and yeah…

Turns out you can sometimes end up with extra rows despite your best efforts and that SQL isn’t always the best tool for joining data, and no I’m not interested in helping you troubleshoot your 7-page SQL query that’s stacked on top of multiple layers of nested SQL views that’s giving you too many rows. You might even say I’m averse.

Re:Making Database Systems Usable by rawgabbit on Wednesday August 21, 2024 @12:12PM (#41311616)
I have run into this scenario a few times where the multi-hour processes produces an explosion of rows that no one cared to troubleshoot further. They only wanted to de-duplicate the final result.

In practice, I ended up creating a skeleton table that not only had the appropriate constraints for de-duplication. But I would also create mostly empty rows (empty except for the required column key ID fields) with the exact rows they were expecting. And then I would perform an UPDATE <skeleton> ... FROM <complicated join>. It is a hack but if there was no desire to rewrite a process that was written years ago by teams of consultants, I can only do what I can do.

Re:Making Database Systems Usable by yunolearn on Wednesday August 21, 2024 @12:20PM (#41311706)
Nobody is entitled to any of this being easy. If you don't like working with badly-designed databases, why not simply work with people who know how databases work? In the meantime, I have bad news: nobody is going to do the hard work for you.
Re:Making Database Systems Usable by consteval on Wednesday August 21, 2024 @12:28PM (#41311791)
Yes when you have duplicated data and data inconsistencies/integrity issues you might get duplicate data and data inconsistencies/integrity issues in your output.

This is a problem of form, not method. JOINs are a fantastic, well-defined method to aggregate data. If the form of your data is messed up, then naturally the result may be too.

> no I’m not interested in helping you troubleshoot your 7-page SQL query that’s stacked on top of multiple layers of nested SQL views that’s giving you too many rows

People say this type of thing but SQL is an incredibly high-level language.

Yes debugging a big SQL query can suck. Debugging the equivalent of it is almost always much worse. I'd rather debug a 30-line query than a 400 line perl script that does the same thing. Because that's actually the alternative.

I have manually aggregated data, messy data, in awk, perl, python... it is much, much worse.

Re:Making Database Systems Usable by halfcat on Wednesday August 21, 2024 @10:54PM (#41316383)
> ”I have manually aggregated data, messy data, in awk, perl, python... it is much, much worse.”

Yes, but with Python/etc you can at least do the same logic in a plain loop, which is much slower but serves as a more reliable reference of what the data is supposed to be, which can be used to validate the functionality of SQL output.

Is there an equivalent in SQL of this “slow and dumb” approach for validating? Like, I’m not sure if a lateral join is essentially doing the same thing under the hood.

Re:Making Database Systems Usable by rawgabbit on Thursday August 22, 2024 @07:56PM (#41325240)
Most databases have the concept of temporary tables that will automatically disappear when your session ends. For troubleshooting, I would breakdown each step and save it in a temp table. Validate it. Then use it as the input for the next step. Etc.
Re:Making Database Systems Usable by sa46 on Wednesday August 21, 2024 @01:06AM (#41306946)
I wish there were a way to specify the expected join cardinality syntactically for 1:1 joins.

    SELECT * FROM order JOIN_ONE customer USING (customer_id)
Malloy has join_one: https://docs.malloydata.dev/documentation/language/join
Re:Making Database Systems Usable by cess11 on Wednesday August 21, 2024 @07:41AM (#41309283)
Don't have a suitable database to test on but I'm pretty sure standard SQL allows you to join on a subselect that limits its result to one.
Re:Making Database Systems Usable by singron on Wednesday August 21, 2024 @08:37AM (#41309673)
You probably want to use LATERAL JOIN, which will compute the subquery for each left hand row. Otherwise it's computed just once.
Re:Making Database Systems Usable by cess11 on Wednesday August 21, 2024 @09:51AM (#41310298)
With SQL there are commonly several options available, and tradeoffs might not be obvious until it's tested against a real data set. Sometimes even an EXPLAIN that looks good still has some drawback in production, though in my experience it's very rare.

I like that though, I have a preference for REPL style development and fleshing things out interactively. Enough so that I build Java like that, hacking things out directly in tests.

Re:Making Database Systems Usable by yunolearn on Wednesday August 21, 2024 @12:21PM (#41311715)
To verify, you have to think through the problem and arrive at a solution, like all of programming.
Re:Making Database Systems Usable by rawgabbit on Tuesday August 20, 2024 @06:42PM (#41304846)
Most databases have the concept of foreign keys. You declare how the tables relate to each other. You can then write a script that queries this metadata to write the join for you. I did this sort of thing over twenty years ago.
Re:Making Database Systems Usable by Spivak on Tuesday August 20, 2024 @11:46PM (#41306598)
Most ORMs have this feature as well and sorry to say you will hit edge cases where the join is ambiguous and have to manually specify it pretty fast.
Re:Making Database Systems Usable by KronisLV on Wednesday August 21, 2024 @01:37AM (#41307103)
> where the join is ambiguous

Join table that maps to an entity in the middle.

You can even have multiple columns that have foreign keys against various tables, like some_table_id, other_table_id, another_table_id with only the needed ones being filled out.

And in practice, this will be way more manageable than the dynamic mess of the OTLT pattern (table_name and table_id): https://www.red-gate.com/simple-talk/blogs/when-the-fever-is...

It's not like you have to particularly care about the fact that most of those columns will be empty in practice, as opposed to making your database hard to query or throwing constraints aside altogether.

Re:Making Database Systems Usable by Terr_ on Wednesday August 21, 2024 @02:22AM (#41307330)
> Join table that maps to an entity in the middle.

I'm not not sure what you mean. Are you saying that instead of FK/PK relations like:

    user.residence_country_id        = country.id 
    plus user.citizenship_country_id = country.id
You would reify every edge like:

    user.residence_link_id           = link_user_residence.user_id
    link_user_residence.country_id   = country.id
    user.citizenship_link_id         = link_user_citizenship.user_id
    link_user_citizenship.country_id = country.id
Even then, the reverse "I have a country gimme a user" request is ambiguous.
Re:Making Database Systems Usable by Izkata on Wednesday August 21, 2024 @08:02PM (#41315396)
GP misunderstood what an "ambiguous join" is. Rather than a join that returns multiple results, they're more like when you have relationships like this [0]:

  user.residence1 -> country
  user.residence2 -> country

  user.join(country)
You have to specify somewhere which foreign key to follow, it can't be figured out automatically. GP's suggestion doesn't work because there's no way to automatically determine which one to use.

I'm only really familiar with Django, and it handles this type of thing by making you specify the "residence1" name instead of the table name "country".

[0] To refer back to the top of this comment chain, that user is complaining they can't do something like also have a "country->continent" relationship and do "user.join(continent)" and have the system figure out the two joins needed.

Re:Making Database Systems Usable by rawgabbit on Thursday August 22, 2024 @11:57PM (#41326074)
Putting on my dogmatic 3NF purist hat. Instead of denormalized

   user.residence1 -> country.id
   user.residence2 -> country.id

I would create a many to many bridge table. To accommodate multiple residences for a given user.

   bridge.userid-> user.id
   bridge.countryid-> country.id
Re:Making Database Systems Usable by Izkata on Friday August 23, 2024 @06:25PM (#41333609)
Oh definitely, I just couldn't think of a better example off the top of my head. But also not really relevant: You could also have another table and the relationships be A -> B -> D and A -> C -> D, with the desired join between A and D.

Remember this whole thread is about the system figuring out the JOIN criteria automatically.

Re:Making Database Systems Usable by pjs_ on Wednesday August 21, 2024 @06:31PM (#41314843)
Yep, that's exactly what I did - using the ORM schema definition rather than the schema directly, but same difference
Re:Making Database Systems Usable by crazygringo on Tuesday August 20, 2024 @07:18PM (#41305082)
> In the end I wrote a join tool myself, which can automatically find paths between distant tables in the schema, but it seems ludicrous to have to homebrew something like that.

I mean, I think the issue is just that there are lots of possible paths so it can't be automated.

If you want to join employees to buildings, is it where their desk is assigned today, or where their desk was assigned two years ago, or where their team is based even though they work remotely, or where they last badged in?

Sure you can build a tool to find all potential joins based on foreign keys, but then how do you know which is correct unless you understand what the tables mean? And then if you understand what the tables mean, writing the join out yourself is trivial.

> Unless your schema is really fucked up, there should only be one or two actually sensible ways to join across multiple tables.

In my experience, having just one or two ways is for simple/toy projects. Lots of joins in no way means a schema is "fucked up". It probably just means it's correctly modeling actual relationships, correctly normalized.

Re:Making Database Systems Usable by Terr_ on Wednesday August 21, 2024 @12:22AM (#41306766)
Real-world example: Someone wants Job-Application by Country, but that could mean via Applicant's Residence, the Applicant's Nationality, the Requisition Primary location, or one of the Requisition's Satellite Offices, etc.

... And god help you if someday Requisitions need to have Revisions too.

Re:Making Database Systems Usable by pjs_ on Wednesday August 21, 2024 @06:42PM (#41314912)
Yes, this is accurate. You often end up with many solutions and human judgement is sometimes required in the end to pick the right strategy. However, what I have found in practice is that heuristics and hinting can rapidly cut through that complexity. E.g. "always pick the shortest path between tables, and never use this set of tables as intermediate nodes on any path" rules out a ton of options, and usually will leave you with one or two, and usually those are the natural choices.

In this way you can use polynomially-many constraints or rules to avoid exponentially-many weird or exceptional routes through the schema. I am optimistic that you can build a system where by default, the autojoin solution is the natural one maybe 90% of the time. There will certainly be exceptions where you have to express the join conditions explicitly. But I think you can dramatically reduce the amount of code required.

I would also hazard the suggestion that this might produce productive backpressure on the system. If the autojoiner is struggling to find a good route through the schema, it's possible that the schema is not properly normalized or otherwise messed up.

Re:Making Database Systems Usable by sgarland on Tuesday August 20, 2024 @08:23PM (#41305499)
> People endlessly want hacks, workarounds, and un-normalized data structures, for a single reason - they don't want to have to think about JOIN. It's not actually for performance reasons, it's not actually for any reason other than it's easier to imagine a big table with lots of columns.

That's because, inexplicably, devs by and large don't know SQL, and don't want to learn it. It's an absurdly simple language that should take a competent person a day to get a baseline level of knowledge, and perhaps a week to be extremely comfortable with.

As an aside, something you can do is create views (non-materialized) for whatever queries are desired. The counter-arguments to this are that is slows development velocity, but then, so does devs who don't know how to do joins.

Re:Making Database Systems Usable by rtpg on Tuesday August 20, 2024 @08:53PM (#41305680)
it's "absurdly simple", but then you get presented with a bunch of weird things that look like abstraction ceilings like "oh you can't refer to the select clause alias you made in the filter because despite that showing up first lexicographically the ordering is different" and "oh you don't have to refer to the table name except when you do because of ambiguity issues".

I think there's a beautiful space for some SQL-like language that just operates a bit more like a general-purpose language in a more regular fashion. Bonus points for ones where you don't query tables but point at indexes or table scans and the like (resolving the "programmer writes query that is super non-performant because they assume an index is present when it's not").

I think it's still super straightforward to sit down and learn it, but it's really unfortunate that we spend a bunch of time in school learning data structures and then SQL tries really hard to hide all that, making it pretty opaque despite people intuitively understanding B-Trees or indexes.

Re:Making Database Systems Usable by sgarland on Wednesday August 21, 2024 @12:17AM (#41306742)
> "oh you don't have to refer to the table name except when you do because of ambiguity issues"

Maybe it's easier if you think of it like helpful syntactic sugar?

> I think there's a beautiful space for some SQL-like language that just operates a bit more like a general-purpose language in a more regular fashion. Bonus points for ones where you don't query tables but point at indexes or table scans and the like

That sounds like imperative programming, which is fine for most things, but [generally] not RDBMS (or IaC, but that's a completely separate topic). You can't possibly know the state of a given table or index – the cardinality of columns, the relative grouping of tuples to one another, etc. While you can hint at index usage (natively with MySQL, via extension with Postgres), that's as close as the planner will let you get, because it knows these things better than you do.

> resolving the "programmer writes query that is super non-performant because they assume an index is present when it's not"

More frequently, I see "programmer writes query that is super non-performant because they haven't read the docs, and don't know the requirements for the planner to use the index." A few examples:

* Given a table with columns foo, bar, baz, with an index on (foo, bar), a query with a predicate on `bar` alone is [generally] non-sargeable. Postgres can do this, but it's rare, and unlikely to perform as well as you'd want anyway.

* Indices on columns are unlikely to be used for aggregations like GROUP BY, except in very specific circumstances for MySQL [0] (I'm not sure what limitations Postgres has on this).

* Not knowing that a leading wildcard on a predicate (e.g. `WHERE user_name LIKE '%ara'`) will, except under two circumstances [1], skip using an index.

> despite people intuitively understanding B-Trees or indexes.

You say that, but the sheer number of devs I've talked to who are unaware that UUIDv4 is an abysmally bad choice WRT performance for indices – primary or secondary – says otherwise.

[0]: https://dev.mysql.com/doc/refman/8.4/en/group-by-optimizatio...

[1]: Postgres can create trigram indices, which can search with these, at the expense of the index being quite large. Both MySQL and Postgres can make use of the REVERSE() function to create a reverse index on the column, which can then be used in a query with the username also reversed.

Re:Making Database Systems Usable by rtpg on Wednesday August 21, 2024 @01:48AM (#41307147)
In the universe in which I "know" what index to use (an assumption that can be contested!), you saying "well silly you, the planner is too stupid to figure this out" is not a great defense of the system!

My serious belief is that all the SQL variants are generally great, but I just want this to be incremented with some lower-level language that I can be more explicit with, from time to time. If only because sometimes there are operational needs.

The fact that the best we get with this is planner _hints_ is still to this day surprising to me. Hints! I am in control of the machine, why shouldn't it just listen to me! (and to stop the "but random data analyst could break thing", this is why we have invented permission systems)

Re:Making Database Systems Usable by sgarland on Wednesday August 21, 2024 @08:55AM (#41309803)
> you saying "well silly you, the planner is too stupid to figure this out" is not a great defense of the system!

I don’t think that’s what I said. At least, it wasn’t what I meant.

The planner needs accurate stats on the tables to produce an optimal output. So would you, as a human, if you were writing a query to be executed precisely as written.

It also needs to know information about the system it’s operating in – the amount of RAM it can expect to use being the most important one, but also things like the relative performance of a random disk seek vs. sequential.

The planner also has a huge leg up on most people, in that it is aware of all of the kinds of joins and lookups it can do. Off the top of my head, Postgres can scan in the following ways: sequential scan, parallel sequential scan, index scan, parallel index scan, index-only scan, and bitmap scan. For joins, it can use: nested loop, merge, hash. Do you know when all of those are ideal? What about when you have a query joining multiple tables, keeping in mind that the intermediate contents of a table that has been joined once may look nothing like what it started as?

Re:Making Database Systems Usable by Izkata on Wednesday August 21, 2024 @07:45PM (#41315305)
> Off the top of my head, Postgres can scan in the following ways: sequential scan, parallel sequential scan, index scan, parallel index scan, index-only scan, and bitmap scan.

I don't think that last one is a thing. There is both "bitmap heap scan" and "bitmap index scan" though.

Re:Making Database Systems Usable by sgarland on Thursday August 22, 2024 @08:53AM (#41319718)
It’s the generalized parent of both of those (since you will not see one without the other). You’re technically correct, but I’m also not the only one [0] who uses this term.

[0]: https://www.cybertec-postgresql.com/en/postgresql-indexing-i...

Re:Making Database Systems Usable by Izkata on Wednesday August 21, 2024 @07:43PM (#41315291)
> * Given a table with columns foo, bar, baz, with an index on (foo, bar), a query with a predicate on `bar` alone is [generally] non-sargeable. Postgres can do this, but it's rare, and unlikely to perform as well as you'd want anyway.

The one I see many times more often than this is having one index on (foo) and one index on (bar), then querying on both foo and bar, and assuming the database uses both indexes for best performance. It can be done, even by ancient mysql, but it's most likely going to be a lot slower than expected so they're heavily biased against doing it and will most likely just choose one of the two indexes.

Re:Making Database Systems Usable by iTokio on Tuesday August 20, 2024 @11:17PM (#41306463)
SQL separates query definition from implementation because there is a planning phase between them that can be sometimes quite complex.

To choose the best path to retrieve data, you have to know what are the possible paths (using the underlying data structures, indexes but also different algorithms to filter, join…), but you should also know some data metrics to evaluate if some shortcuts are worth it (a seq scan can be the best choice with a small table..).

And the thing that will trip most humans, is that you need to reevaluate the plan if the underlying assumptions change (data distribution has become something that you never expected).

Note that the planner is also NOT always right, it heavily relies on heuristics and data metrics that can be skewed or not up to date. Some databases allow the use of hints to choose an index or a specific path.

Re:Making Database Systems Usable by rtpg on Wednesday August 21, 2024 @01:44AM (#41307133)
My honest experience in a skilled team has been that people more or less start off thinking "OK, what indices do we need to make this performant", work off of that, and then in the end try to have queries that hit those ones.

I understand the value of full declarative planning with heuristics, but sometimes the query writers do in fact have a better understanding of the data that will go in.

And beyond that, having consistent plans is actually better in some ideologies! Instead of a query suddenly changing tactics in a data- and time-dependent way, having consistent behavior at the planning phase means that you can apply general engineering maintenance tactics. Keep an eye on query perf, improve things that need to be improved... there are still the possibility of hitting absolutely nasty issues, but the fact that every SQL debugging session starts with "well we gotta run EXPLAIN first after the fact" is actually kind of odd!

Re:Making Database Systems Usable by golergka on Wednesday August 21, 2024 @12:37AM (#41306837)
> I think it's still super straightforward to sit down and learn it, but it's really unfortunate that we spend a bunch of time in school learning data structures and then SQL tries really hard to hide all that, making it pretty opaque despite people intuitively understanding B-Trees or indexes.

That's one of the best things about SQL, it's declarative nature. I describe the end result, data I want to receive — not the instructions on how it should be done. There's no control flow, there's no program state, which means that my mental model of it is so much simpler.

Re:Making Database Systems Usable by lucianbr on Wednesday August 21, 2024 @12:51AM (#41306879)
Yes but for performance you need to know how it is done, which defeats the declarative point.

I've read countless articles on how to rearrange the "declaration of what you want" in order to get the database to do it in a fast way.

Re:Making Database Systems Usable by sgarland on Wednesday August 21, 2024 @08:43AM (#41309719)
Sometimes you do, yes. Often times, though, the issue is that the statistics for the table are wrong, or the vacuum (for Postgres) hasn’t been able to finish. Both of these are administrative problems which can be dealt with by reading docs and applying the knowledge.

I think of RDBMS like C: they’re massively capable and performant, but only if you know what you’re doing. They’re also very eager to catch everything on fire if you don’t.

Re:Making Database Systems Usable by paulmd on Wednesday August 21, 2024 @11:09AM (#41311073)
> I've read countless articles on how to rearrange the "declaration of what you want" in order to get the database to do it in a fast way.

While this is doubtlessly true, in many cases the “rearranging” also involves a subtle change in what you are asking the database to do, in ways which allow the database to actually do less work.

SELECT 1 WHERE EXISTS vs WHERE ID IN (SELECT ID FROM mytable WHERE …) is a great example. The former is a much simpler request despite functionally doing the same thing in the common use-cases.

Re:Making Database Systems Usable by sgarland on Wednesday August 21, 2024 @11:54AM (#41311452)
Yes, although both MySQL and Postgres will automatically rewrite the latter to the former, in most circumstances. I still find it good practice to explicitly write the semijoin, to demonstrate the intent.

MySQL, but not Postgres, will also convert the inverse to an antijoin.

As an aside, this is also often a great example to demonstrate why you shouldn’t use SELECT *, since you can’t write a semijoin if you need the predicate column in the result.

Re:Making Database Systems Usable by lucianbr on Thursday August 22, 2024 @03:45AM (#41317768)
I don't see how that is supporting "SQL is declarative and therefore simple". You've kept the declarative part, but it's not simple anymore. You need to know multiple declarative ways to ask for the same thing, and the consequences of each. Might as well just tell the DB how you want to do it.
Re:Making Database Systems Usable by mike_hearn on Wednesday August 21, 2024 @05:39AM (#41308485)
There's an interesting experiment in this direction here:

https://github.com/permazen/permazen

It maps Java objects to a scalable transactional K/V store of your choice, and handles things like indexing, schema migrations and the rest for you. You express query plans by hand using the Java collections and streams framework.

Re:Making Database Systems Usable by flyingsilverfin on Wednesday August 21, 2024 @05:03PM (#41314172)
I wanted to jump in here and say that what we're working on at typedb.com, in our 3.0 version (coming soon in alpha!), is that we're taking our earlier database query language and making it much more Programming-like: functions, errors containing stack traces, more sophisticated type inference, queries as streams/pipelines... I think it's super exciting and has a huge horizon for where it could go by meshing more ideas from PL design :)

Incidentally I think it also addresses what a lot of the comments here are talking about: not learning JOINs, indexing, build-in relation cardinality constraints, etc, but that's a separate point!

Re:Making Database Systems Usable by sgarland on Wednesday August 21, 2024 @06:37PM (#41314872)
> TypeDB models are described by types, defined in a schema as templates for data instances, analogous to classes. Each user-defined type extends one of three root types: entity, relation, and attribute, or a previously user-defined type.

This sounds like an EAV table, which is generally a bad idea.

Re: types, Postgres allows you to define whatever kind of type you’d like. Also re: inheritance, again, Postgres tables can inherit a parent. Not just FK linking, but literally schema inheritance. It’s rare that you’d want to do this, but you can.

In general, my view is that the supposed impedance mismatch is a good thing, and if anything, it forces the dev to think about less complicated ways to model their data. The relational model has stuck around because it’s incredibly good, not because nothing better has come around.

EDIT: this came across as quite harsh, and I’m sorry for the tone. Making a new product is hard. I’m just very jaded about anything trying to replace SQL, because I love it, it’s near-universal, and it hasn’t (nor is likely to) gone anywhere for quite some time.

Re:Making Database Systems Usable by pif on Wednesday August 21, 2024 @06:50AM (#41308944)
> devs by and large don't know SQL, and don't want to learn it

One huge problem is that learning SQL will take you nowhere, unless your schema consists of a few tables with a handful of rows at most.

There is no standard SQL, and there is no serious application development without having to delve into the sanctum sanctorum of your database vendor.

Re:Making Database Systems Usable by sgarland on Wednesday August 21, 2024 @07:14AM (#41309089)
I have worked in massive DBs (tables with billions of rows) in both Postgres and MySQL. I occasionally have to look at docs to figure out if what I want to do is supported by the particular version, but that’s about it.

There is a standard SQL, and vendors are free to add to it. Generally speaking, though, you can get quite far with the standard.

> there is no serious application development without having to delve into the sanctum sanctorum of your database vendor.

OK? I could say the same thing about needing to intimately know the differences in C++ versions. Yes, you should know your tools well.

Re:Making Database Systems Usable by arkh on Wednesday August 21, 2024 @04:58AM (#41308274)
> Unless your schema is really fucked up

So most enterprise schema who have outlived multiple applications. Usually due to time constraint, lack of database administrators and complex business needs. Excel is the king of databases for a reason.

Re:Making Database Systems Usable by cess11 on Wednesday August 21, 2024 @07:38AM (#41309254)
That join you can have your ORM solve for you with some annotations or whatever implying eager loading and so on.

But that's only the trivial case, often you want something more complicated, where the order of clauses or what keywords you pick and how you want the results sorted will affect performance in your specific schema on your specific database engine.

RDBMS management and querying is a rather deep and experience demanding subject once you step outside the trivial cases. You could put more of it in application code, but it will be a lot of chatter and probably worse outcomes in terms of performance than having people on the team that are really good with databases.

Re:Making Database Systems Usable by srcreigh on Wednesday August 21, 2024 @10:45AM (#41310832)
You should learn about some basic database internals. In particular the data structure for indexes, the difference between primary and secondary indexes, join algorithms, page size being 8KiB, etc.

That knowledge gets you to a place to understand that most joins are very slow due to each DB page containing only 1% useful information for the query algorithm.

It will help you see reasons why ppl do things like put an array column in, or use wide tables, etc.

Re:Making Database Systems Usable by pjs_ on Wednesday August 21, 2024 @06:35PM (#41314859)
We use the tool that I wrote on a database with tens of millions of rows. Because the keys are properly indexed, we rarely have performance issues associated with joins. Joins use the index, the Postgres planner is really good, and we can join across heaps of tables with great performance.

We have database performance issues for other reasons (loading of redundant information, too many queries) but not really because of joins.

Re:Making Database Systems Usable by paulmd on Wednesday August 21, 2024 @11:00AM (#41310990)
> Unless your schema is really fucked up, there should only be one or two actually sensible ways to join across multiple tables. Like say I have apples in boxes in houses. I have an apple table, a box table, and a house table. apples have a box_id, boxes have a house_id. Now I want to find all the apples in a house. Do two joins, cool. But literally everyone seems to write this by hand, and it means that crud apps end up with thousands of nearly identical queries which are mostly just boringly spelling out the chain of joins that needs to be applied.

You are primarily describing natural join here. It won’t read your mind and join your tables automatically, but it automatically uses shared columns as join keys and removes the duplicate keys from the result.

The problem with any “auto” solution is going to be things like modified dates, which may exist in multiple tables but aren’t shared.

Even more magic is natural semi join and natural anti join.

Re:Making Database Systems Usable by agent281 on Wednesday August 21, 2024 @11:23AM (#41311205)
Some databases have the concept of a natural join. It joins two tables on common columns. Not quite what I would want. I would prefer something like a key join that uses foreign key relationships. I don't know if any database has that though. (If anybody does, please let me know.)

Oracle: https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqljnaturalj... MySQL: https://dev.mysql.com/doc/refman/8.4/en/join.html

Re:Making Database Systems Usable by pjs_ on Wednesday August 21, 2024 @06:33PM (#41314848)
Natural join is not jargon that I had heard before but unfortunately it does not refer to what I am talking about - I'm talking about something where the computer automatically figures out how to join across more than two tables.
Re:Making Database Systems Usable by yunolearn on Wednesday August 21, 2024 @12:22PM (#41311732)
It's not just databases, it's everything: git, HTTP, even programming language features. The industry collectively decided you don't have to actually _know_ anything about anything anymore. And people wonder why software is so broken...
Re:Making Database Systems Usable by Taikonerd on Wednesday August 21, 2024 @01:58PM (#41312588)
> crud apps end up with thousands of nearly identical queries which are mostly just boringly spelling out the chain of joins that needs to be applied.

I think EdgeDB [0] is on the right track here. It's a database built on Postgres, but with a different query language. So instead of manually fiddling with JOIN statements, traversing linked tables just looks like dot notation:

    select BlogPost.author.email; # get all author emails
[0] https://www.edgedb.com/
Re:Making Database Systems Usable by Izkata on Wednesday August 21, 2024 @02:26PM (#41312826)
Django is a python web framework over 15 years old that's also very similar to what GP wants. For example their apple->box->house query would be something like:

  Apple.objects.get(box__house__owner = 'pjs_')
Still have to specify "box", but the actual foreign key relationships are defined on the models so you don't do the full JOIN statements anywhere.
Re:Making Database Systems Usable by mrgoldenbrown on Thursday August 22, 2024 @10:14AM (#41320536)
Here's one example of a common complication: Let's say your apple example is for an apple trading app. Each house is related to 2 boxes. One is available_for_trade_box_id and another is save_for_eating_box_id. How does your auto join tool know which to use?