Commit 23649b7a authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9781565924383

0 → 120000
+1 −0
Changes for 9781565924383: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9781565924383
 No newline at end of file

Analyze.sql

0 → 100644
+319 −0
Changes for Analyze.sql: 319 added lines, 0 removed lines.
Original line number Diff line number Diff line
rem $DBA/analyze.sql
rem
rem Performs an ANALYZE COMPUTE STATISTICS on all non-SYS/SYSTEM-owned tables
rem and indexes.
rem
rem Creates an analysis report (analyze.lis) of all analyzed tables and indexes.
rem
rem ***** Notes: *****
rem    1) This may take many hours when analyzing large tables!
rem    2) The analyze will AUTOMATICALLY turn on Cost-Based Optimization !!!
rem    3) Oracle requires locks on the tables to be analyzed.  If this prevents
rem       this script from successfully executing, use the analinds.sql script.
rem
rem Last Change 11/17/98 by Brian Lomasky - Ensure distinct_keys is never 0.
rem					    Handle null or zero values for
rem					    num_rows, chain_cnt, avg_row_len.
rem Last Change 05/22/97 by Brian Lomasky
rem
set echo off
set feedback off
set heading off
set pagesize 0
set serveroutput on size 1000000
set termout off
set verify off
drop table anal_temp;
set termout on
create table anal_temp (lineno number, text varchar2(80));
declare
	cursor time_cursor is select
		to_char(sysdate, 'HH:MI:SS')
		from dual;

	cursor tab_cursor is select
		owner,
		table_name,
		nvl(num_rows, 0),
		nvl(chain_cnt, 0),
		nvl(avg_row_len, 0)
		from sys.dba_tables
		where owner != 'SYS' and owner != 'SYSTEM'
		and table_name != 'ANAL_TEMP'
		order by owner, table_name;

	cursor ind_cursor (c_own VARCHAR2, c_tab VARCHAR2) is select
		to_char(sysdate, 'HH:MI:SS'),
		index_name,
		decode(uniqueness,
			'NONUNIQUE','Non-Unique',
			'UNIQUE','    Unique',
			'BITMAP','    Bitmap', uniqueness),
		decode(nvl(distinct_keys, 1), 0, 1, distinct_keys)
		from sys.dba_indexes
		where owner = c_own and table_name = c_tab
		order by index_name;

	cursor col_cursor (c_own varchar2, c_ind varchar2) is select
		decode(column_position, 1, column_name, ', ' || column_name)
		from sys.dba_ind_columns
		where index_owner = c_own and index_name = c_ind
		order by column_position;

	lv_owner		sys.dba_tables.owner%TYPE;
	lv_table_name		sys.dba_tables.table_name%TYPE;
	lv_num_rows		sys.dba_tables.num_rows%TYPE;
	lv_chain_cnt		sys.dba_tables.chain_cnt%TYPE;
	lv_avg_row_len		sys.dba_tables.avg_row_len%TYPE;
	lv_index_name		sys.dba_indexes.index_name%TYPE;
	lv_distinct_keys	sys.dba_indexes.distinct_keys%TYPE;
	lv_column_name		sys.dba_ind_columns.column_name%TYPE;
	lv_uniqueness		char(10);
	now			varchar2(8);
	lineno			number;
	recno			number;
	n			number;
	a_lin			varchar2(80);
	x			varchar2(80);

	function vwri(x_lin in varchar2, x_str in varchar2,
		x_force in number) return varchar2 is
	begin
		if length(x_lin) + length(x_str) > 80
		then
			lineno := lineno + 1;
			insert into anal_temp values (lineno, x_lin);
			if x_force = 0
			then
				return '                              ' ||
					x_str;
			else
				lineno := lineno + 1;
				insert into anal_temp values (lineno, x_str);
				return '';
			end if;
		else
			if x_force = 0
			then
				return x_lin||x_str;
			else
				lineno := lineno + 1;
				insert into anal_temp values (
					lineno, x_lin||x_str);
				return '';
			end if;
		end if;
	end vwri;

	function format_owner_table (the_owner in varchar2,
		the_table in varchar2) return varchar2 is
	begin
		n := length(the_owner) + length(the_table);
		if n < 40 then
			return rpad(the_owner || '.' || the_table, 40);
		else
			return '..' || substr(the_owner || '.' || the_table,
				n-36, 38);
		end if;
	end format_owner_table;

	procedure wri (my_txt in varchar2) is
	begin
		lineno := lineno + 1;
		insert into anal_temp values (lineno, my_txt);
	end wri;

