游标共享之cursor_sharing=similar

      cursor_sharing=similar   ,exact和force折中的参数。当探测到谓词会导致执行计划的改变,就会重新解析,否则不会。
      适用场景:在无法将应用的代码修改为绑定变量情况下,oracle提供的又一种解决方法,similar的好处是最大限度地避免CBO在绑定变量的情况下做出的错误的判断,但它的代价是分析的次数变大。

       参数还有两种选择:

        游标共享之cursor_sharing=force
        游标共享之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                           FORCE
SQL> create table t as select * from dba_objects;
SQL> Create index ind_t_object_id on t(object_id) nologging;
SQL> exec dbms_stats.gather_table_stats(user,'t',cascade => true);
SQL> alter session set cursor_sharing=similar;
SQL> Set autotrace traceonly
SQL> select /*+test_similar*/* from t where object_id<10;
已选择8行。
执行计划
----------------------------------------------------------
Plan hash value: 1630734149
----------------------------------------------------------------------------------------------
| Id  | Operation                   | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                 |     7 |   651 |     3   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T               |     7 |   651 |     3   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IND_T_OBJECT_ID |     7 |       |     2   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("OBJECT_ID"<10)
统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          5  consistent gets
          0  physical reads
          0  redo size
       1788  bytes sent via SQL*Net to client
        385  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          8  rows processed
SQL> select /*+test_similar*/* from t where object_id<100000;
已选择50675行。
执行计划
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      | 50675 |  4602K|   149   (1)| 00:00:02 |
|*  1 |  TABLE ACCESS FULL| T    | 50675 |  4602K|   149   (1)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("OBJECT_ID"<100000)
统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
       4042  consistent gets
          0  physical reads
          0  redo size
    2460611  bytes sent via SQL*Net to client
      37543  bytes received via SQL*Net from client
       3380  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      50675  rows processed

----当探测到执行计划会改变时,则会重新解析
SQL> Set autotrace off
SQL> alter system flush shared_pool;
SQL> Set autotrace traceonly
SQL> select /*+test_similar*/* from t where object_id<10;
已选择8行。
执行计划
----------------------------------------------------------
Plan hash value: 1630734149
-----------------------------------------------------------------------------------------------
| Id  | Operation                   | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                 |     7 |   651 |     3   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T               |     7 |   651 |     3   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IND_T_OBJECT_ID |     7 |       |     2   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("OBJECT_ID"<10)
统计信息
----------------------------------------------------------
        941  recursive calls
          0  db block gets
        149  consistent gets
          0  physical reads
          0  redo size
       1788  bytes sent via SQL*Net to client
        385  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
         10  sorts (memory)
          0  sorts (disk)
          8  rows processed
SQL> select /*+test_similar*/* from t where object_id<100000;
已选择50675行。
执行计划
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      | 50675 |  4602K|   149   (1)| 00:00:02 |
|*  1 |  TABLE ACCESS FULL| T    | 50675 |  4602K|   149   (1)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("OBJECT_ID"<100000)
统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
       4042  consistent gets
          0  physical reads
          0  redo size
    2460611  bytes sent via SQL*Net to client
      37543  bytes received via SQL*Net from client
       3380  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      50675  rows processed

SQL> Set autotrace off
SQL> col sql_text format a80;
SQL> select sql_id,child_number,sql_text from v$sql where sql_text like '%/*+test_similar*/%'
  2  and  sql_text not like '%sql_text%'
  3  And  sql_text not like '%EXPLAIN PLAN%';
SQL_ID        CHILD_NUMBER SQL_TEXT
------------- ------------ ---------------------------------------------------------------------------
d5101w24qr4hk            0 select /*+test_similar*/* from t where object_id<:"SYS_B_0"
d5101w24qr4hk            1 select /*+test_similar*/* from t where object_id<:"SYS_B_0"



----当探测到执行计划不会改变时,则不会重新解析
SQL> alter system flush shared_pool;
SQL> Set autotrace traceonly
SQL> select /*+test_similar*/* from t where object_name='aaa';
执行计划
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     2 |   186 |   149   (1)| 00:00:02 |
|*  1 |  TABLE ACCESS FULL| T    |     2 |   186 |   149   (1)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("OBJECT_NAME"='aaa')
统计信息
----------------------------------------------------------
        941  recursive calls
          0  db block gets
        846  consistent gets
          0  physical reads
          0  redo size
        992  bytes sent via SQL*Net to client
        374  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
         10  sorts (memory)
          0  sorts (disk)
          0  rows processed


SQL> select /*+test_similar*/* from t where object_name='bbb';
执行计划
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     2 |   186 |   149   (1)| 00:00:02 |
|*  1 |  TABLE ACCESS FULL| T    |     2 |   186 |   149   (1)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("OBJECT_NAME"='bbb')
统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        702  consistent gets
          0  physical reads
          0  redo size
        992  bytes sent via SQL*Net to client
        374  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed


SQL> select /*+test_similar*/* from t where object_name='ccc';
执行计划
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     2 |   186 |   149   (1)| 00:00:02 |
|*  1 |  TABLE ACCESS FULL| T    |     2 |   186 |   149   (1)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("OBJECT_NAME"='ccc')
统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        702  consistent gets
          0  physical reads
          0  redo size
        992  bytes sent via SQL*Net to client
        374  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed


SQL> select /*+test_similar*/* from t where object_name='ddd';
执行计划
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     2 |   186 |   149   (1)| 00:00:02 |
|*  1 |  TABLE ACCESS FULL| T    |     2 |   186 |   149   (1)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("OBJECT_NAME"='ddd')
统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        702  consistent gets
          0  physical reads
          0  redo size
        992  bytes sent via SQL*Net to client
        374  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed

SQL> Set autotrace off
SQL> select sql_id,child_number,sql_text from v$sql where sql_text like '%/*+test_similar*/%'
   and  sql_text not like '%sql_text%'
    And  sql_text not like '%EXPLAIN PLAN%';
SQL_ID        CHILD_NUMBER SQL_TEXT
------------- ------------ ---------------------------------------------------------------------------
2uff8qc1h3jyx            0 select /*+test_similar*/* from t where object_name=:"SYS_B_0"

  • 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、付费专栏及课程。

余额充值