Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Expansion in Oracle

Tags:

sql

oracle

views

So we have some developers who went a little view happy. So now we have views that reference views that reference views, ad nauseum.

So what I want, in order to assist me in Tuning, is to expand these views.

I want a function that takes a string and returns a string. The input string is the query, the output string is the same query without views.

CREATE OR REPLACE VIEW myView AS
SELECT * FROM emp

Using function/stored procedure "F":

F('SELECT * FROM myView') 

...would return:

SELECT * FROM ( SELECT * FROM emp)

  1. Is there an Oracle package for this?
  2. Does someone have code in:
    1. either SQL or PL/SQL
    2. In something else
like image 529
Stephanie Page Avatar asked Sep 30 '10 19:09

Stephanie Page


People also ask

What is view expansion?

VIEW expansion. If object used in FROM clause is object VIEW, then extract view text from database and replace view name with view text. This is an article of Oracle SQL Query rewrite serials.

What is the view in Oracle?

An Oracle view is a validated and named SQL query stored in the Oracle Database's data dictionary. Views are simply stored queries that can be executed when needed, but they don't store data. It can be helpful to think of a view as a virtual table, or as the process of mapping data from one or more tables.

How do I expand a table in Oracle SQL Developer?

View TablesExpand the system node in Oracle SQL Developer. Expand the Other Users node and then expand the HR node. Expand the Tables (Filtered) node and select the EMPLOYEES table. Detailed information about the table is displayed in the object pane.


2 Answers

Short answer:

  1. Not at this time

  2. Not that I'm aware of

UPDATE

It looks like Oracle 12c has exactly what you need: DBMS_UTILITY.expand_sql_text

http://docs.oracle.com/cd/E16655_01/appdev.121/e17602/d_util.htm#ARPLS73973

like image 76
Jeffrey Kemp Avatar answered Sep 28 '22 08:09

Jeffrey Kemp


One problem with what you are proposing is that there are usually multiple ways that a query involving views can be rewritten, so simply expanding the text of the views won't necessarily tell you a lot about how the query is being executed.

Since your purpose is tuning, I would suggest that the execution plans for the queries will probably give you the information you really need. This won't show the rewritten query, but it will show you all the actual tables and how they are referenced in executing the query.

The best way I know of to view the actual execution plan is:

SELECT /*+ gather_plan_statistics */ * FROM myView

select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'))
like image 30
Dave Costa Avatar answered Sep 28 '22 08:09

Dave Costa