新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Oracle中where条件执行顺序是什么

这篇文章将为大家详细讲解有关Oracle中where条件执行顺序是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联公司专注于中大型企业的网站设计制作、成都网站制作和网站改版、网站营销服务,追求商业策划与数据分析、创意艺术与技术开发的融合,累计客户近千家,服务满意度达97%。帮助广大客户顺利对接上互联网浪潮,准确优选出符合自己需要的互联网运用,我们将一直专注品牌网站建设和互联网程序开发,在前进的路上,与客户一起成长!

问题:

  1. SYS@proc> create table t as select * from v$parameter;

  2. Table created.

  3. SYS@proc> select value from t where name='db_block_size' and to_number(value)=8192;

  4. VALUE

  5. --------------------------------------------------------------------------------

  6. 8192

  7. SYS@proc> select value from v$parameter where name='db_block_size' and to_number(value)=8192;

  8. select value from v$parameter where name='db_block_size' and to_number(value)=8192

  9.                                                              *

  10. ERROR at line 1:

  11. ORA-01722: invalid number

为什么语句“select value from t where name='db_block_size' and to_number(value)=8192;”执行成功,换成v$parameter却报错。

实验研究过程:

  1. SYS@proc> set autotrace on

  2. SYS@proc> analyze table t compute statistics;

  3. Table analyzed.

  4. SYS@proc> select value from t where name='db_block_size' and to_number(value)=8192;

  5. VALUE

  6. --------------------------------------------------------------------------------

  7. 8192

  8. Execution Plan

  9. ----------------------------------------------------------

  10. Plan hash value: 1601196873

  11. --------------------------------------------------------------------------

  12. | Id | Operation         | Name  | Rows | Bytes | Cost (%CPU)| Time     |

  13. --------------------------------------------------------------------------

  14. |  0 | SELECT STATEMENT  |       |    1 |    26 |       4 (0)| 00:00:01 |

  15. |* 1 |  TABLE ACCESS FULL| T     |    1 |    26 |       4 (0)| 00:00:01 |

  16. --------------------------------------------------------------------------

  17. Predicate Information (identified by operation id):

  18. ---------------------------------------------------

  19.    1 - filter("NAME"='db_block_size' AND TO_NUMBER("VALUE")=8192)

  20. Statistics

  21. ----------------------------------------------------------

  22.       1  recursive calls

  23.       0  db block gets

  24.       9  consistent gets

  25.       0  physical reads

  26.       0  redo size

  27.    525  bytes sent via SQL*Net to client

  28.    523  bytes received via SQL*Net from client

  29.       2  SQL*Net roundtrips to/from client

  30.       0  sorts (memory)

  31.       0  sorts (disk)

  32.       1 rows processed

一开始看到这个执行计划很懵逼,完全搞不懂为什么能够执行成功,做10046,10053,改写sql加hint还是搞不懂。
最后猜想Oracle在filter("NAME"='db_block_size' AND TO_NUMBER("VALUE")=8192)这个步骤,是先对数据做name='db_block_size'的过滤,在做to_number('value')=8192的过滤。
若是能将谓词信息改变成filter(TO_NUMBER("VALUE") AND  "NAME"='db_block_size'=8192)并且执行报错,那么猜想就是正确的。
尝试将sql语句的and条件调换位置"select value from t whereto_number(value)=8192  and  name='db_block_size';",不过还是和原来一样,这里省略步骤。

这里构造其他测试表:

  1. SYS@proc> create table a(id1 int,id2 int,id3 int,id4 int);

  2. Table created.

  3. SYS@proc> insert into a values(1,1,1,0);

  4. 1 row created.

  5. SYS@proc> commit;

  6. Commit complete.

  7. SYS@proc> select * from a;

  8.        ID1         ID2            ID3        ID4

  9. ---------- ---------- ---------- ----------

  10.          1             1                1           0

