刚开始学习python,对oracle进行简单操作,好奇心之下,比较一下和shell的性能差别,变量控制没有那么严格,都是单线程。
表里100万条数据,先把主键查出来,再根据主键删除掉,计算总共用的时间。
python脚本:
begin=time.clock()
print("begin at:"+str(begin))
con = db.connect(DBUID)
cur=con.cursor()
cur.execute("select serv_id from zhangbo_serv")
res=cur.fetchall()
for i in res:
bind_list={'id':i[0]}
cur.execute("delete from zhangbo_serv where serv_id = :id",bind_list)
con.commit()
end=time.clock()
print("end at :"+str(end))
print("cost:"+str(end-begin))
花费时间:
shell脚本:
get_serv_id()
{
sqlplus $DBUID<<!
select 'xxx delete from zhangbo_serv where serv_id = '||serv_id from zhangbo_serv;
exit;
!
}
getTiming()
{
start=$1
end=$2
start_s=$(echo $start | cut -d '.' -f 1)
start_ns=$(echo $start | cut -d '.' -f 2)
end_s=$(echo $end | cut -d '.' -f 1)
end_ns=$(echo $end | cut -d '.' -f 2)
time=$(( ( 10#$end_s - 10#$start_s ) * 1000 + ( 10#$end_ns / 1000000 - 10#$start_ns / 1000000 ) ))
echo "$time ms"
}
begin=$(date +%s.%N)
echo "begin at:" $begin
echo "sqlplus $DBUID<<!">./delete.sh
get_serv_id|grep ^xxx|awk '{print$2,$3,$4,$5,$6,$7,$8}'>>./delete.sh
echo "commit;">>./delete.sh
echo "exit;">>./delete.sh
echo "!">>./delete.sh
sh delete.sh
end=$(date +%s.%N)
echo "end at:" $end
runtime=$(getTiming $begin $end)
echo "cost:"$runtime
花费时间:
虽然shell里其他一些东西没有严格控制,也占用了一定的时间,但是,差了这么大的数量级,其他的完全可以忽略了。而且,shell需要将select出来的sql语句重定向到新的shell脚本再执行,明显复杂的多。看来,用python还是很有必要的