Emulating materialized views in PostgreSQL with concurrent refreshes - sql

I'm using PostgreSQL 9.2.4 and would like to emulate a materialized view. Are there any well-known methods for doing this, including concurrent refreshes?

PostgreSQL wiki - materialized views links to two trigger-based implementations.
The general idea is to put AFTER INSERT OR UPDATE OR DELETE ... FOR EACH ROW triggers on each involved table that do partial updates on the target table. Implementation is fairly specific to the nature of the view.
For some more complex views you can't really do partial updates and need to do a concurrent view refresh instead. That usually involves creating a new table, populating it, committing, beginning a new transaction, dropping the old table, renaming the new one to the name of the old one, and committing again.

Starting from 9.5, Postgres supports Concurrent Refresh as stated here in the official documentation. However, there are two preconditions that needs to be satisfied to do so:
You must create an unique index on the materialized view
The unique index must include all the records of the materialized view. In other words you cannot have a WHERE clause in your create index command.
The command to refresh the materialized concurrently view is following:
REFRESH MATERIALIZED VIEW CONCURRENTLY *mat_view_name*;
Note that refreshing the materialized view concurrently is relatively slower than the normal refresh. However, it will make sure that none of your queries on the materialized view is blocked during the concurrent refresh.

Related

How to refresh table data efficient way

I have few analytics table which gets refreshed in every few days. By refresh I mean there could be some new records, some records needs to be deleted and some records needs to be updated and there is no specific identifier.
So there are below options in my mind:
For every refresh truncate the whole table and reload data. But if any failure occur during fresh data load then table data will be corrupted and all analytics will show wrong data.
Another option is to keep a refresh id in all analytics table, and while reading data from analytics table use latest refresh id. But with this approach main issue is joining and filtering. We have joining across analytics table so each and every join should join with refresh is always otherwise fetched data will be wrong, and this approach is error-prone.
Can We create a view on these table which will have dynamic filter ? While querying on these views I will use latest refresh id as a filter.
Is there any better approach to refresh data into analytics tables keeping in mind that it should handle any error scenario and not error prone.
Or, the option that I often use:
Create a new version of the table in an alternative location.
Validate the results.
Swap the live table for the new version.
The "swap" might involve renaming tables or truncating and loading the original table. Often, the original contents are saved somewhere else.
This approach is handy particularly when the logic for creating the entire table is complicated to express as incremental changes. It also minimizes the amount of downtime, when the table is not available.
You want incremental changes when you need more up-to-date data and batches don't work -- either because of timing, size, or cost. Many databases support materialized views or replication which simplify this process.

cassandra : send onse store multiple times

I need populate 4 different tables in cassandra, by absolutely same data.
I mean, I have N fields, each has some value, but I need store them in 4 different tables (with different PK definitions) to allow different selects.
For that I need trigger 4 inserts.
4 times more network overhead and work for both cassandra and data producer.
Is there way in cassandra to send 1-ce and save in N different tables?
I need some optimization, but batches are not looking appropriate for that.
Please, help!!
Cassandra triggers could also work for you. You create a trigger Java class and deploy it in a Jar to each node. When it intercepts your main table insert, it also tacks on writes to the others.
If you don't want to use batch inserts, and you have the chance to use Cassandra 3.x you can use a new feature Materialized Views.
With materialized views you can basically do exactly what you asked for.
You will have to create one main table where you do all your inserts on. Then you would have to create 3 materialized views.
When you insert data into your main table, all your views will be updated by cassandra. Internally cassandra will also do those inserts, so the traffic overhead is still there (only starting on the coordinator and not on your client)
Note that those new features are not for free. As I said, there will be overhead traffic and work to be done by the coordinator. Currently there are only simple select statements possible, more complex queries are not working right now.
The provided link gives more insights of how these views work, so please have an additional look there.

Materialized View: How to automatically refresh it upon table data changes?

