Yan*_*ang 6 postgresql jdbc prepared-statement
在(说)Python 中,我可以发出:
psycopg2.connect(...).cursor().execute("select * from account where id='00100000006ONCrAAO'")
Run Code Online (Sandbox Code Playgroud)
这在服务器上导致以下日志条目:
2011-07-18 18:56:08 PDT LOG: duration: 6.112 ms statement: select * from account where id='00100000006ONCrAAO'
Run Code Online (Sandbox Code Playgroud)
但是,在 Java 中,发出:
conn.createStatement().executeQuery("select * from account where id = '00100000006ONCrAAO'");
Run Code Online (Sandbox Code Playgroud)
结果是:
2011-07-18 18:44:59 PDT LOG: duration: 4.353 ms parse <unnamed>: select * from account where id = '00100000006ONCrAAO'
2011-07-18 18:44:59 PDT LOG: duration: 0.230 ms bind <unnamed>: select * from account where id = '00100000006ONCrAAO'
2011-07-18 18:44:59 PDT LOG: duration: 0.246 ms execute <unnamed>: select * from account where id = '00100000006ONCrAAO'
Run Code Online (Sandbox Code Playgroud)
一些搜索显示PG JDBC驱动程序总是使用准备好的语句:http : //postgresql.1045698.n5.nabble.com/JDBC-prepared-statements-amp-server-side-prepared-statements-td1919506.html
有没有办法绕过服务器准备好的语句?如果它有所作为,我问的是 PG 8.4 和 9.0。
从 Postgresql JDBC 驱动程序邮件列表中得到答案:
您可以使用 v2 协议(它将参数值作为文本插入,而不是越行发送),但您将失去依赖于 v3 协议的各种其他驱动程序功能。
使用 V2 协议可以强制驱动程序使用简单查询协议而不是扩展查询协议,并将性能恢复到我们在 Python 中看到的水平。