shell实现读取较大mysql数据集时的脚本

offset=0
batchsize=$1
while :
do
    mysql -h -P -u -p 库 -e "select * from 表  limit $offset,$batchsize"
    rawcount=$(mysql -h -P -u -p 库 -se "select count(1) from 表)
    if [[ $offset -ge $rawcount ]];then
        break
    fi
    offset=$((offset + batchsize))
done

设置偏移量,一次读取较少的行,通过和查到的表的总行数比较,循环读取所有数据,测试可以读取出来1亿的数据量
使用EOF交互式的查询sql数据
cxsql="select * from table1" mysql -h ip -Pport -uuname -p'upass' << EOF > ./test.csv use 库名 $cxsql EOF

 

posted @ 2023-10-30 14:10  菜菜陈  阅读(42)  评论(0)    收藏  举报