5 ms·
I think this solves the problem of Cartesian explosions in ORMs. The requirement of related data needing to be shoved into a single table means data shared by m
by klysm 3y ago
I think this solves the problem of Cartesian explosions in ORMs. The requirement of related data needing to be shoved into a single table means data shared by many entities has to be duplicated. The result set being split into distinct tables removes this constraint.
The only way I know how to deal with this in Postgres while maintaining a single query is to lateral join into a json aggregate. However, I don’t think the tooling around that approach is mature enough yet.
- programmarchy 3y agoI’ve been doing this. Curious what you mean by tooling. All the pg client libraries I’ve used support json/jsonb return types, for example.
- eddd-ddde 3y agoI love postgres json aggregates, but since postgres cant do nested aggregates it is kinda limiting when I would like to build my whole query response in a single postgres query
- twojacobtwo 3y agoI'm new to web-dev (and databases), so my apologies for the potentially silly question, but what's an example situation where you would use nested aggregates and what kind of effort would it save, if it were possible?
- 5636588 3y agoWas curious myself so asked chatgpt and this what I got: Nested aggregates in databases are useful when you want to perform calculations or summaries on groups of data within larger groups. For example, imagine you have a database of sales transactions with products and categories. You could use nested aggregates to find the total sales for each category and then calculate the average sales per category. It saves effort by allowing you to perform calculations at different levels of granularity in a single query.
- sargstuff 3y agogoal directed programming is the related non-sql programming paradigm. unicorn/icon[1] ---- Use cases:[2] autoparts inventory (book keeping page statics of sales/sales person, parts, inventory on hand, etc) internet statistics for through put on all interfaces on group of servers [0] --- topic related books: Joe Celko has book series 'SQL for Smarties' that covers basic through advanced aggrecation with sql using example queries. Joe Celko's 'Trees and Hierarchies in SQL for Smarties' covers some of the ideas related to 'SQL statment returned a database'? --- [0]: https://dba.stackexchange.com/questions/173044/nested-aggregate-functions-over-different-dimensions https://dba.stackexchange.com/questions/173044/nested-aggreg... [1] : http://unicon.org/ http://unicon.org/ [2] : https://cs.uwaterloo.ca/~tozsu/courses/CS338/lectures/6%20Nested%20SQL.pdf https://cs.uwaterloo.ca/~tozsu/courses/CS338/lectures/6%20Ne...
- eddd-ddde 3y agoPersonally, I prefer raw sql over something like an ORM. So many times I want to get all users, with all posts, with all post comments (example). You can do an array aggregate to get all users and their posts in the array, with build_jsonb_object you can make the posts an object of say id and content. But then you can't really make another array aggregate per post.
- sargstuff 3y agoCan fake it with postgress copy/execute[0], where each aggregate block level is a transaction. [0] : https://medium.com/r3d-buck3t/command-execution-with-postgresql-copy-command-a79aef9c2767 https://medium.com/r3d-buck3t/command-execution-with-postgre...
- sargstuff 3y agoshell out for nested aggregates[0]. one 'sql querry' to run a batch shell containing all multiple aggregate levels in a file, import back in to return table reference. [0] : https://medium.com/r3d-buck3t/command-execution-with-postgresql-copy-command-a79aef9c2767 https://medium.com/r3d-buck3t/command-execution-with-postgre...
- klysm 3y agoYou can do nested aggregates though unless I’m misunderstanding what you mean by nested aggregate