这里执行以下4条sql语句:
①Select 'ok' From aaa where id1/id2=1 and id3/id4=2;
②Select 'ok' From aaa where id1/id2=2 and id3/id4=2;
③Select 'ok' From aaa where id3/id4=2 and id1/id2=1;
④Select 'ok' From aaa where id3/id4=2 and id1/id2=2;
其中①和③,②和④只是where后条件位置互换而已。
查看执行结果:

  1. SYS@proc> Select 'ok' From aaa where id1/id2=1 and id3/id4=2;

  2. Select 'ok' From aaa where id1/id2=1 and id3/id4=2

  3.                                             *

  4. ERROR at line 1:

  5. ORA-01476: divisor is equal to zero

  6. SYS@proc> Select 'ok' From aaa where id1/id2=2 and id3/id4=2;

  7. no rows selected

  8. SYS@proc> Select 'ok' From aaa where id3/id4=2 and id1/id2=1;

  9. Select 'ok' From aaa where id3/id4=2 and id1/id2=1

  10.                               *

  11. ERROR at line 1:

  12. ORA-01476: divisor is equal to zero

  13. SYS@proc> Select 'ok' From aaa where id3/id4=2 and id1/id2=2;

  14. Select 'ok' From aaa where id3/id4=2 and id1/id2=2

  15.                               *

  16. ERROR at line 1:

  17. ORA-01476: divisor is equal to zero

②和④只是位置不同,但是一个却正常执行,一个却报错了。
这里查看两条sql的执行计划:

  1. SYS@proc> explain plan for Select 'ok' From aaa where id1/id2=2 and id3/id4=2;

  2. Explained.

  3. SYS@proc> select * from table(dbms_xplan.display());

  4. PLAN_TABLE_OUTPUT

  5. --------------------------------------------------------------------------------

  6. Plan hash value: 864433273

  7. -----------------------------------------------------------------------

  8. | Id | Operation         | Name | Rows | Bytes | Cost (%CPU)| Time     |

  9. ------------------------------------------------------------------------

  10. | 0  | SELECT STATEMENT  |      |    1 |    12 |     2   (0)| 00:00:01 |

  11. |* 1 |  TABLE ACCESS FULL| AAA  |    1 |    12 |     2   (0)| 00:00:01 |

  12. -----------------------------------------------------------------------

  13. Predicate Information (identified by operation id):

  14. ---------------------------------------------------

  15. PLAN_TABLE_OUTPUT

  16. --------------------------------------------------------------------------------

  17.    1 - filter("ID1"/"ID2"=2 AND "ID3"/"ID4"=2)

  18. 13 rows selected.

  19. SYS@proc> explain plan for Select 'ok' From aaa where id3/id4=2 and id1/id2=2;

  20. Explained.

  21. SYS@proc> select * from table(dbms_xplan.display());

  22. PLAN_TABLE_OUTPUT

  23. --------------------------------------------------------------------------------

  24. Plan hash value: 864433273

  25. ------------------------------------------------------------------------

  26. | Id | Operation         | Name | Rows | Bytes | Cost (%CPU)| Time     |

  27. ------------------------------------------------------------------------

  28. |  0 | SELECT STATEMENT  |      |    1 |    12 |     2   (0)| 00:00:01 |

  29. |* 1 |  TABLE ACCESS FULL|  AAA |    1 |    12 |     2   (0)| 00:00:01 |

  30. ------------------------------------------------------------------------

  31. Predicate Information (identified by operation id):

  32. ---------------------------------------------------

  33. PLAN_TABLE_OUTPUT

  34. --------------------------------------------------------------------------------

  35.    1 - filter("ID3"/"ID4"=2 AND "ID1"/"ID2"=2)

  36. 13 rows selected.

这里对比谓词信息刚好是两个位置不同,导致执行结果不一样。
正好说明上边的问题的猜想:
    最后猜想Oracle在filter("NAME"='db_block_size' AND TO_NUMBER("VALUE")=8192)这个步骤,是先对数据做name='db_block_size'的过滤,在做to_number('value')=8192的过滤。
    若是能将谓词信息改变成filter(TO_NUMBER("VALUE") AND  "NAME"='db_block_size'=8192)并且执行报错,那么猜想就是正确的。

由此问题解决。

