Вы находитесь на странице: 1из 6

Set lock mode to page level

(informix)

output to lockmode.sql without headings


select "alter table " || tabname || " lock mode (row);"
from systables where locklevel = "P";

Count of tables in the database


(Informix)

select tabname, nrows from systables


where tabid > 99
order by nrows desc;

(oracle) run the resulting countall.sql

set flush off


set verify off
set echo off
set pagesize 0
set feedback off
set heading off
spool countall.sql
select 'select count(*) as '||table_name||' from '||table_name||';'
from dba_tables
where not (owner = 'SYS' or owner = 'SYSTEM' or owner = 'OEM');
spool off

Remove the sv_ and btv_ views


(Informix) Run the drop_views.sql after each statement. These statements will drop
every view, so make sure no users are on.

unload to "drop_views.sql" delimiter ";"


select "drop view "||tabname
from systables
where tabtype = "V" and tabid >= 100
and tabname like "sv_%" or tabname like "btv_%";

(Oracle) Log out of sqlplus and back in before running the dropviews.sql, and be
sure to change the date to something close to current.

set termout off;


set heading off;
spool dropviews.sql;
select 'DROP VIEW OPS$BSIDBA.' || object_name || ';'
from all_objects
where owner='OPS$BSIDBA' and object_type='VIEW' and
(object_name like 'BTV_%' or object_name like 'SV_%') and
created < '20070812';

Triad sql when refreshing a test database


(Informix)

update sy_scl_def
set last_update = current
where sc_type like 'W%';
update sy_tran_cd
set last_update = current
where type = '4L'
or type = '4P' or type = '4U';

(Oracle)

update sy_scl_def
set last_update = sysdate
where sc_type like 'W%';
update sy_tran_cd
set last_update = sysdate
where type = '4L' or type = '4P' or type = '4U';

Last time stastics were run


(INFORMIX)

select distinct tabname, b.constructed, b.mode


from systables a, sysdistrib b
where a.tabid=b.tabid
order by 1

Condensing extents
(Informix)

The following command can be used to move a table to a different dbspace:

alter fragment on table <table_name> init in <dbspace_name>

Page
2
This will rebuild the table in the process, so it is also a handy way to re-create
extents (you can just specifying the existing dbspace name to re-create the table in
the same space).

Show tables with more than 8 extents


(INFORMIX) Show tables with more than 8 extents (RUN AGAINST SYSMASTER
DATABASE)

select dbsname,
tabname,
count(*) num_of_extents,
sum( pe_size ) total_size
from systabnames, sysptnext
where partnum = pe_partnum
and tabname not matches "sys*"
and tabname not matches "TBL*"
group by 1, 2
having count(*) >= 8
order by 3 desc, 4 desc;

(Oracle) This will also show the size of each table.

col Next head 'Next Ext|Kbyte'


col KBYTES head 'Used|Kbytes'

select substr(owner,1,19) Owner


,substr(segment_name,1,20) Segment_name
,substr(segment_type,1,6) type
,rtrim(to_char((bytes/1024),'999,999,999')) KBytes
,rtrim(to_char(extents,'9,999')) Ext
,rtrim(to_char((next_extent/1024),'999,999')) NexT
from sys.DBA_SEGMENTS
where extents >= 8
and OWNER not in ('SYS','SYSTEM')
order by Ext desc,owner,segment_type
/

Show what tables are in a particular dbspace


(INFORMIX)

SELECT dbinfo("DBSPACE", partnum) dbspace, dbsname, tabname


FROM sysmaster:systabnames
WHERE dbinfo("DBSPACE", partnum) = "imagedbs"

Page
3
Delete data from a table using a single lock
(Informix)

When needing to delete large amount of data from tables you can use this to limit
locks and speed up the process

BEGIN WORK;
LOCK TABLE <tabname> IN EXCLUSIVE MODE;
DELETE FROM <tabname> WHERE 1=1;
COMMIT WORK;

Recreate all IFAS Synonyms


(Oracle) This script is needed when doing a schema-level export/import in Oracle as
the public synonyms do not get recreated automatically.

set flush off


set verify off
set echo off
set pagesize 0
set feedback off
set heading off
spool synonyms.sql
select 'create public synonym '||table_name||' for '||owner||'.'||table_name||' ;'
from dba_tables where owner = ‘OPS$BSIDBA’;
spool off
/

Rebuild all indexes/statistics


(Oracle)

Note: If using the Enterprise edition of Oracle, the keyword “online” can be specified
after “rebuild” to rebuild the indexes with no downtime (ie exclusive access is not
required).

Indexes:

set flush off


set verify off
set echo off
set pagesize 0
set feedback off
set heading off
spool rebuild_indexes.sql
select 'alter index ' ||table_owner||'.'||index_name||' rebuild;'
from dba_indexes

Page
4
where table_owner = 'OPS$BSIDBA';
spool off

Statistics:

set flush off


set verify off
set echo off
set pagesize 0
set feedback off
set heading off
spool compute_statistics.sql
select 'analyze table ' ||owner||'.'||table_name||' compute statistics ;'
from dba_tables
where owner = 'OPS$BSIDBA';
spool off

Put Oracle tablespace in hot backup mode


(Oracle) If necessary to perform a hot backup(online) of an Oracle database (outside
of the automated nightly backup), use these steps:

Hot backup mode:


Run this sql to get a list

select tablespace_name from dba_tablespaces


where not (contents = 'TEMPORARY');

Then create an alter statement for every tablespace:

alter tablespace system begin backup;


alter tablespace tools begin backup;
alter tablespace undotbs begin backup;
alter tablespace temp begin backup;
alter tablespace users begin backup;
alter tablespace ifas begin backup;

After running the alter statements, run this:

select * from v$backup;

As long as every file has a Status of ‘ACTIVE’, you can now backup all .dbf files.

Taking the instance out of hot backup mode:

Run the alter statements for every tablespace specifying “end backup”:

alter tablespace system end backup;


alter tablespace tools end backup;
alter tablespace undotbs end backup;

Page
5
alter tablespace temp end backup;
alter tablespace users end backup;
alter tablespace ifas end backup;

Then run the v$backup select again:

select * from v$backup;

Ensure that all files have a Status of ‘NOT ACTIVE’.

Data file free space report


(oracle)

This shows the amount of free space in each datafile, and whether each datafile is
set to auto extend or not.

select
a.file_id
,a.tablespace_name
,trunc(decode(a.autoextensible,'YES',a.maxsize-
a.bytes+b.free,'NO',b.free)/1024/1024) free_mb
,trunc(a.bytes/1024/1024) size_mb
,trunc(a.maxsize/1024/1024) maxsize_mb
,a.autoextensible ae
,trunc(decode(a.autoextensible,'YES',(a.maxsize-a.bytes+b.free)/a.maxsize*100,
'NO',b.free/a.maxsize*100)) free_pct
from
(select
file_id
,tablespace_name
,autoextensible
,bytes
,decode(autoextensible,'YES',maxbytes,bytes) maxsize
from dba_data_files
group by file_id, tablespace_name, autoextensible, bytes,
decode(autoextensible,'YES',maxbytes,bytes)) a,
(select file_id, tablespace_name, sum(bytes) free
from dba_free_space
group by file_id, tablespace_name) b
where a.file_id=b.file_id(+)
and a.tablespace_name=b.tablespace_name(+)
order by a.tablespace_name asc;

Page
6