OR导致笛卡尔积

系统 1499 0

近期监控数据库,发现以下语句跑得很慢,原来运行计划走了导致笛卡尔积,来看以下语句:

      SQL> explain plan for
  2  SELECT COUNT(*)
  3    FROM "GD_FS"."TZZ_SJ_DEV_DISC_79073" "A2",
  4         "GD_FS"."TZZ_SJ_DEVELOP_MONTH"  "A1"
  5   WHERE "A1"."SERV_ID" = "A2"."SERV_ID"
  6     AND "A1"."STAT_DATE" = "A2"."STAT_DATE"
  7     AND "A2"."ALLO_DISC_TYPE_3" LIKE '%租机%'
  8      OR "A2"."ALLO_DISC_TYPE_3" LIKE '%零预存%';

Explained.

SQL> @getplan
'general,outline,starts'

Enter value for plan type:

PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------

Plan hash value: 1410945947

------------------------------------------------------------------------------------------------
| Id  | Operation              | Name                  | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |                       |     1 |    42 |   271M  (2)|999:59:59 |
|   1 |  SORT AGGREGATE        |                       |     1 |    42 |            |          |
|   2 |   CONCATENATION        |                       |       |       |            |          |
|   3 |    MERGE JOIN CARTESIAN|                       |    82G|  3239G|   271M  (2)|999:59:59 |
|*  4 |     TABLE ACCESS FULL  | TZZ_SJ_DEV_DISC_79073 | 22606 |   640K|   743   (2)| 00:00:11 |
|   5 |     BUFFER SORT        |                       |  3663K|    45M|   271M  (2)|999:59:59 |
|   6 |      TABLE ACCESS FULL | TZZ_SJ_DEVELOP_MONTH  |  3663K|    45M| 11994   (2)| 00:02:48 |
|*  7 |    HASH JOIN           |                       |     1 |    42 | 12762   (2)| 00:02:59 |
|*  8 |     TABLE ACCESS FULL  | TZZ_SJ_DEV_DISC_79073 | 21476 |   608K|   743   (2)| 00:00:11 |
|   9 |     TABLE ACCESS FULL  | TZZ_SJ_DEVELOP_MONTH  |  3663K|    45M| 11996   (2)| 00:02:48 |
------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - filter("A2"."ALLO_DISC_TYPE_3" LIKE '%零预存%')
   7 - access("A1"."SERV_ID"="A2"."SERV_ID" AND "A1"."STAT_DATE"="A2"."STAT_DATE")
   8 - filter("A2"."ALLO_DISC_TYPE_3" LIKE '%租机%' AND LNNVL("A2"."ALLO_DISC_TYPE_3"
              LIKE '%零预存%'))

    

--表大小的情况:

      OWNER                SEGMENT_NAME                   SEGMENT_TYPE           Size(Mb)
-------------------- ------------------------------ -------------------- ----------
GD_FS                TZZ_SJ_DEV_DISC_79073          TABLE                        43
GD_FS                TZZ_SJ_DEVELOP_MONTH           TABLE                   577.375
    

两个表并不大,一个43M,一个577M,这里肯定是开发者不小心,写错了语句,导致跑不出来。
AND "A2"."ALLO_DISC_TYPE_3" LIKE '%租机%'  OR "A2"."ALLO_DISC_TYPE_3" LIKE '%零预存%';改成
AND ("A2"."ALLO_DISC_TYPE_3" LIKE '%租机%'  OR "A2"."ALLO_DISC_TYPE_3" LIKE '%零预存%');
改动语句后,问题解决。


改写后:

      SQL> explain plan for
  2  SELECT COUNT(*)
  3    FROM "GD_FS"."TZZ_SJ_DEV_DISC_79073" "A2",
  4         "GD_FS"."TZZ_SJ_DEVELOP_MONTH"  "A1"
  5   WHERE "A1"."SERV_ID" = "A2"."SERV_ID"
  6     AND "A1"."STAT_DATE" = "A2"."STAT_DATE"
  7     AND ("A2"."ALLO_DISC_TYPE_3" LIKE '%租机%' OR "A2"."ALLO_DISC_TYPE_3" LIKE '%零预存%');

Explained.

SQL> @getplan
'general,outline,starts'

Enter value for plan type:

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------

Plan hash value: 3104770780

---------------------------------------------------------------------------------------------
| Id  | Operation           | Name                  | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                       |     1 |    42 | 12765   (2)| 00:02:59 |
|   1 |  SORT AGGREGATE     |                       |     1 |    42 |            |          |
|*  2 |   HASH JOIN         |                       | 44081 |  1808K| 12765   (2)| 00:02:59 |
|*  3 |    TABLE ACCESS FULL| TZZ_SJ_DEV_DISC_79073 | 44081 |  1248K|   745   (3)| 00:00:11 |
|   4 |    TABLE ACCESS FULL| TZZ_SJ_DEVELOP_MONTH  |  3663K|    45M| 11996   (2)| 00:02:48 |
---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("A1"."SERV_ID"="A2"."SERV_ID" AND "A1"."STAT_DATE"="A2"."STAT_DATE")
   3 - filter("A2"."ALLO_DISC_TYPE_3" LIKE '%租机%' OR "A2"."ALLO_DISC_TYPE_3" LIKE
              '%零预存%')

    



OR导致笛卡尔积


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论