shell脚本查询mysql

来源:互联网 发布:mac右键菜单设置 编辑:程序博客网 时间:2024/05/18 03:44

接到统计需求,需要对一批对一批uid过滤,只保留10月1日之后注册的uid,用户注册时间可以从mysql里面拿,写了个shell脚本
ps:用户信息使用十库十表存储,像 12345这个uid存储在5这个库中的4这个表里面

 #! /bin/bashhostip="107.0.0.1"port=6304username="net_budao_root"pwd="123456"database="db_bd"first_login_time="2017-10-01"#构造用户信息表,用户信息使用十库十表function getDbTb(){    first=${1:0-1:1}    second=${1:0-2:1}    echo "db_budao_"$first".t_user_"$second}#查询这个uid是否存在function exist(){    tb=`getDbTb $1`    sql="select count(1) from $tb where uid = $1 and from_unixtime(first_login_time) > \"$first_login_time\""    res=(`mysql -h10.25.68.144 -P6304 -unet_budao_root -ppbVo9teu -Ddb_bd -e "$sql" | awk 'NR>1'`) # NR>1 去掉查询的第一行    echo $res}for uid in $(cat $1)do    res=`exist $uid`    if [ $res -eq 1 ]    then        echo $uid >> $1".dump"        #   else        #       echo "false"    fidone

有个缺点,查询的时候有点慢,可能每次查询都需要连接一次数据库的原因,哪位大牛指点一下有没有好点的方式~