其他:

  1. SYS@proc> create table test (id int);

  2. Table created.

  3. SYS@proc> insert into test values(null);

  4. 1 row created.

  5. SYS@proc> commit;

  6. Commit complete.

  7. SYS@proc> select * from test;

  8.     ID

  9. ----------

  10. SYS@proc> set autotrace on

  11. SYS@proc> select value from t,test a where a.id||name='db_block_size' and to_number(a.id||t.value)=8192;

  12. VALUE

  13. --------------------------------------------------------------------------------

  14. 8192

  15. Execution Plan

  16. ----------------------------------------------------------

  17. Plan hash value: 423998170

  18. ---------------------------------------------------------------------------

  19. | Id | Operation          | Name  | Rows | Bytes | Cost (%CPU)| Time     |

  20. ---------------------------------------------------------------------------

  21. |  0 | SELECT STATEMENT   |       |    1 |    39 |    6    (0)| 00:00:01 |

  22. |  1 |  NESTED LOOPS      |       |    1 |    39 |    6    (0)| 00:00:01 |

  23. |  2 |   TABLE ACCESS FULL| TEST  |    1 |    13 |    2    (0)| 00:00:01 |

  24. |* 3 |   TABLE ACCESS FULL| T     |    1 |    26 |    4    (0)| 00:00:01 |

  25. ---------------------------------------------------------------------------

  26. Predicate Information (identified by operation id):

  27. ---------------------------------------------------

  28.    3 - filter(TO_CHAR("A"."ID")||"NAME"='db_block_size' AND

  29.      TO_NUMBER(TO_CHAR("A"."ID")||"T"."VALUE")=8192)

  30. Note

  31. -----

  32.    - dynamic sampling used for this statement (level=2)

  33. Statistics

  34. ----------------------------------------------------------

  35.      32  recursive calls

  36.       0  db block gets

  37.      28  consistent gets

  38.       0  physical reads

  39.       0  redo size

  40.     525  bytes sent via SQL*Net to client

  41.     523  bytes received via SQL*Net from client

  42.       2  SQL*Net roundtrips to/from client

  43.       4  sorts (memory)

  44.       0  sorts (disk)

  45.       1 rows processed

  46. SYS@proc> set autotrace off

  47. SYS@proc> select value from t,test a where to_number(a.id||t.value)=8192 and a.id||name='db_block_size';

  48. select value from t,test a where to_number(a.id||t.value)=8192 and a.id||name='db_block_size'

  49.                                                 *

  50. ERROR at line 1:

  51. ORA-01722: invalid number

  52. SYS@proc> explain plan for select value from t,test a where to_number(a.id||t.value)=8192 and a.id||name='db_block_size';

  53. Explained.

  54. SYS@proc> select * from table(dbms_xplan.display());

  55. PLAN_TABLE_OUTPUT

  56. --------------------------------------------------------------------------------

  57. Plan hash value: 423998170

  58. ---------------------------------------------------------------------------

  59. | Id | Operation          | Name  | Rows | Bytes | Cost (%CPU)| Time     |

  60. ---------------------------------------------------------------------------

  61. |  0 | SELECT STATEMENT   |       |    1 |    39 |    6    (0)| 00:00:01 |

  62. |  1 |  NESTED LOOPS      |       |    1 |    39 |    6    (0)| 00:00:01 |

  63. |  2 |   TABLE ACCESS FULL| TEST   |    1 |    13 |    2    (0)| 00:00:01 |

  64. |* 3 |   TABLE ACCESS FULL| T     |    1 |    26 |    4    (0)| 00:00:01 |

  65. ---------------------------------------------------------------------------

  66. PLAN_TABLE_OUTPUT

  67. --------------------------------------------------------------------------------

  68. Predicate Information (identified by operation id):

  69. ---------------------------------------------------

  70.    3 - filter(TO_NUMBER(TO_CHAR("A"."ID")||"T"."VALUE")=8192 AND

  71.      TO_CHAR("A"."ID")||"NAME"='db_block_size')

  72. 16 rows selected.


所以where后边条件的执行顺序,实际上和执行计划谓词信息的顺序有关,和where的位置无关。
网上有些在10g做实验得出结论是从右到左,在11g里边,按照相同步骤执行并得不出相同结论。

问题延伸:
filter("NAME"='db_block_size' AND TO_NUMBER("VALUE")=8192),这里是一次性扫描出全部数据在进行过滤,还是一行一行获取在判断的。
延伸链接:http://blog.itpub.net/30174570/viewspace-2149212/

关于Oracle中where条件执行顺序是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享标题:Oracle中where条件执行顺序是什么
转载注明:http://wjwzjz.com/article/gshosj.html
在线咨询
服务热线
服务热线:028-86922220
TOP