通过shell脚本进行数据库索引的查询
#!/bin/sh
export PGPASSWORD=appuser_pt
while read -r line
do
tablename=` echo $line | cut -d "|" -f 1`
indexnum=`echo $line | cut -d "|" -f 2`
psql -h 20.555.14.37 -p 5412 -U appuser -d dcbsdb <<EOF >pg_index_result.txt
select count(*) from pg_indexes where tablename = '${tablename}';
EOF
dbnum=`cat ./pg_index_result.txt | grep -v "count" | grep -v '\-' | grep -v "(1 row)"`
if [ $indexnum -eq $dbnum ];then
echo "${tablename}索引个数相同。"
else
echo ${tablename} >> pg_index_miss.txt
echo "${tablename}表索引缺失,索引个数应该为:${indexnum}个,当前个数为:${dbnum}个,缺失索引的库表已经记录在文档:pg_index_miss.txt"
fi
done <./Sql_Index_Num.txt