当前位置: 首页 > news >正文

Oracle笔记:测试update语句关联表扫描的次数

我们的文章会在微信公众号IT民工的龙马人生和博客网站( www.htz.pw )同步更新 ,欢迎关注收藏,也欢迎大家转载,但是请在文章开始地方标注文章出处,谢谢!
由于博客中有大量代码,通过页面浏览效果更佳。

Oracle笔记:测试update语句关联表扫描的次数

    下面是测试一下update语句执行时,与更新表关联的表被扫描的次数,也是为什么我们常常将update语句,更改为merge into或者是pl/sql的方式来实现

欢迎大家加入ORACLE超级群:17115662 免费解决各种ORACLE问题,以后BLOG将迁移到http://www.htz.pw

1,数据库版本

www.htz.pw > select * from v$version;BANNER——————————————————————————–Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 – 64bit ProductionPL/SQL Release 11.2.0.4.0 – ProductionCORE    11.2.0.4.0      ProductionTNS for Linux: Version 11.2.0.4.0 – ProductionNLSRTL Version 11.2.0.4.0 – Production

2,创建测试表

www.htz.pw > insert into scott.htz select * from scott.htz;690192 rows created.www.htz.pw > commit;Commit complete.www.htz.pw > select count(*) from scott.htz;COUNT(*)———-1380384www.htz.pw > update scott.htz1 set owner=’HTZ’;86275 rows updated.www.htz.pw > commit;Commit complete.www.htz.pw > create index scott.ind_htz2_object_id on scott.htz1(object_id,object_type,owner);Index created.

3,update更新表

www.htz.pw > update /*+ gather_plan_statistics */scott.htz a set (a.object_type,a.owner) = (select object_type,owner from scott.htz1 b where  a.object_id=b.object_id);1380384 rows updated.Elapsed: 00:00:29.40Execution Plan———————————————————-Plan hash value: 932534721—————————————————————————————–| Id  | Operation          | Name               | Rows  | Bytes | Cost (%CPU)| Time     |—————————————————————————————–|   0 | UPDATE STATEMENT   |                    |  1442K|    56M|    11M (25)| 38:28:25 ||   1 |  UPDATE            | HTZ                |       |       |            |          ||   2 |   TABLE ACCESS FULL| HTZ                |  1442K|    56M|  5416   (1)| 00:01:05 ||*  3 |   INDEX RANGE SCAN | IND_HTZ2_OBJECT_ID |   886 | 36326 |     3   (0)| 00:00:01 |—————————————————————————————–Predicate Information (identified by operation id):—————————————————3 – access("B"."OBJECT_ID"=:B1)Note—–– dynamic sampling used for this statement (level=2)Statistics———————————————————-270  recursive calls1419046  db block gets1631433  consistent gets4781  physical reads382985292  redo size846  bytes sent via SQL*Net to client913  bytes received via SQL*Net from client3  SQL*Net roundtrips to/from client1  sorts (memory)0  sorts (disk)1380384  rows processed

这里看到逻辑读是相当的高

下面查看一下htz1被扫描的次数

www.htz.pw > @find_sqlwww.htz.pw > set echo offEnter value for sql_text: gather_plan_statisticsEnter value for sql_id:SQL_ID              CHILD HASH_VALUE  PLAN_HASH      EXECS         ETIME     AVG_ETIME USERNAME—————— —— ———- ———- ———- ————- ————- ————-SQLTEXT———————————————————————————————————————————————————————————————-219bgfbrqx1ck           0 4016997778  932534721          1         36.51         36.51 SYSupdate /*+ gather_plan_statistics */scott.htz a set (a.object_type,a.owner) = (select object_type,owner from scott.htz1 b where  a.object_id=b.object_id)www.htz.pw > @plan_by_last.sqlEnter value for sqlid: 219bgfbrqx1ckPLAN_TABLE_OUTPUT——————————————————————————————————————————————————————————————————–SQL_ID  219bgfbrqx1ck, child number 0————————————-update /*+ gather_plan_statistics */scott.htz a set(a.object_type,a.owner) = (select object_type,owner from scott.htz1 bwhere  a.object_id=b.object_id)Plan hash value: 932534721————————————————————————————————————| Id  | Operation          | Name               | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |————————————————————————————————————|   0 | UPDATE STATEMENT   |                    |      1 |        |      0 |00:00:36.51 |    3050K|  19226 ||   1 |  UPDATE            | HTZ                |      1 |        |      0 |00:00:36.51 |    3050K|  19226 ||   2 |   TABLE ACCESS FULL| HTZ                |      1 |   1442K|   1380K|00:00:20.62 |   19674 |  18876 ||*  3 |   INDEX RANGE SCAN | IND_HTZ2_OBJECT_ID |   1365K|    886 |   1365K|00:00:07.04 |    1611K|    344 |————————————————————————————————————

这里看到了HTZ1表上面的索引扫描了1365K次range scan,每次是将a.object_id的值传送给htz1的object_id

Predicate Information (identified by operation id):—————————————————3 – access("B"."OBJECT_ID"=:B1)Note—–– dynamic sampling used for this statement (level=2)26 rows selected.

4,更改为merge into语句

