Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh materialized views with concurrency

Tags:

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".

like image 475
Volodymyr Zavada Avatar asked Jan 23 '17 10:01

Volodymyr Zavada


People also ask

What is refresh materialized view concurrently?

CONCURRENTLY. Refresh the materialized view without locking out concurrent selects on the materialized view. Without this option a refresh which affects a lot of rows will tend to use fewer resources and complete more quickly, but could block other connections which are trying to read from the materialized view.

How do I optimize a materialized view refresh?

- Use super-fast solid-state disks - The easiest and most reliable way is to speed-up a materialized view refresh is to move the target tables and MV's to SSD. SSD runs several hundred times faster than platter disk, and it plops right in, just a few hours to install.

Can we automate refresh of materialized views?

You can set autorefresh for materialized views using CREATE MATERIALIZED VIEW. You can also use the AUTO REFRESH clause to refresh materialized views automatically.

Do materialized views need to be refreshed?

Unlike indexes, materialized views are not automatically updated with every data change. They must explicitly be refreshed, either on every commit, on a periodically time schedule or – typically in data warehouses – at the end of an ETL job.


1 Answers

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.

like image 137
Laurenz Albe Avatar answered Oct 10 '22 02:10

Laurenz Albe