Is there a way in Oracle Materialized views so that it automatically refresh itself when there are changes on the tables used in the materialized view? What is the Refresh Mode and Refresh Method that I should use? What options should I use using Sql Developer?
Thank you in advance
Yes, you can define a Materialized View with ON COMMIT, e.g.:
CREATE MATERIALIZED VIEW sales_mv
BUILD IMMEDIATE
REFRESH FAST ON COMMIT
AS SELECT t.calendar_year, p.prod_id ... FROM ...
In this case after every commit the MV is refreshed, provided the last transaction was done on master table, of course.
Since refresh is done after each commit it is strongly recommendd to use FAST REFRESH, rather than COMPLETE this would last too long.
You have several restrictions and pre-conditions in order to use FAST REFRESH, check Oracle documentation: CREATE MATERIALIZED VIEW, FAST Clause for details.
I don't think there's any way to 'automatically' replicate the changes to the m.view right after they are made. But there are ways to use FAST (incremental) refresh on demand, you'd only have to schedule a job for the m.view or and m.view group to do the refresh. You can also use m.view log to keep track of all the dml and the have it propagated to the m.view with a fast refresh on a remote database through the db link.
If you need the changes to be replicated as soon as they are made, then I recommend using golden gate or streams (if you don't want do license GG). Just beware that oracle discontinued support for streams in favor of Golden Gate, so if you have any issues, you're on your own. But anyway, it's a pretty solid replication tool, once you get the hang of it.

materialized view logging exclude deletes

I am using MVIEWs with Fast refresh to replicate some tables across a network. Everything works great, however I ran into an issue when considering my Delete/Purge process.
The source for the MVIEWs that are feeding the log tables have a data retention of 7 days. Ie I will be running a nightly purge process to delete data older than 7 days from current date.
The target MVIEWs however are on an ODS and have a data retention policy of 30 days. Also, these MVIEWs are NOT currently populating another schema or set of tables.
Problem is, when I Delete from the source tables, those delete statements will propagate through to the target MVIEWs and now I no longer have 30 days worth of data - only 7.
Is there a way to exclude logging DELETE for the MVIEW log tables? I noticed in the MLOG$_Table_Name there is a column 'DMLTYPE$$'. Could I somehow delete from the Log table all records where DMLTYPE$$ = 'D'?
Thanks everyone, and yes, I did try researching this online first.
Regards,
Steve
I suppose that you could manually delete data from the materialized view logs before running the refresh. That would probably work. But it would not be a solution that I'd be really comfortable with. It would be a very bespoke solution that would probably not be officially supported. And it if there might ever be another materialized view that depends on the materialized view log, you'd have to ensure that you're only deleting those rows that relate to your materialized view's subscription. Plus, the materialized view on the destination would need to be updatable in order for you to be able to manually remove the rows older than 30 days via a separate process.
If these are the business requirements, something like Oracle Streams (or GoldenGate) would be a much more appropriate architectural solution. Those products are designed to give you more flexibility about which logical change records (LCRs) you apply. In Streams, for example, it is easy enough to create a custom apply handler that discards delete LCRs. And since you're applying LCRs to a table on the destination rather than a materialized view, your 30 day purge process is much easier to manage. This would be a relatively common Streams setup rather than a very unique materialized view setup.

Life cycle of an Oracle Materialized view

I am looking for the life cycle of an Oracle materialized view. For example the statement:
Create materialized view foo
Refresh On Commit
...
Will this view be updated every time there is a commit to my database, or just one of the tables referenced in the view statement? Also beyond this at what point does Oracle destroy the old cache and replace it with the new one? Specifically what is the window of "staleness" for a materialized view? Meaning is it dependent on how long it takes to create the materialized view.
The ON COMMIT clause will modify the commit process of all transactions that issue DML on a base table:
Specify ON COMMIT to indicate that a fast refresh is to occur whenever the database commits a transaction that operates on a master table of the materialized view. This clause may increase the time taken to complete the commit, because the database performs the refresh operation as part of the commit process.
The commit will be dependent upon the success of the refresh of the materialized view (which means that a commit can fail because a dependent MV can't be refreshed).
The refresh takes place in the same transaction as the one that issues the commit. This means that as soon as the commit is complete, the changes are visible to all sessions (data is thus never stale).
Some of the things you have to be aware of:
The use of on-commit MVs has a performance cost: materialized view logs (adds DML "triggers" to the base table) increase the work on DML and obviously the commit will perform more work than usual. Benchmark your workload to make sure the extra work won't be a burden.
In aggregate on-commit MV, concurrent transactions can update the same MV row, which can lead to some contention during the commit on top of the extra work.
Some tools don't expect a commit to fail, this can lead to some UI problems (usually old client-server apps).