News for hackers. Stuff that matters.
Re:For Want of a JOIN by asalahli on Thursday December 22, 2022 @12:26PM (#34095894)
Also, caching can be done with materialized views
Re:For Want of a JOIN by jasonhansel on Thursday December 22, 2022 @12:32PM (#34095969)
IIRC in Postgres you need to refresh an entire materialized view all at once, effectively recreating the entire table; you can't just have it update incrementally whenever the underlying data changes.

I think SQL Server can do this, but...then you have to use SQL Server.

Re:For Want of a JOIN by mnahkies on Thursday December 22, 2022 @01:15PM (#34096494)
I think it's being worked on for postgres but probably another major release or two away, quick Google came up with this https://pgconf.ru/en/2021/288667 but I'm sure I've come across discussion in postgres mailing lists / wiki in the past.

Would definitely be a nice feature to have, without it I find the main use case I have for materialised views is batch processing where you want to prepare a complex result set and then stream process it in a Cronjob or similar

Re:For Want of a JOIN by aidos on Thursday December 22, 2022 @01:55PM (#34096980)
There’s also info on this page about it (incremental materialised views).

https://wiki.postgresql.org/wiki/Incremental_View_Maintenanc...

In the meantime my preferred technique is to have a column where I stamp the last generation time for each row and then I rebuild anything that’s changed since then (assuming all your source data has some sort of last updated stamp).

Re:For Want of a JOIN by branko_d on Thursday December 22, 2022 @02:24PM (#34097346)
> I think SQL Server can do this, but...then you have to use SQL Server.

Yes, SQL Server can update indexed views incrementally, but there are severe limitations:

https://learn.microsoft.com/en-us/sql/relational-databases/v...

If memory servers, indexed views have been in SQL Server for 20-odd years, and haven't seen meaningful improvements in all that time. We still can't do a LEFT JOIN, or join the same table more than once or MAX etc...

The same story with T-SQL, which is firmly stuck in the '80s (not that other databases are better).

There are some extremely powerful features in SQL Server that can be used effectively with some pain, but they could be so much better if Microsoft invested in fully fleshing-out their potential instead of chasing the latest buzzword.

Sorry for the rant.

Re:For Want of a JOIN by tshaddox on Thursday December 22, 2022 @03:44PM (#34098393)
Yep, which is why materialized views don't tend to work great for a lot of tasks where they initially seem to be the most natural implementation, particularly almost any view that's a feed or aggregate of data in the system over all time. It's so easy to just play with a simple SQL select query until you get it working, then throw it into a materialized view. It'll probably even work for a long time! But as soon as the data in the system grows and that refresh starts getting slower, you're stuck with a (potentially tricky or at least frustrating) migration to another implementation (maybe something like event sourcing).
Re:For Want of a JOIN by cwillu on Thursday December 22, 2022 @01:58PM (#34097006)
Also computed columns in a covering index.