begin
	lineno := 0;
	recno := 0;
	/* Analyze all of the desired tables and indexes */
	open tab_cursor;
	loop
		open time_cursor;
		fetch time_cursor into now;
		close time_cursor;
		fetch tab_cursor into
			lv_owner,
			lv_table_name,
			lv_num_rows,
			lv_chain_cnt,
			lv_avg_row_len;
		exit when tab_cursor%notfound;

		dbms_output.put_line('Starting analysis of ' ||
			format_owner_table(lv_owner, lv_table_name) ||
			' at ' || now);

		/* Analyze each table */
		dbms_ddl.analyze_object('TABLE', lv_owner, lv_table_name,
			'COMPUTE');

		open ind_cursor(lv_owner, lv_table_name);
		loop
			fetch ind_cursor into
				now,
				lv_index_name,
				lv_uniqueness,
				lv_distinct_keys;
			exit when ind_cursor%notfound;

			dbms_output.put_line('Starting analysis of ' ||
				rpad(lv_index_name, 40) || ' at ' || now);

			/* Analyze each index for this table */
			dbms_ddl.analyze_object('INDEX', lv_owner,
				lv_index_name, 'COMPUTE');

		end loop;
		close ind_cursor;
	end loop;
	close tab_cursor;

	dbms_output.put_line('Done with analysis at ' || now);

	/* Rerun all the cursors again to create the report */

	wri('                                                            ' ||
		'   Avg');
	wri('                                                       Chain' ||
		'   Row   Distinct');
	wri('Object Owner / Name                       Num Rows     Count' ||
		'   Len     Keys');
	wri('---------------------------------------- --------- ---------' ||
		' -----  ---------');

	open tab_cursor;
	loop
		fetch tab_cursor into
			lv_owner,
			lv_table_name,
			lv_num_rows,
			lv_chain_cnt,
			lv_avg_row_len;
		exit when tab_cursor%notfound;

		recno := recno + 1;
		if recno > 1 then
			wri(' ');
		end if;

		wri(format_owner_table(lv_owner, lv_table_name) ||
			to_char(lv_num_rows, '999999999') ||
			to_char(lv_chain_cnt, '999999999') ||
			to_char(lv_avg_row_len, '99999'));

		open ind_cursor(lv_owner, lv_table_name);
		loop
			fetch ind_cursor into
				now,
				lv_index_name,
				lv_uniqueness,
				lv_distinct_keys;
			exit when ind_cursor%notfound;

			wri(rpad('  ' || lv_uniqueness || ' ' || lv_index_name,
				40) || '                           ' ||
				to_char(lv_distinct_keys, '999999999'));

			a_lin := '             Indexed columns: ';
			open col_cursor(lv_owner, lv_index_name);
			loop
				fetch col_cursor into lv_column_name;
				exit when col_cursor%notfound;
				a_lin := vwri(a_lin, lv_column_name, 0);
			end loop;
			close col_cursor;
			a_lin := vwri(a_lin, '', 1);

			/* Analyze for proper index type */
			if lv_uniqueness = 'Non-Unique' then
				if lv_distinct_keys < 21 then
					wri('');
					wri('  *************************' ||
						'**************************' ||
						'***************************');
					wri('  ** The above non-unique index' ||
						' might not be appropriate,' ||
						' since non-unique    **');
					wri('  ** indexes should be created' ||
						' on columns which return' ||
						' no more than 2-4% of   **');
					wri('  ** the total number of rows' ||
						' in the table - Assuming' ||
						' an average distribution **');
					x := to_char(trunc(100 /
						lv_distinct_keys));
					wri('  ** of values, this index will' ||
						' return ' || x ||
						'% of the rows' || substr(
						'                         ',
						1, 26 - length(x)) || '**');
					wri('  *************************' ||
						'**************************' ||
						'***************************');
					wri('');
				end if;
			elsif lv_uniqueness = '    Bitmap' then
				if lv_num_rows > 0 then
					if lv_distinct_keys / lv_num_rows > .001
					then
						wri('');
						wri('  ********************' ||
							'******************' ||
							'******************' ||
							'**********************'
							);
						wri('  ** The above bitmap' ||
							' index might not' ||
							' be appropriate,' ||
							' since bitmap' ||
							' indexes    **');
						wri('  ** should be created' ||
							' on columns having' ||
							' no more than 1' ||
							' unique value' ||
							' per      **');
						x := to_char(trunc(lv_num_rows /
							lv_distinct_keys));
						wri('  ** 1000 rows - This' ||
							' index currently' ||
							' has 1 unique' ||
							' value per ' || x ||
							' rows' || substr(
							'           ', 1, 12 -
							length(x)) || '**');
						wri('  ********************' ||
							'******************' ||
							'******************' ||
							'**********************'
							);
						wri('');
					end if;
				end if;
			end if;

		end loop;
		close ind_cursor;

	end loop;
	close tab_cursor;

	commit;
