Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: SQL
Expertise: Advanced
Apr 20, 2007

Predicate Pushing

Oracle supports the pushing of predicates into a given view. Suppose you have a set of layered views:

--View One
CREATE VIEW vw_layer_one
-- View Two
CREATE VIEW vw_layer_two_dept_100
AS SELECT * FROM vw_layer_one
WHERE deptno=100;
Then assume you issued this query:

SELECT * FROM vw_layer_two_dept_100
WHERE empid=100;
The predicate in this statement is WHERE empid=100;. You may have one of tens or even hundreds of predicates (if you have hundreds, you don't want to be supporting your SQL code!). In many cases, Oracle will push those predicates down into the views being called. Thus, Oracle will transform the vw_layer_one view into a SQL statement that looks like this:

CREATE VIEW vw_layer_one
WHERE deptno=100
AND empid=100;
Note that both the predicate from vw_layer_two (where deptno=100) and the predicate from the SQL statement being executed (where empid=100) are pushed down into the final view that is executed. This can have significant performance benefits, because now the bottom view can possibly use an index if one exists on deptno and/or empid.

Predicate pushing has a number of restrictions that are beyond the scope of this tip, but you can find them in the Oracle documentation. Also, any predicate pushing may result in a hard parse of the underlying SQL that is executed. Hence, it is important to make sure you use bind variables instead of literals in SQL code calling views. Your SQL should look something like this, for best performance:

SELECT * FROM vw_layer_two_dept_100
WHERE empid=:b100;
Shraddha Pandya
Thanks for your registration, follow us on our social networks to keep up-to-date