2011-02-24 95 views
1

我有一个shell脚本,它从文件中逐行读取用户标识,并将记录插入到数据库中。然而插入过程很慢,我想通过分批进行优化。由于事务大小超过专有限制,因此无法将整个文件作为一个批处理使用。这里是脚本:ksh批处理

for user in `cat $userlist` 
do 
echo "processing user $user" 
{ 
    echo "begin" 
     i=1 
     while [[ $i -le 30 ]] ; do 
     echo "insert into usertab values ($user,-1,\"\",-1)" 
     i=$(expr $i + 1) 
     done 
     echo "commit" 
} | propSql userDb - 
done 

开始和提交是这个数据库的标准关键词。在这里,我不是一次处理一个用户,而是一次处理10个用户。有人可以建议需要改变吗?

回答

1

这显示了一次累积用户10的方式。您将需要对其进行修改以适应您需要数据查找insert声明的方式。我离开了内部循环,但将其更改为for循环,因为这使得所有控制都在一个地方发生。尽管你可能不需要那个循环。

process() { 
    # Somewhere in here you'll use the value of "[email protected]" 
    echo "begin" 
     # you may not need this loop any more 
     for ((i=1; i<30; i++)) 
     do 
      echo "insert into usertab values ($user,-1,\"\",-1)" 
     done 
    echo "commit" 
} 

j=0 
while read -r user 
do 
    echo "processing user $user" 
    if ! ((j++ % 10)) # every tenth user, do a database operation 
    then 
     process "$accum" | propSql userDb - 
     accum="" 
    fi 
    accum+=" $user" # accumulate user names 
done < "$userlist" 
process "$accum" | propSql userDb - # one more time to get the remainder 

请注意,这是使用ksh93语法编写的。如果您使用的是其他版本,则需要进行一些修改。