Are our cursors in shared pool shared at all?
How many DBA’s uses this feature of 9i (introduced in 8i but enhanced in 9i?)?
Actually, lot of us doesn’t use it all. Let’s first understand this feature and implement this in our systems.
CURSOR_SHARING is an init.ora parameter which decides whether a SQL send from user is a candidate for fresh parsing or will use an existing plan.
This parameter has 3 values.
1. CURSOR_SHARING = Exact (Default)
Definition: Share the plan only if text of SQL matches exactly with the text of SQL lying in shared pool
Let’s take an example
SQL> create table test1 (t1 number);
Table created.
SQL> insert into test1 values(1);
1 row created.
SQL> insert into test1 values(2);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from test1 where t1=1;
T1
----------
1
SQL> select * from test1 where t1=2;
T1
----------
2
SQL> select sql_text
2 from v$sql
3 where sql_text like 'select * from test1%'
4 order by sql_text;
SQL_TEXT
-----------------------------------------------------
select * from test1 where t1=1
select * from test1 where t1=2
As you see there were 2 statements in V$sql, so it generated 2 plans. Oracle had to do the same work again to generate the plan even when the difference between the two SQL was just literal value.
2. CURSOR_SHARING = Force (Introduced in 8.1.6)
Definition: Share the plan (forcibly) of a SQL if the text of SQL matches (except the literal values) with text of SQL in shared pool
This means if 2 SQL’s are same except their literal values, share the plan.
Let’s take an example:
I’m using the same table and data which is used in case of above example.
SQL> alter system flush shared_pool;
System altered.
SQL> alter session set cursor_sharing=force;
Session altered.
SQL> select * from test1 where t1=1;
T1
----------
1
SQL> select * from test1 where t1=2;
T1
----------
2
SQL> select sql_text
2 from v$sql
3 where sql_text like 'select * from test1%'
4 order by sql_text;
SQL_TEXT
---------------------------------------------------
select * from test1 where t1=:"SYS_B_0"
You can see for both the statements there was only one entry in V$sql. This means for second occurrence, oracle did not generate a new plan.
This not only helps in savings DB server engine time for generating the plan but also helps in reducing the number of plans shared pool can hold.
Important note:
Cursor_sharing = force can have some flip behavior as well, so you must be careful to use this. Using this we are forcing oracle to use the same plan for 2(or more) SQL’s even when using the same plan may not be good for similar SQL’s.
Example: “where t1=2” may be a good candidate for index scan while “where t1=10” should use a full table scan because 90% of the rows in the table has t1=10 (assumption).
3. CURSOR_SHARING = SIMILAR (Introduced in 9i)
This is the tricky one, but most used.
Definition: SIMILAR causes statements that may differ in some literals, but are otherwise identical, to share a cursor, unless the literals affect either the meaning of the statement or the degree to which the plan is optimized. (Source: Oracle documentation)
Let’s understand this.
Re-quoting the example above > “where t1=2” may be a good candidate for index scan while “where t1=10” should use a full table scan because 90% of the rows in the table has t1=10 (assumption).
To avoid 2 statements using the same plan when the same plan is not good for one of them, we have cursor_sharing=similar
Let’s take an example:
SQL> alter system flush shared_pool;
System altered.
SQL> drop table test1;
Table dropped.
SQL> create table test1 (t1 number,t2 number);
Table created.
SQL>
1 begin
2 for i in 1 .. 100 loop
3 insert into test1 values(1,i);
4 end loop;
5 commit;
6 update test1 set t1=2 where rownum <> /
PL/SQL procedure successfully completed.
In this case t1 has value “2” in first row and “1” in rest 99 rows
SQL> create index tt_indx on test1(t1);
Index created.
SQL> alter session set cursor_sharing=similar;
Session altered.
SQL> select * from test1 where t1=2;
1 row selected.
SQL> select * from test1 where t1=1;
99 rows selected.
SQL> select sql_text
2 from v$sql
3 where sql_text like 'select * from test1%'
4 order by sql_text;
SQL_TEXT
----------------------------------------------------
select * from test1 where t1=:"SYS_B_0"
select * from test1 where t1=:"SYS_B_0"
This tells us that even though the 2 statements were similar, Oracle opted for a different plan. Now even if you put t1=30 (0 rows), Oracle will create another plan.
SQL> select * from test1 where t1=30; -- (0 rows)
SQL> select sql_text
2 from v$sql
3 where sql_text like 'select * from test1%'
4 order by sql_text;
SQL_TEXT
---------------------------------------------------
select * from test1 where t1=:"SYS_B_0"
select * from test1 where t1=:"SYS_B_0"
select * from test1 where t1=:"SYS_B_0"
This is because the first time when the SQL ran, oracle engine found the literal value as “unsafe” because using the same literal value can cause bad plans for other similar SQL’s. So along with the PLAN, optimizer stored the literal value also. This will ensure the reusability of the plan only in case the same lieteral is provided. In case of any change, optimizer will generate a new plan.
But this doesn’t mean that SIMILAR and EXACT are same.
See this:
SQL> alter system flush shared_pool;
System altered.
SQL> select * from test1 where t1=2 and t1=22;
no rows selected
SQL> select * from test1 where t1=2 and t1=23;
no rows selected
SQL> select sql_text
2 from v$sql
3 where sql_text like 'select * from test1%'
4 order by sql_text;
SQL_TEXT
--------------------------------------------------------------
select * from test1 where t1=:"SYS_B_0" and t1=:"SYS_B_1"
Optimizer used single plan for both.
Conclusions:
1. Use CURSOR_SHARING=similar only when you have library cache misses and/or most of the SQL statements differ only in literal values
2. CURSOR_SHARING=force/similar significantly reduces the number of plans in shared pool
Note:
1. Oracle does not recommend setting CURSOR_SHARING to FORCE in a DSS environment or if you are using complex queries. Also, star transformation is not supported with CURSOR_SHARING set to either SIMILAR or FORCE
2. Setting CURSOR_SHARING to SIMILAR or FORCE causes an increase in the maximum lengths (as returned by DESCRIBE) of any selected expressions that contain literals (in a SELECT statement). However, the actual length of the data returned does not change.
Posted by Sachin at 4:09 AM 0 comments
Labels: Tuning
Thursday, November 30, 2006
Precaution while defining data types in Oracle
I was reading a wonderful article on Tom Kyte’s blog on repercussion of ill defined data types.
Some of the example he mentions is:
- Varchar2(40) Vs Varchar2(4000)
- Date Vs varchar2
Varchar2(40) Vs Varchar2(4000)
Generally developers ask for this to avoid issues in the application. They always want the uppermost limit to avoid any application errors. The fact on which they argue is “varchar2” data type will not reserve 4000 character space so disk space is not an issue. But what they don’t know is how costly are these.
Repercussions
- Generally application does an “array fetch” from the database i.e. they select 100 (may be more) rows in one go. So if you are selecting 10 varchar2 cols. Then effective RAM (not storage) usage will be 4000(char) x 10 (cols) x 100 (rows) = 4 MB of RAM. On contrary, had this column defined with 40 char size the usage would have been 40 x 10 x 100 ~ 40KB (approx)!! See the difference; also we didn’t multiply the “number of session”. That could be another shock!!
- Later on, it will be difficult to know for what the column was made. Ex: for first_name, if you define varchar2 (40000), it’s confusing for a new developer to know for what this column was made.
Date Vs varchar2
Again lots of developers define date cols as varchar2 (or char) for their convenience. But what they forget is not only data integrity (a date could be 01/01/03 .. what was dd,mm,yy .. then u don’t know what did you defined) but also performance.
While doing “Index range scans” by using “ between and ”
Optimizer will not be able to use the index as efficiently as in case of:
“ between and ”
I suggest you to read the full article by maestro himself.
Posted by Sachin at 4:43 AM 2 comments
Labels: Tuning
Tuesday, November 28, 2006
Oracle table compression
Compress your tables
This feature has been introduced in 9i rel 2 and is most useful in a warehouse environment (for fact tables).
How to compress? Simple
SQL> alter table test move compress;
Table altered.
How Oracle implements compression?
Oracle compress data by eliminating duplicate values within a data-block. Any repetitive occurrence of a value in a block is replaced by a symbol entry in a “symbol table” within the data block. So for example deptno=10 is repeated 5 times within a data block, it will be only stored once and rest 4 times a symbol entry will be stored in symbol table.
Its very important to know that every data block is self contained and sufficient to rebuild the uncompressed form of data.
Table compression can significantly reduce disk and buffer cache requirements for database tables while improving query performance. Compressed tables use fewer data blocks on disk, reducing disk space requirements.
Identifying tables to compress:
First create the following function which will get you the extent of compression
create function compression_ratio (tabname varchar2)
return number is — sample percentage
pct number := 0.000099;
blkcnt number := 0; blkcntc number; begin
execute immediate ' create table TEMP$$FOR_TEST pctfree 0
as select * from ' tabname ' where rownum < 1';
while ((pct < 100) and (blkcnt < 1000)) loop
execute immediate 'truncate table TEMP$$FOR_TEST';
execute immediate 'insert into TEMP$$FOR_TEST select *
from ' tabname ' sample block (' pct ',10)';
execute immediate 'select
count(distinct(dbms_rowid.rowid_block_number(rowid)))
from TEMP$$FOR_TEST' into blkcnt;
pct := pct * 10;
end loop;
execute immediate 'alter table TEMP$$FOR_TEST move compress ';
execute immediate 'select
count(distinct(dbms_rowid.rowid_block_number(rowid)))
from TEMP$$FOR_TEST' into blkcntc;
execute immediate 'drop table TEMP$$FOR_TEST';
return (blkcnt/blkcntc);
end;
/
1 declare
2 a number;
3 begin
4 a:=compression_ratio('TEST');
5 dbms_output.put_line(a);
6 end
7 ;
8 /
2.91389728096676737160120845921450151057
PL/SQL procedure successfully completed.
1 select bytes/1024/1024 "Size in MB" from user_segments
2* where segment_name='TEST'
SQL> /
Size in MB
----------
18
SQL> alter table test move compress;
Table altered.
SQL> select bytes/1024/1024 "Size in MB" from user_segments
2 where segment_name='TEST';
Size in MB
----------
6
After compressing the table, you need to rebuild indexes because the rowid's have changed.
Notes:
- This feature can be best utilized in a warehouse environment where there are lot of duplicate values (for fact tables). Infact a larger block size is more efficient, becuase duplicate values will be only stored once within a block.
- This feature has no -ve effect, infact it accelerates the performance of queries accessing large amount of data.
- I suggest you to read the following white paper by Oracle which explains the whole algorithm in details along with industry recognized TPC test cases.
http://www.vldb.org/conf/2003/papers/S28P01.pdf
I wrote the above article after reading the oramag. I suggest you to read the full article on Oracle site
2 comments:
replica watches
ray ban sunglasses
nike trainers
coach outlet
carolina jerseys
coach outlet
coach outlet
fitflops
polo ralph lauren
detroit lions jerseys
nike zoom
nike air force 1
foamposites
nike roshe run
jordan shoes
pandora bracelet
jordan shoes
nike air zoom
adidas stan smith
ultra boost
Post a Comment