游标共享之cursor_sharing=force

        cursor sharing=force , oracle将2条类似的SQL的谓词用一个变量代替,同时将它们看做同一条SQL语句处理。这种方式很粗暴,极可能造成执行计划的不准确。
        适用场景:在无法将应用的代码修改为绑定变量情况下,oracle提供的一种解决方法。

        注意:游标共享特性有个不太好的名声,就是它不是很稳定,过去数年,大量与之相关的bug被发现和确认。

         参数还有两种选择:

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

        下面来做个试验:

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 session set cursor_sharing=force;
SQL> alter system flush shared_pool;
SQL> select /*+test_force*/count(1) from t where object_id=100;
  COUNT(1)
----------
         1
SQL> select /*+test_force*/count(1) from t where object_id=100;
  COUNT(1)
----------
         1
SQL> select /*+test_force*/count(1) from t where object_id=200;
  COUNT(1)
----------
         1

SQL> col sql_text format a80;

----谓词不同的情况下只解析一次
SQL> select sql_id,child_number,sql_text from v$sql where sql_text like '%/*+test_force*/%'
  2  and  sql_text not like '%sql_text%';
SQL_ID        CHILD_NUMBER SQL_TEXT
------------- ------------ --------------------------------------------------------------------------------
f3jksnvgk93w9            0 select /*+test_force*/count(:"SYS_B_0") from t where object_id=:"SYS_B_1"

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值