How to get the CREATE query of an existing materialized view in Hive? - hadoop

I want to extract the entire create query of a materialized view present in Hive. How can I do that? I have the underlying query for data but I want the create query to include storage characteristics, etc.

Two options -
use describe formatted mv_name anad then refer to the 'view original text' line for view SQL.
xx is the name of materialized view
if you are using hue, then you can use table browser to check MV definition/SQL used to create it.
Here is a screenshot on how to get the SQL - xx is the name of materialized view. Create statement below.
CREATE MATERIALIZED VIEW xx AS SELECT * FROM activity;
2.1 Click on 'i' and then click on 'Table Browser'.
2.2 Click on 'view sql' to see view query.

Related

Why we can't make materialized view on top of another view in clickhouse?

A is a collapsingMergeTree engine table
CREATE VIEW A AS SELECT * FROM A final;
CREATE MATERIALIZED VIEW a_mview1 TO B
AS select id,
name
from A;
This is not working it seems we can't make mview on view..but why?
Normal View doesn't store any data (see doc) so that it is wrong to use it as a source of data for Materialized View.
It needs to create Materialized View based on the origin table:
CREATE TABLE A (
..
) ENGINE = CollapsingMergeTree
.. ;
CREATE MATERIALIZED VIEW a_mview1 TO B
AS
SELECT ..
FROM A
.. ;
Look at the article ClickHouse Materialized Views Illuminated for details.
CREATE VIEW A AS SELECT * FROM A final;
It's impossible. Because MV never reads a source table. MV gets inserted blocks from INSERT command.

Redefine materialized view with no downtime

I have a materialized view that I need to redefine the SQL for. We have an external system that hits the view over a db link, and the monster view takes 5 minutes to refresh the data in the view. The only way I know how to redefine the SQL for a view is to drop it and recreate it, but it would be very bad if the external system couldn't find the table, or it didn't have a complete data set. I need to have as little downtime as possible.
Is there any way to do this natively or more elegantly than:
Create public synonym for materialized view and make everything that uses the view use the synonym instead.
Create new materialized view with new SQL
Change the synonym to point to the new view
Drop the old view.
I've got code to do this dynamically but it is getting really ugly. It seems like there should be a better way to handle this.
Oracle has a build in solution for that. Keep in mind that the mview declaration is separate from that of the table.
The original mview
create materialized view mv1 as select dept , count(*) as cnt from scott.emp;
we want to change the declaration so that only dept over 5 will be calculated
drop materialized view mv1 preserve table;
notice the PRESERVE TABLE clause - the table mv1 is not droped - only the mview layer.
desc mv1
now we create the mview with a different query on top of the existing table
create materialized view mv1 on prebuilt table as
select dept , count(*) as cnt from scott.emp where dept > 5;
notice the on prebuilt table clause. the mview is using the existing object.
exec dbms_mview.refresh_mview('mv1');

How to dump individual view statements in MySQL

I want to be able to dump individual view statements/definitions from a MySQL database (version 5.5.28) so I can edit them and insert them into a new database. I've been trying to do it with MySQL Workbench (version 5.2.44 on OSX) but it dumps the views as 'CREATE Table' statements without the view logic.
I'm looking for something to analyze a db, give an options for views to dump, and dumps 'CREATE VIEW...' statements to a file to re-create those views.
MySQL has a SHOW CREATE VIEW statement that should do what you need.
Example usage, assuming you view's name is sampleView:
SHOW CREATE VIEW sampleView;
As a note, your user will need the SHOW VIEW and the SELECT privileges to be able to use the SHOW CREATE VIEW command.
An alternative, you can also get this information from the information_schema.VIEWS table.
Using the sampleView name again, the query would look something like:
SELECT
VIEW_DEFINITION
FROM
INFORMATION_SCHEMA.VIEWS
WHERE
TABLE_NAME = 'sampleView';

Oracle: how to create a fast refresh materialized view that extracts data from XMLType?

I have a table xml_documents with two columns: a document_id column (primary key) and an xml column with some XML data, which is a schema-less XMLType. I can create a materialized view with just the document_id with:
create materialized view mv refresh fast on commit as
select document_id
from xml_documents
This works fine, but isn't very useful. As you might expect, I'd like the materialized view to extract data from the XML, and for this I use extractValue(). I am trying the following:
create materialized view mv refresh fast on commit as
select document_id, extractValue(xml, '/my/gaga') gaga
from xml_documents
This fails with:
ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view
How should I go about to create a fast refresh on commit materialized view that extract values from XML?
Your XMLType is (probably) stored as a CLOB. Find the hidden column with a query like this:
select * from user_tab_cols where table_name = 'XML_DOCUMENTS';
Then create a function to convert a CLOB into an XMLType, and extract the value. Note that the "deterministic" keyword is necessary, although
I'm not sure why. Passing data back and forth between SQL and PL/SQL will be slow, but if you're using a materialized view things are probably
already slow.
create or replace function extract_from_clob(p_xml in clob) return varchar2 deterministic
is
begin
return XMLType(p_xml).extract('/my/gaga/text()').getStringVal();
end;
/
Then drop and create your materialized view with the system column passed into the function:
create materialized view mv refresh fast on commit as
select document_id, extract_from_clob(SYS_NC00003$) gaga
from xml_documents;
I'm unsure about using a system-generated hidden column. It works, but doesn't seem like a really good idea. At the very least it will make it
difficult to create the object on different systems - you'll need to find the new column name each time.
It seems weird that XMLTypes don't work when LOBs work fine. I can't find any documentation about this; I'm not sure if it's a bug, an unimplemented feature, or if there's some magic setting that will make it work. If no one else can provide a better answer, it might be worth checking with Oracle support before you use the above approach.

changes in Materialized

I have one Materialized view on one server which is created by DB link.
There is one job running on that Mview. (created with dbms_refresh.make earlier).
Now I have created 3 new fields in original table.
My queries are.
1) Do I need to drop and create Mview again, if yes, do i need to create Mview log on main server again
2) What happens to job running on Mview , do i need to create it agin?
Also there are views created on Mview ,so
--If i run create or replace view query , will it create any problem?
Please guide.
Thanks!
If you need to include the new columns in your materialized view then yes you need to re-create the materialized view. You must explicitly drop the view as there is no "create or replace materialized view" statement.
DROP MATERIALIZED VIEW blah;
CREATE MATERIALIZED VIEW blah...
Dropping/recreating the materialized view should re-create the refresh job. Not 100% certain, but you should probably recreate the log as well.
And, if you don't need to include the new columns in your view, you really don't need to do anything...
After dropping/creating the materialized view, you should recompile the other views afterwards, because they may have become invalid.
You can check if that happened with
select *
from user_objects
where status = 'INVALID';
Recompile a view can be done with
alter view the_view compile;
or
exec dbms_utility.compile_schema(user);
This simply recompiles everything in your schema. Be sure to have no running jobs while doing this!

Resources