oracle 查詢當天記錄 三種方法效率比較

來源:互聯網
上載者:User

標籤:os   資料   io   for   cti   re   

-- 查詢一表中當天產生的資料-- 原表mobilefrends中的cdate欄位上有索引,建立索引語句是:create index mobilefrends_cdate_idx on mobilefrends(cdate); ----------------------------------------------------------------------------------------------------------------------- 方法一:用to_char()函數 [email protected]> select count(*) from mobilefrends where to_char(cdate,‘yyyy-mm-dd‘)=to_char(sysdate,‘yyyy-mm-dd‘);   COUNT(*)----------     82119 經過時間:  00: 00: 17.18 執行計畫----------------------------------------------------------Plan hash value: 3731074549 ------------------------------------------------------------------------------------------------| Id  | Operation             | Name                   | Rows  | Bytes | Cost (%CPU)| Time     |------------------------------------------------------------------------------------------------|   0 | SELECT STATEMENT      |                        |     1 |     8 | 11773  (16)| 00:02:22 ||   1 |  SORT AGGREGATE       |                        |     1 |     8 |            |          ||*  2 |   INDEX FAST FULL SCAN| MOBILEFRENDS_CDATE_IDX |   129K|  1012K| 11773  (16)| 00:02:22 |------------------------------------------------------------------------------------------------ Predicate Information (identified by operation id):---------------------------------------------------    2 - filter(TO_CHAR(INTERNAL_FUNCTION("CDATE"),‘yyyy-mm-dd‘)=TO_CHAR([email protected]!-1,‘yyyy              -mm-dd‘))  統計資訊----------------------------------------------------------          1  recursive calls          0  db block gets      36896  consistent gets          0  physical reads          0  redo size        345  bytes sent via SQL*Net to client        338  bytes received via SQL*Net from client          2  SQL*Net roundtrips to/from client          0  sorts (memory)          0  sorts (disk)          1  rows processed ----------------------------------------------------------------------------------------------------------------------- 方法二:用trunc()函數 [email protected]> select count(*) from mobilefrends where trunc(cdate)=trunc(sysdate);   COUNT(*)----------     82119 經過時間:  00: 00: 16.32 執行計畫----------------------------------------------------------Plan hash value: 3731074549 ------------------------------------------------------------------------------------------------| Id  | Operation             | Name                   | Rows  | Bytes | Cost (%CPU)| Time     |------------------------------------------------------------------------------------------------|   0 | SELECT STATEMENT      |                        |     1 |     8 | 12580  (22)| 00:02:31 ||   1 |  SORT AGGREGATE       |                        |     1 |     8 |            |          ||*  2 |   INDEX FAST FULL SCAN| MOBILEFRENDS_CDATE_IDX |   129K|  1012K| 12580  (22)| 00:02:31 |------------------------------------------------------------------------------------------------ Predicate Information (identified by operation id):---------------------------------------------------    2 - filter(TRUNC(INTERNAL_FUNCTION("CDATE"))=TRUNC([email protected]!-1))  統計資訊----------------------------------------------------------          0  recursive calls          0  db block gets      36896  consistent gets          0  physical reads          0  redo size        345  bytes sent via SQL*Net to client        338  bytes received via SQL*Net from client          2  SQL*Net roundtrips to/from client          0  sorts (memory)          0  sorts (disk)          1  rows processed ----------------------------------------------------------------------------------------------------------------------- 方法三:不用函數[email protected]> select count(*) from mobilefrends where cdate>=trunc(sysdate) and cdate<trunc(sysdate)+1;   COUNT(*)----------     82119 經過時間:  00: 00: 00.43 執行計畫----------------------------------------------------------Plan hash value: 2668176725 ---------------------------------------------------------------------------------------------| Id  | Operation          | Name                   | Rows  | Bytes | Cost (%CPU)| Time     |---------------------------------------------------------------------------------------------|   0 | SELECT STATEMENT   |                        |     1 |     8 |     3   (0)| 00:00:01 ||   1 |  SORT AGGREGATE    |                        |     1 |     8 |            |          ||*  2 |   FILTER           |                        |       |       |            |          ||*  3 |    INDEX RANGE SCAN| MOBILEFRENDS_CDATE_IDX |    48 |   384 |     3   (0)| 00:00:01 |--------------------------------------------------------------------------------------------- Predicate Information (identified by operation id):---------------------------------------------------    2 - filter(TRUNC([email protected]!-1)<TRUNC([email protected]!))   3 - access("CDATE">=TRUNC([email protected]!-1) AND "CDATE"<TRUNC([email protected]!))  統計資訊----------------------------------------------------------          0  recursive calls          0  db block gets        221  consistent gets          0  physical reads          0  redo size        345  bytes sent via SQL*Net to client        338  bytes received via SQL*Net from client          2  SQL*Net roundtrips to/from client          0  sorts (memory)          0  sorts (disk)          1  rows processed ---------------------------------------------------------------------------------------------------------------------  -- 由上三種方法可見:-- 方法一:用to_char()函數,耗時17.18秒,邏輯讀為36896次,執行的是全索引掃描;-- 方法二:用trunc()函數,耗時16.32秒,邏輯讀也是36896次,執行的也是全索引掃描;-- 方法三:不用函數,耗時0.43秒,邏輯讀為221次,執行的是索引範圍掃描;




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.