简体   繁体   中英

performance of refreshing postgres materialized view

I am exploring materialized views to create de-normalized view to avoid joining multiple tables for read performance. APIs will read from the materialized views to provide data to clients.

I am using amazon aurora postgres (version 11).

I am using a unique index on the materialized view (MV) so that I can use the “refresh concurrently” option.

What I am noticing though is that when only a fraction of the rows get updated in one of the source tables and I try to refresh the view, it's pretty slow. In fact slower than populating the view for the first time. eg: to populate MV first time takes ~30 mins, refresh is taking more than an hour. less than 1% of rows have been updated. The main three tables involved in generating the MV have ~18 million, 27 million & 40 million rows.

The timeliness of the materialized view refresh is important so that data is not stale for too long.

I could go with custom tables to store the denormalized data instead of materialized views but would have to implement logic to refresh data. So planning to avoid that if possible.

Is there anything that can be done to speed up the refresh process of the materialized views?

Please let me know if you need more details.

thanks Kiran

Refreshing a materialized view is slow even if little has changed, because every time the view is refreshed, the defining query is run.

Using CONCURRENTLY makes the operation even slower, because it is not a wholesale replacement of the materialized view contents, but modification of the existing data.

Perhaps you could create a denormalized table that is updated by a trigger whenever the underlying tables are modified.

You can create a second materialised view and update it (not concurrently), and then swap the names.to the tables in a transaction.

I actually have no idea why postgres didn't implement CONCURRENTLY this way.

Maybe I'm a bit late to the party, but if you would be on postgres 13 or later you could try this extension: https://github.com/sraoss/pg_ivm

They have some limitations but promise much faster rebuilt times.

Here's a bit more on it from pganalyze: https://pganalyze.com/blog/5mins-postgres-15-beta1-incremental-materialized-views-pg-ivm (in contrary to project authors, they want you to be on v14 or later)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM