透過shell指令碼生成查詢表資料的sql
當然了對於不同的表來說,應該還是可以做一些細分,能夠最大程度的提高效率,比如表中含有主鍵列,嘗試走索引掃面可能會被全表掃描效率要高。如果表中的資料太多,而且沒有一些相關的約束,可以考慮使用並行來提高等等。
以下就是使用shell指令碼所做的查詢資料條數的一個例子,看起來有些太不值得了,但是如果資料量很大的情況下這些分析就格外有用了。
比如表customer,資料量不是很大,可以直接走索引來做。
> ksh get_tab_cnt.sh prdappo customer
************************************************
CUSTOMER .859375
CUSTOMER
select /*+ index_ffs(CUSTOMER,CUSTOMER_pk ) parallel_index(CUSTOMER,CUSTOMER_pk,1) */ 'CUSTOMER,', count(*) from CUSTOMER ;
************************************************
GREEMENT 10.359375
GREEMENT
select /*+ index_ffs(GREEMENT,GREEMENT_pk ) parallel_index(GREEMENT,GREEMENT_pk,4) */ 'GREEMENT,', count(*) from GREEMENT ;
對應的指令碼如下:
#!/bin/ksh
#get_tab_cnt_sql
DATE=`/bin/date '+%y%m%d_%H%M%S'`
constr1=`echo ${constr1} | tr '[:lower:]' '[:upper:]'`
timestamp=$3
time_vs=""
if [ "${timestamp}" = "" ]
then
echo " "
echo " "
else
if [ "${timestamp2}" = "" ]
then
exit;
else
time_vs=" As of timestamp to_date('${timestamp}','mm/dd/yyyy hh24:mi:ss') "
time_vt=" As of timestamp to_date('${timestamp2}','mm/dd/yyyy hh24:mi:ss') "
fi
fi
user1=`echo $constr1 | awk -F "/" '{print $1}'`
inst1=`echo $constr1 | awk -F "@" '{print $2}'`
if [[ -z ${inst1} ]];then
inst1=$ORACLE_SID
}
fi
function check_connectivity
{
Num=`echo show user | $ORACLE_HOME/bin/sqlplus -s $1| grep -i 'USER ' | wc -l`
if [ $Num -gt 0 ]
then
## ok - instance is accessible
echo '************************************************'
else
## inst is Inaccessible
echo Connection Details: `echo $1 ` is Inaccessible...
echo '************************************************'
exit;
fi
}
check_connectivity $constr1
sleep 1;
sleep 1;
mkdir `pwd`/Get_tab_cnt_${user1}_${inst1}_${DATE}
mkdir `pwd`/Get_tab_cnt_${user1}_${inst1}_${DATE}/List
small2='1'
small='10'
medium='30'
big_par=8
medium_par=4
small_par=2
small2_par=1
COUNT_DIR=`pwd`/Get_tab_cnt_${user1}_${inst1}_${DATE}
exclude_tab=x
sqlplus -s ${constr1} <<EOF
set head off
set line 100
set pages 50000
set long 10000
col segment_name for a40
spool ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_size_temp.lst
select distinct segment_name,to_number(to_char(sum(bytes/1024/1024/1024))) from dba_segments
where owner=upper('$1') and segment_name =upper('$2') group by segment_name order by 2 desc;
spool off;
spool ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_with_pk.lst
select table_name from dba_tables where owner=upper('$1') and table_name in ( select table_name from dba_constraints where owner=upper('$1') and table_name =upper('$2')
and constraint_type='P');
spool off;
EOF
################## Getting Big Tables ######################
sed '/^$/d' ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_size_temp.lst | grep -v SQL | grep -v select | grep -v ERROR | grep -v ORA- | grep -v '*' > ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_size.lst
echo ${medium} | awk -v medium=${medium} ' $2 > medium { print $1 } ' ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_size.lst > ${COUNT_DIR}/List/${user1}_${inst1}_big_tab.lst
echo ${medium} | echo ${small} | awk -v medium=${medium} -v small=${small} ' $2 < medium && $2 > small { print $1 } ' ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_size.lst > ${COUNT_DIR}/List/${user1}_${inst1}_medium_tab.lst
echo ${small} | echo ${small2} | awk -v small=${small} -v small2=${small2} ' $2 < small && $2 > small2 { print $1 } ' ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_size.lst > ${COUNT_DIR}/List/${user1}_${inst1}_small_tab.lst
echo ${small2} | awk -v small2=${small2} ' $2 < small2 { print $1 } ' ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_size.lst > ${COUNT_DIR}/List/${user1}_${inst1}_small2_tab.lst
###############################( Generating Count Files ( Big ) #############################################
while read table
do
is_pk=`grep -i ${table} ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_with_pk.lst | wc -l `
is_pk=`echo $is_pk`
if [ is_pk -ne 0 ]
then
echo "select /*+ index_ffs(${table},${table}_pk ) parallel_index(${table},${table}_pk,${big_par}) */ '${table},', count(*) from ${table} ${time_vs} ;" >> ${COUNT_DIR}/sqls/${user1}_${inst1}_${table}_tab_count.sql
else
echo "select /*+ PARALLEL(${table},${big_par}) */ '${table} ,' , count (1) from ${table} ${time_vs} ;" >> ${COUNT_DIR}/sqls/${user1}_${inst1}_${table}_tab_count.sql
fi
done < ${COUNT_DIR}/List/${user1}_${inst1}_big_tab.lst
################################ Generating Count Files ( Medium ) #############################################
while read table
do
#echo " Table Name is : $table "
is_pk=`grep -i ${table} ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_with_pk.lst | wc -l `
is_pk=`echo $is_pk`
if [ is_pk -ne 0 ]
then
echo "select /*+ index_ffs(${table},${table}_pk ) parallel_index(${table},${table}_pk,${medium_par}) */ '${table},', count(*) from ${table} ${time_vt} ;" >> ${COUNT_DIR}/List/${user1}_${inst1}_medium_tab_count.lst
else
echo "select /*+ PARALLEL(${table},${medium_par}) */ '${table} ,' , count (1) from ${table} ${time_vs} ;" >> ${COUNT_DIR}/List/${user1}_${inst1}_medium_tab_count.lst
fi
done < ${COUNT_DIR}/List/${user1}_${inst1}_medium_tab.lst
################################ Generating Count Files ( Small ) #############################################
while read table
do
#echo " Table Name is : $table "
is_pk=`grep -i ${table} ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_with_pk.lst | wc -l `
is_pk=`echo $is_pk`
if [ is_pk -ne 0 ]
then
echo "select /*+ index_ffs(${table},${table}_pk ) parallel_index(${table},${table}_pk,${small_par}) */ '${table},', count(*) from ${table} ${time_vs} ;" >> ${COUNT_DIR}/List/${user1}_${inst1}_small_tab_count.lst
else
echo "select /*+ PARALLEL(${table},${small_par}) */ '${table} ,' , count (1) from ${table} ${time_vs} ;" >> ${COUNT_DIR}/List/${user1}_${inst1}_small_tab_count.lst
fi
done < ${COUNT_DIR}/List/${user1}_${inst1}_small_tab.lst
################################ Generating Count Files (Very Small ) #############################################
while read table
do
#echo " Table Name is : $table "
is_pk=`grep -i ${table} ${COUNT_DIR}/List/${user1}_${inst1}_all_tab_with_pk.lst | wc -l `
is_pk=`echo $is_pk`
if [ is_pk -ne 0 ]
then
echo "select /*+ index_ffs(${table},${table}_pk ) parallel_index(${table},${table}_pk,${small2_par}) */ '${table},', count(*) from ${table} ${time_vs} ;" >> ${COUNT_DIR}/List/${user1}_${inst1}_small2_tab_count.lst
else
echo "select /*+ PARALLEL(${table},${small2_par}) */ '${table} ,' , count (1) from ${table} ${time_vs} ;" >> ${COUNT_DIR}/List/${user1}_${inst1}_small2_tab_count.lst
fi
done < ${COUNT_DIR}/List/${user1}_${inst1}_small2_tab.lst
rm ${COUNT_DIR}/List/*temp.lst
#################### get table list from schemas ##########################
touch ${COUNT_DIR}/${user1}_${inst1}_final_tab_cnt_stats.sql
schema_con=${user1}_${inst1}
function get_final_tab_cnt_stats
{
schema_type=$1
if [ -s ${COUNT_DIR}/List/${schema_con}_big_tab_count.lst ];then
cat ${COUNT_DIR}/List/${schema_con}_big_tab_count.lst >> ${COUNT_DIR}/${schema_con}_final_tab_cnt_stats.sql
fi
if [ -s ${COUNT_DIR}/List/${schema_con}_medium_tab_count.lst ];then
cat ${COUNT_DIR}/List/${schema_con}_medium_tab_count.lst >> ${COUNT_DIR}/${schema_con}_final_tab_cnt_stats.sql
fi
if [ -s ${COUNT_DIR}/List/${schema_con}_small2_tab_count.lst ];then
cat ${COUNT_DIR}/List/${schema_con}_small2_tab_count.lst >> ${COUNT_DIR}/${schema_con}_final_tab_cnt_stats.sql
fi
if [ -s ${COUNT_DIR}/List/${schema_con}_small_tab_count.lst ];then
cat ${COUNT_DIR}/List/${schema_con}_small_tab_count.lst >> ${COUNT_DIR}/${schema_con}_final_tab_cnt_stats.sql
fi
}
get_final_tab_cnt_stats $schema_con
cat ${COUNT_DIR}/${schema_con}_final_tab_cnt_stats.sql
rm -rf ${COUNT_DIR}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30633755/viewspace-2127773/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 通過shell指令碼生成查詢表資料的sql指令碼SQL
- 透過shell指令碼生成資料統計資訊的報表指令碼
- 透過shell指令碼定位效能sql和生成報告指令碼SQL
- 通過shell指令碼生成資料統計資訊的報表指令碼
- 透過shell和sql結合查詢效能sqlSQL
- 透過shell指令碼得到資料字典的資訊指令碼
- 巧用shell生成資料庫檢查指令碼資料庫指令碼
- 將表資料生成SQL指令碼的儲存過程和工具SQL指令碼儲存過程
- 透過shell指令碼檢視資料庫表空間使用情況指令碼資料庫
- 透過shell得到資料庫中許可權的指令碼資料庫指令碼
- 透過shell指令碼分析足彩指令碼
- 透過shell指令碼監控sql執行頻率指令碼SQL
- 有關表空間查詢的sql指令碼SQL指令碼
- 透過shell指令碼抓取awr報告中的問題sql指令碼SQL
- 通過shell指令碼定位效能sql和生成報告指令碼SQL
- 透過shell定製ash指令碼指令碼
- 利用shell指令碼生成動態sql指令碼SQL
- 透過shell指令碼來檢視Undo中資源消耗高的sql指令碼SQL
- 透過shell指令碼檢視procedure的資訊指令碼
- 透過shell指令碼檢視package的資訊指令碼Package
- 使用shell指令碼生成只讀許可權的sql指令碼指令碼SQL
- sql server 資料指令碼生成工具SQLServer指令碼
- 透過shell指令碼檢視鎖資訊指令碼
- 透過shell指令碼監控oracle session指令碼OracleSession
- 【資料庫】sql連表查詢資料庫SQL
- 通過spid,查詢執行慢的sql指令碼SQL指令碼
- [Shell] Shell 生成 HTML指令碼HTML指令碼
- 通過shell和sql結合查詢效能sqlSQL
- 通過shell指令碼得到資料字典的資訊指令碼
- 使用shell批量生成資料整合式遷移的指令碼指令碼
- 巧用shell指令碼生成快捷指令碼指令碼
- shell查詢prometheus資料Prometheus
- [ Shell ] 通過 Shell 指令碼匯出 CDL 網表指令碼
- 透過shell解析dump生成parfile
- 透過shell抓取html資料HTML
- sql查詢一張表的重複資料SQL
- 使用Shell指令碼查詢程式對應的程式ID指令碼
- 通過shell指令碼檢視資料庫表空間使用情況指令碼資料庫