Refresh materialized views with concurrency

时间秒杀一切 提交于 2019-12-18 15:56:03

问题


I have a PostgreSQL DB, where I use materialized views. The problem occurs when I try to refresh these materialized views.

REFRESH MATERIALIZED VIEW product_cat_mview;
REFRESH MATERIALIZED VIEW productsforproject;

My solution is, when the user want to see updated data, he should click a "refresh button" on the web page, but this takes about 50s (on a local connection and about 2 minutes from the application server) and all this time the user has to wait, which is not good.

Now I should create a solution to automatically refresh these materialized views every 10 minutes. I have created a Java solution with multithreading. But I have one problem.

The first query

REFRESH MATERIALIZED VIEW CONCURRENTLY product_cat_mview;

works correct, but the second

REFRESH MATERIALIZED VIEW CONCURRENTLY productsforproject;

complains that I need to create a unique index. I tried create index, unique index etc. that I found in google, but I still get the message to "Create unique index".


回答1:


You will have to create a unique index on the materialized view itself.

This would look like this:

CREATE UNIQUE INDEX ON productsforproject (id);

Replace id with a suitable unique key column or a (comma separated) combination of such columns.



来源:https://stackoverflow.com/questions/41803781/refresh-materialized-views-with-concurrency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!