www.htz.pw > MERGE INTO /*+ gather_plan_statistics */2            scott.htz a3       USING (SELECT b.object_type, b.owner, b.object_id4                FROM scott.htz1 b) c5          ON (a.object_id = c.object_id)6  WHEN MATCHED7  THEN8     UPDATE SET a.object_type = c.object_type, a.owner = c.owner;1380384 rows merged.Elapsed: 00:00:32.20Execution Plan———————————————————-Plan hash value: 3787432690——————————————————————————————————| Id  | Operation               | Name               | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |——————————————————————————————————|   0 | MERGE STATEMENT         |                    |  1272K|    67M|       | 21499   (1)| 00:04:18 ||   1 |  MERGE                  | HTZ                |       |       |       |            |          ||   2 |   VIEW                  |                    |       |       |       |            |          ||*  3 |    HASH JOIN            |                    |  1272K|   315M|  4584K| 21499   (1)| 00:04:18 ||   4 |     INDEX FAST FULL SCAN| IND_HTZ2_OBJECT_ID | 88560 |  3545K|       |    99   (0)| 00:00:02 ||   5 |     TABLE ACCESS FULL   | HTZ                |  1442K|   301M|       |  5423   (1)| 00:01:06 |——————————————————————————————————Predicate Information (identified by operation id):—————————————————3 – access("A"."OBJECT_ID"="B"."OBJECT_ID")Note—–– dynamic sampling used for this statement (level=2)Statistics———————————————————-309  recursive calls1419444  db block gets20518  consistent gets6175  physical reads382998348  redo size846  bytes sent via SQL*Net to client1034  bytes received via SQL*Net from client3  SQL*Net roundtrips to/from client3  sorts (memory)0  sorts (disk)1380384  rows processedSQL_ID  153haxsb6d8p1, child number 1————————————-MERGE INTO /*+ gather_plan_statistics */           scott.htz aUSING (SELECT b.object_type, b.owner, b.object_id               FROMscott.htz1 b) c         ON (a.object_id = c.object_id) WHEN MATCHEDTHEN    UPDATE SET a.object_type = c.object_type, a.owner = c.ownerPlan hash value: 3787432690——————————————————————————————————————————————–| Id  | Operation               | Name               | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |——————————————————————————————————————————————–|   0 | MERGE STATEMENT         |                    |      1 |        |      0 |00:00:43.86 |    1439K|   5757 |       |       |          ||   1 |  MERGE                  | HTZ                |      1 |        |      0 |00:00:43.86 |    1439K|   5757 |       |       |          ||   2 |   VIEW                  |                    |      1 |        |   1380K|00:00:11.45 |   20025 |   5754 |       |       |          ||*  3 |    HASH JOIN            |                    |      1 |   1272K|   1380K|00:00:08.89 |   20025 |   5754 |  7628K|  3091K| 7690K (0)||   4 |     INDEX FAST FULL SCAN| IND_HTZ2_OBJECT_ID |      1 |  88560 |  86275 |00:00:00.15 |     351 |    162 |       |       |          ||   5 |     TABLE ACCESS FULL   | HTZ                |      1 |   1442K|   1380K|00:00:01.95 |   19674 |   5592 |       |       |          |——————————————————————————————————————————————–Predicate Information (identified by operation id):—————————————————3 – access("A"."OBJECT_ID"="B"."OBJECT_ID")Note—–– dynamic sampling used for this statement (level=2)

这里能明显的看到HTZ1表上面的索引,只扫描了一次,逻辑读已经下降到20518了。

如果更新的表行数越多的时候,效果就会越明显

------------------作者介绍-----------------------
姓名:黄廷忠
现就职:Oracle中国高级服务团队
曾就职:OceanBase、云和恩墨、东方龙马等
电话、微信、QQ:18081072613
个人博客: (http://www.htz.pw)
CSDN地址: (https://blog.csdn.net/wwwhtzpw)
博客园地址: (https://www.cnblogs.com/www-htz-pw)

http://www.wxhsa.cn/company.asp?id=6581

相关文章:

  • ​​电流互感器选型指南:以普科科技产品为例
  • .NET驾驭Word之力:玩转文本与格式
  • 读书笔记:白话解读位图索引:什么时候该用,什么时候千万别用?
  • 泰克CT-6交流电流探头测量原理
  • 结构体成员赋值问题
  • RepositoryItemGridLookUpEdit 使用 ok
  • wso2~系统端口总结
  • 故障处理:19C RAC改私网IP后重建集群时报网络找不到
  • 谈谈程序猿的职业方向
  • Flash Attention详解
  • eclipse插件调用保护后的jar包流程
  • 通义上线 FunAudio-ASR,噪声场景幻觉率降 70%;盒智科技推出 AI 口语练习陪伴设备 Lookee 丨日报
  • reLeetCode 热题 100-11 盛最多的谁 - MKT
  • Markdown语法学习
  • AI 视频生成网站 Viddo AI 的 SEO 分析和优化建议
  • k3s 离线部署流程(内网环境)
  • GPS简单模拟
  • C# Avalonia 15- Animation- XamlAnimation
  • 多个表格汇总到一个表格不同的sheet,vba宏
  • python读取Excel表合并单元格以及清除空格符
  • 算法作业第一周
  • 域名购买方案
  • Anby_の模板题集
  • AI 编程的“最后一公里”:当强大的代码生成遇上模糊的需求
  • ctfshowWeb应用安全与防护(第四章)wp
  • 创建sshkey并链接git
  • 使用bash脚本检测网站SSL证书是否过期 - sherlock
  • Python 2025:低代码开发与自动化运维的新纪元 - 教程
  • 为什么Claude Code放弃代码索引,使用50年前的grep技术
  • 【QT】使用QT编写一款自己的串口助手