《SQL命令(含例句)精心整理版(6)》
18 DB2查询语句
18.1 查询数据库大小
//命令
$db2 "call GET_DBSIZE_INFO(?,?,?,0)"
//示例
$ db2 "call GET_DBSIZE_INFO(?,?,?,0)"
Value of output parameters
--------------------------
Parameter Name : SNAPSHOTTIMESTAMP
Parameter Value : 2013-10-24-09.57.58.642597
Parameter Name : DATABASESIZE
Parameter Value : 3756920832
Parameter Name : DATABASECAPACITY
Parameter Value : 8237072382
说明:
1-命令输出为执行时间,红色字体为数据库大小,蓝色字体为数据库容量大小。
2-输出值大小单位为Byte,除以3次1024,得到单位为GB
18.2 查看表占表空间大小
1、表大小
仅查看表占用不含大数据:
select TABSCHEMA,TABNAME,OWNER,TYPE,CREATE_TIME,ALTER_TIME,STATS_TIME,NPAGES*16/1024/1024||'G' from syscat.tables order by FPAGES desc
物理包含索引大小、lop大数据占用:
slt_dir=/databak
tab_name=`cat tables.txt`
curdate=`date +%Y%m%d`
counter=0
db2 connect to tcorp > /dev/null
:> $slt_dir/size/${curdate}_size.txt
tab_name=`cat tables.txt`
for tabname in $tab_name
do
tbschema=`echo "$tabname" | awk -F"." '{print $1}'`
tbname=`echo "$tabname" | awk -F"." '{print $2}'`
counter=`echo "$counter + 1" | bc`
echo "$counter \c" >> $slt_dir/size/${curdate}_size.txt
db2 "SELECT TABNAME,SUM(DATA_OBJECT_P_SIZE+INDEX_OBJECT_P_SIZE+LONG_OBJECT_P_SIZE+XML_OBJECT_P_SIZE)/1024||'M' AS "SIZE" FROM TABLE(SYSPROC.ADMIN_GET_TAB_INFO('$tbschema','$tbname')) AS T GROUP BY TABNAME" | grep "$tbname" >> $slt_dir/size/${curdate}_size.txt
done
18.3 查看正在执行的语句
# get db2 current active sql
# create by chenzl
dbname=CFAS
echo "dbname: $dbname"
appinfolog=/db2databkup/appinfo.log
echo "logfile: $appinfolog"
>$appinfolog
db2pd -d $dbname -act >listact.tmp
linenum=`wc listact.tmp|awk 'BEGIN{FS=" "}{print $1}'`
actappnum=`expr $linenum - 5`
echo actappnum:$actappnum
tail -n $actappnum listact.tmp>actapp.tmp
echo ******************`date "+20%y-%m-%d %H:%M:%S"`******************** >>$appinfolog
delcounter=$actappnum
while [ $delcounter -gt 0 ]
do
#echo "delcounter=$delcounter"
head -n 1 actapp.tmp >dellogdataret
apphandle=`cat dellogdataret|awk 'BEGIN{FS=" "}{print $2}'`
echo "------------------apphandle: $apphandle--------------------">>$appinfolog
db2pd -d $dbname -apinfo $apphandle>>$appinfolog
delcounter=`expr $delcounter - 1`
tail -n $delcounter actapp.tmp >dellogdatatt
cat dellogdatatt >actapp.tmp
done
#display the sql
echo "----------the current active sql:-----------"
more $appinfolog|grep "Statement :"
rm -f dellogdatatt
rm -f dellogdataret
#rm -f actapp.tmp
18.4 db2expln 查看执行计划
1、查询语句的执行计划
db2expln -d dbname -i -g -q “sql语句” -o 文件名
2、示例语句:
db2expln -d tycjdb -i -g -q "SELECT B.BANKCODE, B.BANKNAME, S.SAFENAME, B.SAFECODE,SUM(E.RECNUMS) as RECNUMS,SUM(E.ERRNUMS) as ERRNUMS,CAST(SUM(E.ERRNUMS) * 100.00 / SUM(E.RECNUMS) AS DEC(12, 2)) AS RATE1 FROM tycj.VSYS_APICHKINFO E JOIN tycj.vsys_STAT_DLY_BANK B ON substr(E.ORGCODE, 7, 4) = B.BANKCODE JOIN tycj.TBL_CODESAFE S ON B.SAFECODE = S.SAFECODE WHERE 1 = 1 and E.DEALTIME >= '2019-10-01 00:00:00' and E.DEALTIME <= '2020-09-30 23:59:59' and B.BANKCODE in ('0001','0002','0003') GROUP BY B.BANKCODE, B.BANKNAME, S.SAFENAME, B.SAFECODE order by B.BANKCODE" -o 2020.txt
3、示例语句2:
db2expln -d tycjdb -q " 语句 " -g -t
4、执行计划查看方法 :
由下往上看,有索引Index: 的表示已经使用上索引,无需优化。TBSCAN :表示全表扫描,需要加索引。e+06表示10的6次方,单位是毫秒ms,所以纯数字级别的都不高。
18.5 db2advis 查看优化建议
1、查询db2优化建议
// 格式
db2advis -d tycjdb -t -s "sql语句" > 文件名
// 示例
db2advis -d tycjdb -n tycj -s "sql语句" >文件名
//说明
-d 数据库名
-n schema名
-s 后面跟语句
-o+文件名 输出时缺少前几行
用>+文件名输出比较完整
2、示例1
db2advis -d tycjdb -t -s "SELECT B.BANKCODE, B.BANKNAME, S.SAFENAME, B.SAFECODE,SUM(E.RECNUMS) as RECNUMS,SUM(E.ERRNUMS) as ERRNUMS,CAST(SUM(E.ERRNUMS) * 100.00 / SUM(E.RECNUMS) AS DEC(12, 2)) AS RATE1 FROM tycj.VSYS_APICHKINFO E JOIN tycj.vsys_STAT_DLY_BANK B ON substr(E.ORGCODE, 7, 4) = B.BANKCODE JOIN tycj.TBL_CODESAFE S ON B.SAFECODE = S.SAFECODE WHERE 1 = 1 and E.DEALTIME >= '2019-10-01 00:00:00' and E.DEALTIME <= '2020-09-30 23:59:59' and B.BANKCODE in ('0001','0002','0003') GROUP BY B.BANKCODE, B.BANKNAME, S.SAFENAME, B.SAFECODE order by B.BANKCODE" > 2020.sql
3、示例2:
db2advis -d tycjdb -s " 语句 "
4、执行优化建议方法
绿色框中为执行语句
19 空值
19.1 NULL
//命令:
is not null
19.2 TRIM
//命令:
trim(b.branchname) <>''
//示例:
update SYS_ADMITORGS a set a.ORGNAME=(select b.branchname from tbl_codebranch b where a.orgcode=b.BRANCHCODE and b.BRANCHNAME is not null and trim(b.branchname) <>'') where exists (select 1 from tbl_codebranch b where a.orgcode=b.BRANCHCODE and b.BRANCHNAME is not null and trim(b.branchname) <>'')