Solved

can I create index on a view or a materialized view ?

Posted on 2007-12-05
6
5,310 Views
Last Modified: 2013-12-19
I have a query which does group by and is really slow. can I create a materialzed view on group by fields and create index on it ? an example would be helpful.

0
Comment
Question by:tech_question
  • 2
  • 2
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
sujith80 earned 300 total points
ID: 20417275
Yes you can have an index on MV.
See the example:

drop materialized view test_mv
/
create materialized view test_mv as
select table_name, count(1) no_of_cols
from user_tab_columns
group by table_name
/
create index test_mv_idx on test_mv( table_name )
/

see how the execution plan changes with an index:

SQL> set autotrace on;

SQL> select * from test_mv where table_name = 'TBL1';
 

TABLE_NAME                     NO_OF_COLS

------------------------------ ----------

TBL1                                    4

-- plan without index
 

Execution Plan

----------------------------------------------------------

   0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3 Card=1 Bytes=30)

   1    0   MAT_VIEW ACCESS (FULL) OF 'TEST_MV' (MAT_VIEW) (Cost=3 Card=1 Bytes=30)
 

SQL> 

SQL> create index test_mv_idx on test_mv( table_name )

  2  /
 

Index created.
 

SQL> select * from test_mv where table_name = 'TBL1';
 

TABLE_NAME                     NO_OF_COLS

------------------------------ ----------

TBL1                                    4
 

-- plan with index

Execution Plan

----------------------------------------------------------

   0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=2 Card=1 Bytes=30)

   1    0   MAT_VIEW ACCESS (BY INDEX ROWID) OF 'TEST_MV' (MAT_VIEW) (Cost=2 Card=1 Bytes=30)

   2    1     INDEX (RANGE SCAN) OF 'TEST_MV_IDX' (INDEX) (Cost=1 Card=1)
 

SQL> 

Open in new window

0
 

Author Comment

by:tech_question
ID: 20417348
will the indexes automatically get updated ?
0
 
LVL 27

Expert Comment

by:sujith80
ID: 20417353
Yes
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 7

Assisted Solution

by:DiscoNova
DiscoNova earned 200 total points
ID: 20469569
Just to comment; every time I've figured that I need a materialized view, I've been wrong. Furthermore, the MVs have usually caused me more trouble than they've ever proved to be worth of - after all; they were originally designed as items used for replication rather than "speeding up queries", which seems to be what too many people are trying to use them for :)

So... Are you absolutely certain you're trying to solve the correct problem? How about indexing the tables below the view to better accommodate for the query you're trying to perform on the view?

As an answer to the question - as you've already heard; I'd say sujith80... you can have indexes on materialized views, and the indexes will be automatically updated (along with the MVs, which is a different matter altogether; they don't necessarily do it automatically unless you tell them to (and as a warning, not all MVs can be set up to automatically update when the data below changes)).
0
 
LVL 7

Expert Comment

by:DiscoNova
ID: 20469575
I meant to write "I'd say sujith80 is correct"... wonder where the two words disappeared :)
0
 

Author Closing Comment

by:tech_question
ID: 31413053
Guys thanks for the feedback. I ended up not using an MV but using an ETL.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle Pivot 2 43
Oracle regular expression 6 47
How can I rollback insert statements after commit in oracle? 7 61
Oracle query output question 4 36
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now