关于PostgreSQL绑定变量的问题 [英] question about postgresql bind variables

查看:911
本文介绍了关于PostgreSQL绑定变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看问题,并决定尝试使用绑定变量。我用

I was looking at the question and decided to try using the bind variables. I use

sql = 'insert into abc2 (interfield,textfield) values (%s,%s)'
a = time.time()
for i in range(10000):
    #just a wrapper around cursor.execute
    db.executeUpdateCommand(sql,(i,'test'))

db.commit()

sql = 'insert into abc2 (intfield,textfield) values (%(x)s,%(y)s)'
for i in range(10000):
    db.executeUpdateCommand(sql,{'x':i,'y':'test'})

db.commit()

看两套时间,上面似乎并没有太大的时差。实际上,第二个需要更长的时间。如果我在某个地方犯了错误,有人可以纠正我吗?在这里使用psycopg2。

Looking at the time taken for the two sets, above it seems like there isn't much time difference. In fact, the second one takes longer. Can someone correct me if I've made a mistake somewhere? using psycopg2 here.

推荐答案

查询在Postgresql中是等效的。

The queries are equivalent in Postgresql.

绑定是Oracle行话使用时,它将保存查询计划,因此下一次执行会更快一些。 准备在Postgres中做同样的事情。

Bind is oracle lingo. When you use it will save the query plan so the next execution will be a little faster. prepare does the same thing in Postgres.

http://www.postgresql.org/docs/current/static/sql-prepare.html

psycopg2支持内部绑定,而不是使用 cursor.executemany() prepare >和 cursor.execute()

psycopg2 supports an internal 'bind', not prepare with cursor.executemany() and cursor.execute()

(但不要将其绑定到pg上,请准备。否则他们可能不知道你的意思:)

(But don't call it bind to pg people. Call it prepare or they may not know what you mean:)

这篇关于关于PostgreSQL绑定变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