存档

2014年6月9日 的存档,文章数:2

曾经写过博文介绍利用代理表统计数据库内所有表占用空间情况,也包括表的行数:

可以参考:ASE15.0中利用代理表实现统计用户表存储空间大小的功能

配置代理表可能有些麻烦,本文提供一个简单的脚本来获得数据库内表的行数。

#!/bin/bash

ISQL="isql -Usa -P -SSYBASE -w5000"
database_name="tpchdb"

$ISQL  <<EOF  | sed -e '1,2d' -e 's/^ *//;s/ *$//' -e '/^$/d' > tablename.list
use ${database_name}
go
set nocount on
go
select name from sysobjects where type='U' order by name
go
quit
EOF

while read table_name
do
        if [ "${table_name}" = "" ]; then
                break
        fi
        $ISQL <<EOF | grep "=" | sed -e 's/^ *//;s/ *$//;s/= */=/'
use ${database_name}
go
set nocount on
go
select "${table_name}=",count(*) from ${table_name}
go
quit
EOF
done < tablename.list
echo "Table Count:`wc -l tablename.list|awk '{print $1}'`"
rm -f tablename.list

使用的时候,仅仅需要修改一下红色标记的用户名、密码、服务器名称、数据库名称即可。

当然你也可以只统计相应的表的行数,修改select name from sysobjects where type='U' order by name,比如:name like 'flow_%' and crdate > '2014-01-01'

另外,如果不想用脚本而在TSQL中实现的话,可能得用到游标了。

脚本tablerowcount.sh下载地址

 

在定义游标时不指定for update 或 for read only,ASE会检查以了解游标是否可更新;

如果游标查询语句中包含order by子句,则ASE会将游标定义为只读;其它情况下定义为可更新游标;

如果不涉及更新或删除表数据的话,建议在游标定义中加上for read only选项,这样ASE将游标定义为只读;

 

表customer在c_custkey列上建有唯一索引,查询表的前10行内容(所有字段拼接成一个字符串),

如果定义游标为for read only:

    declare cur_hash cursor for select top 10 convert(varchar,c_custkey)+coalesce( nullif(isnull(c_name,''), '') , ' ')+coalesce( nullif(isnull(c_address,''),'') , ' ')+convert(varchar,c_nationkey)+coalesce( nullif(isnull(c_phone,''),'') , ' ')+convert(varchar,c_acctbal)+coalesce( nullif(isnull(c_mktsegment,''),'') , ' ')+coalesce( nullif(isnull(c_comment,''),'' ) , ' ') from customer for read only

则使用表扫描返回表的前10行数据;