by crazygringo · 2y ago · 0 comments
If I understand it correctly, for a query with joins, then instead of returning a single combined set of results with the information you need, it returns multiple sets of results based on each separate table.
But I don't understand why that would ever be desirable.
I always write my queries to return the information I need, per whatever "key" I need, in each row. Splitting that up in multiple sets of results is not helpful.
Or if I really need multiple sets of data, I write multiple queries.
So I really don't get what this is for. It's like it takes one of the biggest features of databases -- joins -- and removes it? Why?
Then the authors show ways to use multiple queries (in section 3, "SQL-BASED REWRITE METHODS"). Each method has its drawbacks. If you've done enough SQL, these patterns will look familiar to you. Compare the syntax needed here vs the proposed "SELECT RESULTDB" syntax in Listing 3 and you should see that their proposal is easier to write. Then in section 4, the authors "present an algorithm that can be integrated into a DBMS to efficiently compute the result set of our SELECT RESULTDB queries." Presumably this would be more efficient than any of the alternatives from section 3.
Reading it closely, this is what I already disagree with. You do a good job of summarizing their two main arguments, so allow me to rebut:
> Either the result set will be larger than it needs to be
Yes, they're using the example of repeating information (denormalization), such as multiple courses taught by the same professor. But it's genuinely hard to see this as a drawback -- that's a feature. Data should be stored as normalized as possible, but queries are supposed to denormalize to present information in the desired format.
(And if you're dealing with what would be an overly-large amount of repeated values, you just run multiple queries yourself instead of one. And if round-trip latency is some kind of issue with running queries sequentially, you can always issue queries in parallel instead.)
> which "discard[s] all schema and relational information on the way."
Again, this is a feature. You're not supposed to retrieve all possible relational information in query results. You write your query to retrieve and differentiate precisely what you need and no more. Discarding irrelevant information is a feature, not a bug. More than that -- you want your query to define and adhere to its output format regardless of the underlying database structure, precisely so you can refactor things in the database and rewrite the query but not need to rewrite the code that uses the query results.
I guess my overall bafflement is that the things they describe as "not ideal" seem to me like features rather than problems, and these features have been highly beneficial in my practical experience of writing a lot of database-driven apps.
but queries are supposed to denormalize
Well, that's how they've always worked, yes.Might there be a better way for some use cases?
denormalize to present information in the desired format
What if my "desired format" is a structure that maintains the relationships specified in the database?This is an incredibly common pattern, particularly with ORMs.
# psuedocode
for each recipe in Recipe.joins(Ingredient).select_all() do
for each ingredient in recipe.ingredients do
print(f"{recipe.name} requires {ingredient.name}")
Depending on the ORM and the query options specified, this results in one of the following:1. Everything getting denormalized into a single, wasteful result table and then teased back out into Recipes and Ingredients by the ORM
2. Two SQL queries. One to fetch the recipes, and then one to fetch the ingredients once we know the id's of the recipies.
3. A big ol' N+1 situation with 1 query to fetch recipes and then N additional queries to fetch the ingredients.
They all have various inefficiencies. What if we just returned the relational structures directly?
But the desired format usually isn't a square, flat table. Usually the data has structure and you want to preserve that structure when you're displaying, not just having it hidden in the database. E.g. you might want to show a heading for each professor and then a list of classes taught by that professor. Getting back a flat table with n duplicate copies of the professor's information is a step backwards.
The desired format depends on the application.
At this point a substantial portion of all SQL queries are generated by (and the results consumed by) ORMs.
For that use case having results that include Products and Categories separately (so you can instantiate Product and Category objects) is more useful than a single table.
Honestly, it sounds like they haven't really grasped the purpose of relational databases.
That seems so unlikely that I'd think a distributed sqlite database would be a better idea instead, and just have it sync up when it can, and always query local.
It is a bit annoying to do this though. It would be nice if it was done for me automatically be some db driver.
> And if round-trip latency is some kind of issue with running queries sequentially, you can always issue queries in parallel instead.
Some languages or frameworks don’t provide great parallel ization features though.
I guess at the end of the day if it’s an extension to SQL that you can optionally use, then I would have some situations where I would use it.
Similarly keeping schema information gives the ORM more to work with I suppose? Helpful for typing, foreign key links where it's not necessarily the same original table structure, but in the query result professor_id is still ultimately a foreign key to professor table, etc.
Nit: data should be stored as normalized as possible, if your use-case prioritizes fast writes over fast reads. There are other benefits to normalization, but in practice they pale in comparison to the performance shift.
It does not have a `GROUP BY` functionality!
What `sql` calls `GROUP BY` is `SUMMARIZE`.
I’m familiar with SQL, but not the artificial limitations that you’re referring to, and I’d like to better understand.
I think the authors' work is interesting but they shouldn't have said 'relational database' in the paper, just 'sql database' instead ("We keep an SQL database storing information about professors ..." and so on).
In "industry", I don't argue with people saying Oracle/MySQL/PostgreSQL are relational databases because that would make me an insufferable colleague and would hardly add any value to the discussion, but for an academic paper on databases, I would prefer more accurate language.
I’m in industry, so I’ve only ever heard these databases described as relational databases. I’d love to understand more.
It's something lots of apps need all the time.
For an example see: https://shusson.info/post/building-nested-json-objects-with-...
There are 8 problems listed there that will help you understand the probablement and the elegance of this proposed solution via the SQL standard.
eg, select P.name, C.* from Parent P inner join Child C on C.Parent_id = P.Id where P.Id = 1234
so from what I can tell is this idea would have a result set of a Parent table and child tables based on the join and projection with no duplication? At least that's what I understand it to be. That seems useful.
Let's say you're querying orders, and each order has a customer name in another table that you want to pull through. If you do a join and include the customer name in the result set it's going to amount to a lot of repeated data that has to be sent across the network. Instead, you could receive two result sets, one with only the relevant customers and the other with the orders containing only the customer ID. The amount of data getting sent across the network will be massively reduced and you can still reference the customer names at the other end.
Not really a new idea, just new to SQL databases. Usually if you find yourself needing to dump a whole ton of data, you don't use SQL, you use any number of other database solutions that are better suited to that model. So I suppose this is really meant to give you a little bit of that for when you need it.
For most queries, a single table makes perfect sense. But in the real world things aren't usually so neat.
The list of patients remains distinct but which of the conditions they had would still be referencable.
It's a nice shortcut but not world changing.
[0] https://www.cyberciti.biz/faq/understanding-bash-fork-bomb/
Chris Date does discuss the concept of nested tuples / relation-valued tuples of some kind in his books as an extension to the relational model. But that's something else entirely.
Multiple ways in fact. Both unions and joins.
You can pack multiple relations into one relation using unions/joins and then unpack it again in the client to ultimately achieve the same effect, which is a workaround that some utilize, but that is really hacky (to be clear, unions and joins are not hacks when used for their intended purpose, but the data packing is).
The proposed would be much less hacky - although still far from an ideal database that has no latency constraints that force deviation from what is "pure". But that’s engineering for you.