【发布时间】:2021-10-06 03:32:26
【问题描述】:
当我使用此处找到的绑定变量方法时:https://cx-oracle.readthedocs.io/en/latest/user_guide/bind.html#bind 在这里:Python cx_Oracle bind variables 我的查询大约需要 8 分钟,但当我使用硬编码值(文字)时,大约需要 20 秒。
我正在努力理解“幕后”(变量/内存访问/数据传输/查询解析)发生的事情,看看是否有任何方法可以让我坚持使用绑定变量的推荐方法并获取大约 20 秒的性能。
这个python脚本会自动化,值是动态的,所以我绝对不能使用硬编码的值。
技术背景:Python 3.6;甲骨文 11g; cx_Oracle 8
----python部分代码-----
第一个版本
param_dict = {“startDate:”01-Jul-21”, “endDate:”31-Jul-2021”}
conn = (typical database connection code….)
cur = conn.cursor()
###### this query has the bind variables and param_dict keys match bind variable aliases; runtime ~480s (8mins)
cur_df = pandas.DataFrame(cur.execute("inserted_query_here", param_dict))
第二版
conn = (typical database connection code….)
cur = conn.cursor()
###### this query has the hardcoded values (literals); runtime ~20s
cur_df = pandas.DataFrame(cur.execute("inserted_query_here"))
【问题讨论】:
-
向我们展示您在这两种情况下使用的代码。
-
嗨,John,您想查看代码的 SQL 部分、python 部分还是两者兼而有之?
-
我认为仅 Python 部分就足够了。
-
@JohnGordon,我已按要求使用代码更新了原始帖子。想法?
-
感谢版面编辑@Luke Woodward。伙计们,我可能不得不将此线程移至 Oracle 部分 :-( 我直接在 SQL Developer 中运行更多测试并且我得到相同的结果(绑定与文字性能),所以这可能不是 cx_Oracle/Python完全没有问题。我不是 DBA,也没有权限查看数据库级别的计算统计信息,但这是我在当前研究的大部分文章中看到的。