exception
        when others then
                rollback;
                raise_application_error(-20000,
                        'Unexpected error on ' || lv_owner || '.' ||
			lv_table_name || ': ' || to_char(SQLCODE) || chr(10) ||
			sqlerrm || chr(10) || 'Aborting...');
end;
/

set termout off
spool analyze.lis
select text from anal_temp order by lineno;
spool off
drop table anal_temp;
set termout on
select 'Created analyze.lis report for your viewing pleasure...' from dual;
exit

CDROM/DBA.TAR

0 → 100755
+920 KiB

File added.

No diff preview for this file type.

CDROM/DBA.ZIP

0 → 100755
+241 KiB

File added.

No diff preview for this file type.

CDROM/DBA/ALERTLOG

0 → 100755
+219 −0
Changes for CDROM/DBA/ALERTLOG: 219 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/bin/sh
#
# $DBA/alertlog
#
# Analyzes the alert.log file for Redo Log statistics and errors.
# Creates a report (alertlog.lis) describing the statistics and errors.
# Creates a script (alertlog.sql) for use in loading Redo Log statistics into
# DBCare.
#
# Restrictions:
#	1) ORACLE_SID and ORACLE_HOME must be set before running this script.
#	2) An OPS$username account must exist.
#
# <<<<<<<<<< MODIFICATION HISTORY >>>>>>>>>>
# 05/27/97	Brian Lomasky	Original
#
cat /dev/null > alertlog.sql
if [ "$ORACLE_HOME" = "" ]
then
	echo "ORACLE_HOME is not set - Aborting..."
	exit 1
fi
if [ "$ORACLE_SID" = "" ]
then
	echo "ORACLE_SID is not set - Aborting..."
	exit 1
fi
#
# See if nawk should be used instead of awk
#
(nawk '{ print ; exit }' /etc/passwd) > /dev/null 2>&1
if [ ${?} -eq 0 ]
then
	cmd=nawk
else
	cmd=awk
fi
#
# get background_dump_dest and db_name using SQL*Plus into a file
#
alert_temp_file=alertlog$ORACLE_SID.tmp; export alert_temp_file
echo "Calculating background dump file destination and database name..."
sqlplus -s / >/dev/null<<EOD
set echo off
set embedded off
set feedback off
set heading off
set pagesize 0
set recsep off
set termout off
set time off
set timing off
set verify off
col value format a68
spool $alert_temp_file
select 'USRDMPDEST', value
from sys.v_\$parameter
where name = 'background_dump_dest';
select 'DBNAME', value
from sys.v_\$parameter
where name = 'db_name';
spool off
exit
EOD
if [ ! -s $alert_temp_file ]
then
	cat $alert_temp_file
	if [ -r $alert_temp_file ]
	then
		rm -f $alert_temp_file
	fi
	echo "Unexpected SQL*Plus error - Aborting..."
	exit 1
fi
set `grep "USRDMPDEST " $alert_temp_file`
bdump_dest=$2; export bdump_dest
set `grep "DBNAME " $alert_temp_file`
sid=$2; export sid
rm -f $alert_temp_file
file_spec="${bdump_dest}/alert_${sid}.log"
echo "Analyzing $file_spec for database $sid..."
if [ ! -f $file_spec ]
then
	echo "Can not locate alert log file - Aborting..."
	exit 1
fi
thiscpu=`uname -n`
echo "x" | $cmd '{ print substr("                             ",1, \
	(64-length(file_spec)-length(cpu))/2) "Analysis of " \
	file_spec " on " cpu > lis
	}' file_spec="$file_spec" cpu="$thiscpu" lis="alertlog.lis"
echo " " >> alertlog.lis
echo " " >> alertlog.lis
echo "                               Checkpoint            Pct Wait" \
	>> alertlog.lis
echo "                    Number of     Not      Wait Time   per" \
	>> alertlog.lis
echo "  Hourly Bucket   Log Switches  Complete   (seconds)  Bucket" \
	>> alertlog.lis
echo "----------------- ------------ ---------- ---------- --------" \
	>> alertlog.lis
