游标共享之cursor_sharing=EXACT

        cursor sharing用来告诉oracle什么情况下可以共享游标,即SQL重用。oracle默认cursor_sharing 是exact  指的是SQL语句必须绝对一样的情况下才能共享游标,否则作为新的SQL语句处理。
        适合场景:使用exact 高效的前提是应用代码中使用了绑定变量,是最好的实践方式,这也是oracle推荐的。

        参数还有两种选择:

        游标共享之cursor_sharing=similar
        游标共享之cursor_sharing=force

SQL> Drop table t purge;

表已删除。

SQL> show parameter cursor_sharing;
NAME                                                                  TYPE        VALUE
------------------------------------                            ----------- ------------------------------
_optimizer_extended_cursor_sharing          string      UDO

cursor_sharing                                                  string      EXACT


SQL> create table t as select * from dba_objects;
SQL> alter system flush shared_pool;

SQL> select /*+test_exact*/count(1) from t where object_id=100;
  COUNT(1)
----------
         1
SQL> select /*+test_exact*/count(1) from t where object_id=200;
  COUNT(1)
----------
         1

SQL> col sql_text format a80;

----可以看到谓词不同则产生两条解析的SQL

SQL> select sql_text from v$sql where sql_text like '%/*+test_exact*/%'   and  sql_text not like '%sql_text%';
SQL_TEXT
--------------------------------------------------------------------------------
select /*+test_exact*/count(1) from t where object_id=100
select /*+test_exact*/count(1) from t where object_id=200


SQL> alter system flush shared_pool;
SQL> var x number;
SQL> exec :x:=100;
SQL> select /*+test_exact*/count(1) from t where object_id=:x;
  COUNT(1)
----------
         1

SQL> exec :x:=200;
SQL> select /*+test_exact*/count(1) from t where object_id=:x;
  COUNT(1)
----------
         1
----在使用绑定变量的情况下只产生一次硬解析
SQL> select sql_text from v$sql where sql_text like '%/*+test_exact*/%'   and  sql_text not like '%sql_text%';
SQL_TEXT
--------------------------------------------------------------------------------
select /*+test_exact*/count(1) from t where object_id=:x
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import pymysql import time while True: # 连接源数据库 src_conn = pymysql.connect(host='10.43.64.110', port=3306, user='selectuser', password='Xy@123456', database='messpdb') print("连接源数据库成功") # 连接目标数据库 dst_conn = pymysql.connect(host='10.43.144.231', port=3306, user='root', password='123456', database='czjsc') print("连接目标数据库成功") # 创建源游标对象 src_cursor = src_conn.cursor() # 创建目标游标对象 dst_cursor = dst_conn.cursor() # 编写SQL查询语句 #各牌号烟丝总重量 sql1 = 'select mat_id,material_name ,ROUND (sum(quantity) ,1 ) weight ,unit_id from messpdb.silk_stock a left join messpdb.maindata_material b on a.mat_id =b.ctrl where mat_id >0 group by mat_id,material_name, unit_id' sql2 = "select ROUND (sum(quantity) ,1 ) weight ,unit_id from messpdb.silk_stock a where mat_id >0 group by unit_id" sql3 = "select aa.ids,ROUND (aa.c1/bb.c2 ,3 )*100 from (select 1 as ids,count(box_code) c1 from messpdb.silk_stock a where mat_id >0 group by ids) aa, (select 1 as ids,count(box_code) c2 from messpdb.silk_stock a group by ids)bb where aa.ids=bb.ids" # 执行SQL查询语句 src_cursor.execute(sql1) src_cursor.execute(sql2) src_cursor.execute(sql3) # 获取查询结果 results1 = src_cursor.fetchall() results2 = src_cursor.fetchall() results3 = src_cursor.fetchall() print("查询数据库成功") # 更新数据到目标数据库 for row in results1: mat_id = row[0] material_name = row[1] weight = row[2] unit_id = row[3] # 将数据更新到目标数据库中 update_sql = "update cs_list set material_name=%s, weight=%s, unit_id=%s where mat_id=%s" dst_cursor.execute(update_sql, (material_name, weight, unit_id, mat_id)) print("更新数据成功") # 更新数据到目标数据库 for row in results2: weight = row[0] unit_id = row[1] # 将数据更新到目标数据库中 update_sql = "update cs2_list set weight=%s, unit_id=%s where id=1" dst_cursor.execute(update_sql, (weight, unit_id)) for row in results3: ids = row[0] stock_ratio = row[1] # 将数据更新到目标数据库中 update_sql = "update cs3_list set stock_ratio=%s where id=1" dst_cursor.execute(update_sql, stock_ratio) # 提交事务并关闭连接 dst_conn.commit() dst_cursor.close() src_cursor.close() dst_conn.close() src_conn.close() print("断开数据库连接成功") time.sleep(1) 帮我debug
最新发布
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值