- SQLPlus environment settings
under [oracle_home]\sqlplus\admin
there is a glogin.sql file. This one is being run each time you login into a database.
Here are some commands which will set a couple of useful things up:
--enable the DBMS_OUTPUT and set the buffer size to 100000 bytes
set serveroutput on size 100000
--trim the spaces, set pagesize and line length
set trimspool on
set long 5000
set linesize 100
set pagesize 9999
column plan_plus_exp format a80
--set the prompt to "[user]@[dbname]>"
column global_name new_value gname
set termout off;
select lower(user) || '@' || global_name as global_name from global_name;
set sqlprompt '&gname> '
set termout on
- setup the PLAN_TABLE and AUTOTRACE
Then create a public synonym with same name and issue GRANT ALL ON PLAN_TABLE TO PUBLIC
can now use the explain plan statement in any schema.
- set the AUTOTRACE in SQLPlus
then run GRANT PLUSTRACE TO PUBLIC
to use the autotrace issue one of the following:
set autotrace on
set autotrace off
set autotrace on explain
set autotrace on statistics
set autotrace traceonly
- avoid using long running transactions in MTS environment
- using bind variables causes already compiled SQL statements to be reused from shared pool. Otherwise they are being compiled over and over again.
- B* tree based index, does not index NULL values
- Memory
select a.name, b.valueSGA consists of: java pool, large pool, shared pool, null pool
from v$statname a join v$mystat b
on (a.statistic# = b.statistic#) where a.name like '%ga %';
null pool has fixed sga, buffer cache and redo log buffer inside.
shared pool keeps all the compilted sql and pl/sql objects. To make use of shared pool optimal - always use bind variables. Otherwise shared pool will grow too large and handling an over sized shared pool takes a lot of processor time and this leads to dramatic system slow down. Using the dbms_shared_pool utility, you can make some certain object to stay in sp for ever, otherwise unused objects are removed from sp when it's getting full.
shared_pool_size init parameter is always smaller than the actual sp size.
large pool is meant for larger mem structures than in shared pool. It also doesn't keep cache things: after memory has been used, it may be rewritten. large pool keep things like these: UGA are in SGA in case MTS is used, parallel stuff, RMAN IO buffering.
to get sga statistics run in sqlplus
compute sum of bytes on poolmore simple report: show sga
break on pool skip 1
select pool, name, bytes
from v$sgastat
order by pool, name;
- Locks
This was fixed in 9i, but still creating indexes on fks is good.
TX - transaction lock. Is set when a transaction starts modifying some rows. This lock is kept until transaction issues commit or rollback. Initial number of transactions capable of blocking some data in a block is set my INITRANS parameter of CREATE statement (default 2). Maximum number of transactions is set by MAXTRANS (default 255). But it might be also limited by the free space in block's header.
To get information on TX locks, v$lock can be queried
select username,here ID1 and ID2 fields contain the transaction id. since it has three numbers saved in two fields, some math tricks are needed to get those numbers. You can see that rbs, slot and seq coincide with
v$lock.sid,
trunc(id1/power(2,16)) rbs,
bitand(id1,to_number('ffff','xxxx'))+0 slot,
id2 seq,
lmode,
request
from v$lock, v$session
where v$lock.type = 'TX'
and v$lock.sid = v$session.sid
and v$session.username = USER
/
select XIDUSN, XIDSLOT, XIDSQN from v$transactionTM lock - locks an object from being modified by DDL statements while transaction is modifying data in it. Can use the same query as above, only exclude the where = 'TX' and query simple values of ID1 and ID2. Here ID1 will contain object id being blocked. It coincide with object id from
column object_name format a20it is also possible to forbid DDL statements at all by using the DML_LOCK init param. Or do the same for certain tables by using ALTER TABLE [table] DISABLE TABLE LOCK
select object_name, object_id from user_objects;
- DDL Locks
beginThis is done so not to rollback all the previous steps in case DDL fails. So it is important to remember that DDL will initiate a commit silently.
commit;
DDL statement
commit;
exception
when others then rollback
end;
To get list of all ddl locks held at the moment use the DBA_DDL_LOCKS view
- Transactions
begin
savepoit sp1
[some pl/sql stuff]
exception
when others then
rollback to sp1;
end;
INSERTS are handled by Oracle in the following way:
savepoint statement1So when an error occurs with some INSERT operator then only the portion of transaction related to this operator is rolled back.
insert blabla
if error then rollback to statement1
savepoint statement2
insert blabla
if error then rollback to statement2
- Tables
imp user/pass tables=[tablename,..]you will get the full create statements inside the index file.
exp user/pass full=y indexfile=[filename]
- Temp tables
To generate the table statistics, so that CBO would optimize the queries against temp tables correctly, you can create a normal table with the same name and then export/import statistics using the dmbs_stats package.
- Indexes
The most common index type. Stores the indexed values in a tree structure.
If several columns are used in an index and values in some of them are repeated a lot, then index could be compressed using the compress n option. In this case index structure will take less space and index reading will require less IO. On the other hand, index operation will require more processor time.
B* in descending order
Indexes could be stored in ASC or DESC order. This is only needed when you have several columns in an index and you need to select each of them with different order. Like this: order by key1 DESC, key2 ASC. You need to specify the desired order when creating an index.
BITMAP
Use bitmap index when you have a very non-unique column. Drawback: when updating a row, a lot more rows are getting locked at the same time.
Other notices concerning index.
When using something like select * from t where x = 5 But x is of a character type, the statement will be silently rewritten as select * from t where to_number(x) = 5 And of course in this case index will not be used.
- EXP and IMP
Constrains with automatically generated names, will be imported even if such constraint already exists: oracle will simply generate a new name for it. This could be influencing performance in case import is performed repeatedly. So always give names to the constraints!
No comments:
Post a Comment