cat $file_spec | $cmd 'BEGIN {
	first_date = 1
} function store_totals() {
	if (log_switch_ctr + check_not_comp_ctr != 0) {
		if (log_switch_ctr != 0) {
			log_sw_ctr = sprintf("%12d", log_switch_ctr)
		} else {
			log_sw_ctr = "            "
		}
		if (check_not_comp_ctr != 0) {
			comp_ctr = sprintf("%10d", check_not_comp_ctr)
		} else {
			comp_ctr = "          "
		}
		if (wait_time != 0) {
			wait = sprintf("%10d", wait_time)
			pct_wait = sprintf("     %3d%s", \
				(wait_time + 18) / 36, "%")
		} else {
			wait = "          "
			pct_wait = "         "
		}
		mm = index("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC", \
			substr(prev_date,4,3))
		print_date = sprintf("%2.2d/%2.2d/%-11s", \
			(mm + 2) / 3, substr(prev_date,1,2), \
			substr(prev_date,10,11))
		printf("%-17s%13s%11s%11s%s\n", print_date, \
			log_sw_ctr, comp_ctr, wait, pct_wait) >> lis
		print "insert into SYS.DBCARE (" \
			"time_stamp,group_desc,key_fld,value1," \
			"value2,value3)" >> alertsql
		if (substr(prev_date,1,1) == " ") {
			the_dat = "0" substr(prev_date,2,10)
		} else {
			the_dat = substr(prev_date,1,11)
		}
		printf "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n%s%s%s%s", \
			"select to_char(to_date(", q, the_dat, q, \
			",", q, "DD-MON-YYYY", q, \
			"),", q, "DD-MON-YY", q, "),", q, "alertlog", q, \
			",", q, substr(prev_date, 13, 2), q, "," >> alertsql
		print log_switch_ctr "," check_not_comp_ctr "," wait_time \
			" from dual;" >> alertsql
		return 0
	}
} function to_secs(d) {
	dd = substr(d,1,2) * 86400
	if (substr(d,4,3) == "JAN") { mm = 0 }
	if (substr(d,4,3) == "FEB") { mm = 31 }
	if (substr(d,4,3) == "MAR") { mm = 59 }
	if (substr(d,4,3) == "APR") { mm = 90 }
	if (substr(d,4,3) == "MAY") { mm = 120 }
	if (substr(d,4,3) == "JUN") { mm = 151 }
	if (substr(d,4,3) == "JUL") { mm = 181 }
	if (substr(d,4,3) == "AUG") { mm = 212 }
	if (substr(d,4,3) == "SEP") { mm = 243 }
	if (substr(d,4,3) == "OCT") { mm = 274 }
	if (substr(d,4,3) == "NOV") { mm = 304 }
	if (substr(d,4,3) == "DEC") { mm = 334 }
	yy = substr(d,10,2) * 1314000
	if (yy / 4 == int(yy / 4) && mm > 31) { mm = mm + 1 }
	hh = substr(d,13,2) * 3600
	mi = substr(d,16,2) * 60
	ss = substr(d,19,2)
	n = dd + (mm * 86400) + yy + hh + mi + ss
	return n
} {
	# See if we found a date
	if (substr($0,14,1) == ":" && substr($0,17,1) == ":") {
		# Calculate hourly-bucketed date as DD-MON-YYYY HH24:00:00
		the_date = substr($0,9,2) "-" toupper(substr($0,5,3)) "-" \
			substr($0,21,4) " " substr($0,12,2) ":00:00"
		# Calculate date as DD-MON-YYYY HH24:MI:SS
		full_date = substr($0,9,2) "-" toupper(substr($0,5,3)) "-" \
			substr($0,21,4) " " substr($0,12,8)
		if (first_date == 1) {
			first_date = 0
			prev_date = the_date
			log_switch_ctr = 0
			wait_time = 0
			check_not_comp_ctr = 0
			check_not_comp_flag = 0
		} else {
			if (check_not_comp_flag == 1) {
				curr_secs = to_secs(full_date)
				if (curr_secs >= prev_secs) {
					wait_time = wait_time + \
						curr_secs - prev_secs
				} else {
					wait_time = wait_time + \
						curr_secs - prev_secs + 86400
				}
				check_not_comp_flag = 0
			}
			if (the_date != prev_date) {
				dummy = store_totals()
				prev_date = the_date
				log_switch_ctr = 0
				wait_time = 0
				check_not_comp_ctr = 0
				check_not_comp_flag = 0
			}
		}
	}
	if (index($0, "advanced to log") != 0) {
		log_switch_ctr = log_switch_ctr + 1
		if (check_not_comp_flag == 1) { check_not_comp_flag = 0 }
	}
	if (index($0, "Checkpoint not complete") != 0) {
		check_not_comp_ctr = check_not_comp_ctr + 1
		prev_secs = to_secs(full_date)
		check_not_comp_flag = 1
	}
} END {
	if (first_date == 0) { dummy = store_totals() }
}' alertsql="alertlog.sql" lis="alertlog.lis" q="'"
echo "Created alertlog.lis report for your viewing pleasure..."
Loading