Oracle執行計畫——all_rows和first_rows(n) 最佳化器模式

來源:互聯網
上載者:User
0. 環境建立
SQL> create usertest identified by test  2 default tablespace users  3 temporary tablespace temp  4 quota unlimited on users;User created.SQL> grant createsession, resource, alter session to test;Grant succeeded.SQL> conntest/test;Connected.SQL> create tableemp(id number, name varchar2(10));Table created.SQL> insert intoemp values(100, 'tom');1 row created.SQL> insert intoemp values(200, 'mike');1 row created.SQL> insert intoemp values(300, 'jack');1 row created.SQL> insert intoemp values(400, 'rose');1 row created.SQL> commit;Commit complete.
1. 資料少時的情況 1.1 FIRST_ROWS_10
SQL> setautotrace traceonly;SQL> executedbms_stats.gather_table_stats('TEST', 'emp', cascade=>true);PL/SQL proceduresuccessfully completed.SQL> altersession set optimizer_mode=first_rows_10;Session altered.

1.2 ALL_ROWS
SQL> alter sessionset optimizer_mode=all_rows;Session altered.

1.3 分析

通過上面的簡單舉例比較,我們可以看到,在表上沒有索引,當資料量很少,並且值唯一的情況下,兩種模式的表現是一樣的

2. 資料少時的情況
SQL> create indexemp_idx on emp(name);Index created.SQL> setautotrace offSQL> insert intoemp select * from emp;4 rows created.SQL> insert intoemp select * from emp;8 rows created.SQL> /16 rows created.SQL> /32 rows created.SQL> /64 rows created.SQL> /128 rows created.SQL> /256 rows created.SQL> /512 rows created.SQL> /1024 rows created.SQL> /2048 rows created.SQL> /4096 rows created.SQL> /8192 rows created.SQL> /16384 rows created.SQL> /32768 rows created.SQL> commit;Commit complete.SQL> executedbms_stats.gather_table_stats('TEST', 'emp', cascade=>true);PL/SQL proceduresuccessfully completed.

2.1 FIRST_ROWS_10

SQL> setautotrace traceonlySQL> altersession set optimizer_mode=first_rows_10;Session altered.

2.2 ALL_ROWS
SQL> setautotrace traceonlySQL> altersession set optimizer_mode=all_rows;Session altered.

2.3 分析

我們看到fisrt_rows走了索引,這顯然不是一種理想的結果,而all_rows走了全表掃描,我們可以看到成本明顯更低。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.