|
|
|
|
|
Re: performance doubt [message #519368 is a reply to message #519366] |
Thu, 11 August 2011 08:09 |
cookiemonster
Messages: 13958 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Generally I think views should be written to answer a specific question. If you want an answer to a different question don't use the view, go to the base tables or create a new view that does what you need.
I've had to rewrite a lot of code where developers had joined views, or nested views, which resulted in queries that either accessed the base tables more times than necessary or accessed tables that weren't needed to get the answer. Unsurprisingly this led to a number of performance problems.
Joining and nesting views can work if each view only accesses one table, but as soon as the views start joining tables you shouldn't try joining or nesting them. Also if you need a query that only needs a subset of what a view does, avoid the view.
|
|
|
|
Re: performance doubt [message #520822 is a reply to message #519360] |
Wed, 24 August 2011 20:25 |
|
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
Depends on many factors some of which have been noted:
1) issue is really, what are your design goals for your system.
2) ** my opinion ** I like views. They let you create ideas in your database beyond simple tables and enforce the idea across applications and queries so that results are consistent.
3) your question is related to the phenomenon known as DUNSEL JOIN REMOVAL. If your databse is designed well, then in the newer versions of Oracle, the issue is resolved somewhat by query optimizations, more so in 11g than 10g. Under the right circumstances, Oracle is able to figure out that the tables are not needed and will remove them from the query before it executes it. Consider this:
SELECT EMP.*
FROM EMP,DEPT
WHERE DEPT.DEPT_ID = EMP.DEPT_ID;
is the above query the same as the below query?
Answer: don't know. It depends. Consder this:
create table emp(emp_id integer not null
,dept_id integer not null)
alter table emp add constraint emp_fk1 foreign key (dept_id) references dept;
Are the two queries the same now? Answer: YES. If they are the same then why do the join to DEPT? Answer: don't bother. If we don't need to do the join, does the CBO know this? Answer: depends, you running 9i or 10g or 11g?
SQL> create table dept
2 (
3 dept_id integer not null primary key
4 )
5 /
Table created.
SQL> create table emp
2 (
3 emp_id integer not null primary key
4 , dept_id integer not null
5 )
6 /
Table created.
SQL> alter table emp add foreign key (dept_id) references dept
2 /
Table altered.
SQL> set autotrace on
SQL>
SQL> select dept.*
2 from emp,dept
3 where dept.dept_id = emp.dept_id
4 /
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 3956160932
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| EMP | 1 | 13 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
222 recursive calls
0 db block gets
47 consistent gets
0 physical reads
0 redo size
335 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
6 sorts (memory)
0 sorts (disk)
0 rows processed
SQL>
Notice anything interesting about the plan vs. the query?
Good luck, Kevin
|
|
|
Re: performance doubt [message #520869 is a reply to message #520822] |
Thu, 25 August 2011 03:25 |
cookiemonster
Messages: 13958 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Of course that only works where foreign keys are defined so it's probably not a good idea to rely on it to dig you out of holes.
|
|
|
Re: performance doubt [message #520917 is a reply to message #520869] |
Thu, 25 August 2011 07:45 |
|
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
very true. I get so frustrated with the number of systems I deal with daily that failed to do basic design. 99% of all performance problems are fixed before they ever happen, by managing the basics before code is written.
Kevin
|
|
|