Is it possible to create a materialized view on top of Stage (loaded CSV files) or alternate to create MV on direct files without a table?
example:
create or replace materialized view mv_ext_v1 as
select metadata$filename, metadata$file_row_number, $1, $2 from #newstage order by 1;
error:
Materialized view not supported over a stage.
Thanks.
You can't define a MATERIALIZED VIEW over a staged file.
But you can define Materialized Views over External Tables, which are the same, but in an extra wrapping.
No, it is not possible. As stated in the Snowflake docs a materialized view is limited to query only a single table and no other objects.
Wonder if you have looked at materialized view over external table (which are just files on stage also):
https://docs.snowflake.net/manuals/user-guide/tables-external-intro.html#materialized-views-over-external-tables
Related
I'm interested in getting the structure of each table in my DB.
Currently I'm using: DESCRIBE TABLE table1.
However, this means I have to do a separate query for each table. Was wondering whether there is a query I can get the structure of several tables at once (and therefore saving me some queries)?
Thanks,
Nir.
You can use Account Usage/Information Schema view COLUMNS
https://docs.snowflake.com/en/sql-reference/account-usage/columns.html
Following article have a slight difference example of using COLUMNS view to create a select statement but it should give you an idea
https://community.snowflake.com/s/article/Select-columns-based-on-condition-in-Snowflake-using-Information-Schema-and-Stored-Procedure
You have a couple options:
you can use the COLUMNS view in the information schema
https://docs.snowflake.com/en/sql-reference/info-schema/columns.html
Note: The view only displays objects for which the current role for the session has been granted access privileges.
you can use the COLUMNS view in the account_usage share schema:
https://docs.snowflake.com/en/sql-reference/account-usage/columns.html
Note: this will show all the columns in all tables, will show deleted objects and such as well.
Also note, there is a delay in the data (latency could be as much as 90 minutes, typically isn't though)
I have materialized views in Snowflake that is not refreshing. Below is a basic example of what I'm doing.
--Create table and insert two records
CREATE OR REPLACE TABLE T1 (ID INTEGER);
INSERT INTO T1 VALUES (1);
INSERT INTO T1 VALUES (2);
--Create materialized view on table
CREATE OR REPLACE MATERIALIZED VIEW VW_T1 AS SELECT ID AS AVG_ID FROM T1;
--Insert two more records after creating the materialized view
INSERT INTO T1 VALUES (3);
INSERT INTO T1 VALUES (4);
-- Show metadata
SHOW MATERIALIZED VIEWS LIKE '%T1';
No matter how long I wait, the view does not seem to be updating. The row count is always 2. Behind_by always has a value.
What am i doing wrong. I have followed the troubleshooting in the Snowflake documentation, but no success. https://docs.snowflake.com/en/user-guide/views-materialized.html#troubleshooting
Marius
This is expected behaviour. Snowflake materialized views are different than materliazed views on other databases. Two important points:
1) Materialized views are automatically and transparently maintained by Snowflake.
2) Materialized views provide always current data. If a query is run before the materialized view is up-to-date, Snowflake either updates the materialized view or uses the up-to-date portions of the materialized view and retrieves any required newer data from the base table.
So you do not need to worry about the updates. It will be updated in the background time to time (based on some criteria such as DML size, DML count, time). You can see when it's updated if you check the "refreshed_on" column on the output of SHOW command.
---------- Extra info --------------
MV keeps the data on its own data files. The SHOW command shows "when the data is refreshed", "how many rows it contains" etc... Marius saw 2 rows, because the MV had 2 rows at that point. When Marius add more rows to the source table, MV will not copy them immediately. There are some thresholds, but if you try to read from MV, MV will read the delta from source table, and provide current data all the time. The users do not need to worry about the "behind_by", "refreshed_on" or "number of rows" (unless the lag is several days).
In summary, SHOW command and MV seem working as expected.
In sql server we can Update data view.I think the concept of view is a read only table.
Why we can edit view in sql.is there possible in oracle?
To answer your question of why can we create an editable view, it is so that you can limit access to fields that you do not want updated (or viewed). Then you can give a user access to the view, but not to the underlying tables
For a simple example, you could have a personnel table. You could create an view allowing some users to update a field like emergency contact details, but not see or update bank details or salary
There are lots of criteria to meet to make a view updatable, and you can indeed use INSTEAD OF triggers for extended functionality http://msdn.microsoft.com/en-us/library/ms187956.aspx
I think the concept of view is a read only table
No, it's more of a virtual table - anywhere you have a real table, you ought to be able to replace it with a view, and the users should be none the wiser.
According to Codd:
Rule 6: The view updating rule:
All views that are theoretically updatable must be updatable by the system.
However, in practicality, this ideal has not been achievable.
In addition to what #JamieA wrote, views can not only limit access to fields, but also limit access to data in the table.
Look at simple SQL-Fiddle example and experiment with it.
The view in the example restrict access only to columns id,val1 of the table, but also restrics access to rows (only id = 2..10). You can update and delete only rows 2..10 throught the view.
However the view does not prevent insertion of a row with id = 20
Here is another example - a view with check option - it this case the view prevents not only deletes and updates, but prevent also inserting rows that do not match a where clause of the view.
#yogi wrote that we can't update a view if the view joins two tables -> here is a third demo that shows a simple view that joins two tables, and how an update of this view works.
These simple examples are for Oracle, but after small modifications should also work in MS-SQL (must change datatypes in create tables), since when i looked througs MSDN documentation (section: updatable views -> http://msdn.microsoft.com/en-us/library/ms187956.aspx), I didn't find any significant differences between ms-sql and oracle, it seems that views work similary on both databases.
Yes it is possible in Oracle, the other answers already explained why views are updatable and had shed some light on that question, they are also allowed in Oracle but have some restrictions/limitations here is the Oracle documentation
Like, the view select cant have: aggregate functions, distinct clause, group by... read the link for more info
Since views are read only tables and its doesn't support DML statements you can't perform update on view.
An interesting factor is there you can write update statemnt over view and write a instead of trigger for that hence you can perform multiple update statements on tables which are in the view.
According to Pinal Dev Views having following limitations
ORDER BY Does Not Work.
Adding Column is Expensive by Joining Table Outside View
Index Created on View not Used Often
SELECT * and Adding Column Issue in View
COUNT(*) Not Allowed but COUNT_BIG(*) Allowed
UNION Not Allowed but OR Allowed in Index View
Cross Database Queries Not Allowed in Indexed View
Outer Join Not Allowed in Indexed Views
SELF JOIN Not Allowed in Indexed View
Keywords View Definition Must Not Contain for Indexed View
View Over the View Not Possible with Index View
We know that we can use table hints when selecting records in our own queries.
my question is:
when we create a view and use table hints such as "Nolock", "Holdlock", etc ...
and then save the view in SQL server, does the table hint we used affect the view?
Yes. Any hints in the view affect how it's used.
Note: a table hint on a view propagates as well
SELECT * FROM MyView WITH (NOLOCK)-- hint applies to base tables too
What is a view in Oracle?
A View in Oracle and in other database systems is simply the representation of a SQL statement that is stored in memory so that it can easily be re-used. For example, if we frequently issue the following query
SELECT customerid, customername FROM customers WHERE countryid='US';
To create a view use the CREATE VIEW command as seen in this example
CREATE VIEW view_uscustomers
AS
SELECT customerid, customername FROM customers WHERE countryid='US';
This command creates a new view called view_uscustomers. Note that this command does not result in anything being actually stored in the database at all except for a data dictionary entry that defines this view. This means that every time you query this view, Oracle has to go out and execute the view and query the database data. We can query the view like this:
SELECT * FROM view_uscustomers WHERE customerid BETWEEN 100 AND 200;
And Oracle will transform the query into this:
SELECT *
FROM (select customerid, customername from customers WHERE countryid='US')
WHERE customerid BETWEEN 100 AND 200
Benefits of using Views
Commonality of code being used. Since a view is based on one common set of SQL, this means that when it is called it’s less likely to require parsing.
Security. Views have long been used to hide the tables that actually contain the data you are querying. Also, views can be used to restrict the columns that a given user has access to.
Predicate pushing
You can find advanced topics in this article about "How to Create and Manage Views in Oracle."
If you like the idea of Views, but are worried about performance you can get Oracle to create a cached table representing the view which oracle keeps up to date.
See materialized views
regular view----->short name for a query,no additional space is used here
Materialised view---->similar to creating table whose data will refresh periodically based on data query used for creating the view
A view is a virtual table, which provides access to a subset of column from one or more table. A view can derive its data from one or more table. An output of query can be stored as a view. View act like small a table but it does not physically take any space. View is good way to present data in particular users from accessing the table directly. A view in oracle is nothing but a stored sql scripts. Views itself contain no data.
A view is simply any SELECT query that has been given a name and saved in the database. For this reason, a view is sometimes called a named query or a stored query. To create a view, you use the SQL syntax:
CREATE OR REPLACE VIEW <view_name> AS
SELECT <any valid select query>;