成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

【書評(píng):Oracle查詢優(yōu)化改寫】第二章

【書評(píng):Oracle查詢優(yōu)化改寫】第二章

創(chuàng)新互聯(lián)建站專注于耿馬網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供耿馬營銷型網(wǎng)站建設(shè),耿馬網(wǎng)站制作、耿馬網(wǎng)頁設(shè)計(jì)、耿馬網(wǎng)站官網(wǎng)定制、成都小程序開發(fā)服務(wù),打造耿馬網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供耿馬網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

BLOG文檔結(jié)構(gòu)圖

【書評(píng):Oracle查詢優(yōu)化改寫】第二章

在上一篇中http://blog.itpub.net/26736162/viewspace-1652985/,我們主要分析了一些單表查詢的時(shí)候需要注意的內(nèi)容,今天第二章也很簡單,主要是關(guān)于排序方面的內(nèi)容,以下貼出第二章的內(nèi)容:

第 2 章 給查詢結(jié)果排序

2.1 以指定的次序返回查詢結(jié)果

2.2 按多個(gè)字段排序

2.3 按子串排序

2.4 TRANSLATE

2.5 按數(shù)字和字母混合字符串中的字母排序

2.6 處理排序空值

2.7 根據(jù)條件取不同列中的值來排序

排序基本上沒有什么可以講的,不過書中著重介紹了下translate的用法。

一.1 translate用法

語法:TRANSLATE(char, from, to)

用法:

1. 返回將出現(xiàn)在from中的每個(gè)字符替換為to中的相應(yīng)字符以后的字符串。

2. 若from比to字符串長,那么在from中比to中多出的字符將會(huì)被刪除,或者認(rèn)為from中多出的字符在to中與空對(duì)應(yīng)

3. 三個(gè)參數(shù)中有一個(gè)是空,返回值也將是空值。

09:43:50 SQL>  select translate('abcdefga','abc','wo')  from dual;

TRANSLA

-------

wodefgw

Elapsed: 00:00:00.14

09:43:57 SQL>  select translate('abcdefga','abc','')  from dual;

T

-

Elapsed: 00:00:00.00

SELECT translate('ab 你好 bcadefg','abcdefg','1234567'),translate('ab 你好 bcadefg','1abcdefg','1') FROM dual;

【書評(píng):Oracle查詢優(yōu)化改寫】第二章

一.2 按數(shù)字和字母混合字符串中的字母排序,采用translate函數(shù)來實(shí)現(xiàn)

09:52:01 SQL> create or replace view v as select empno || ' '||ename as data from scott.emp;

View created.

Elapsed: 00:00:00.54

09:52:07 SQL> select * from V

09:52:15   2  ;

DATA

---------------------------------------------------

9000 lastwiner

9001 lastwiner

7369 SMITH

7499 ALLEN

7521 WARD

7566 JONES

7654 MARTIN

7698 BLAKE

7782 CLARK

7788 SCOTT

7839 KING

7844 TURNER

7876 ADAMS

7900 JAMES

7902 FORD

7934 MILLER

16 rows selected.

Elapsed: 00:00:00.20

09:55:07 SQL> select data,translate(data,'- 0123456789','-') from V order by 2;

DATA                                                TRANSLATE(DATA,'-0123456789','-')

--------------------------------------------------- ------------------------------------------------------------------------------------------------------

7876 ADAMS                                          ADAMS

7499 ALLEN                                          ALLEN

7698 BLAKE                                          BLAKE

7782 CLARK                                          CLARK

7902 FORD                                           FORD

7900 JAMES                                          JAMES

7566 JONES                                          JONES

7839 KING                                           KING

7654 MARTIN                                         MARTIN

7934 MILLER                                         MILLER

7788 SCOTT                                          SCOTT

7369 SMITH                                          SMITH

7844 TURNER                                         TURNER

7521 WARD                                           WARD

9001 lastwiner                                      lastwiner

9000 lastwiner                                      lastwiner

16 rows selected.

Elapsed: 00:00:00.10

09:55:33 SQL>

一.3 關(guān)于order by 排序的優(yōu)化

關(guān)于SQL優(yōu)化中有一個(gè)原則叫:避免使用耗費(fèi)資源的操作(DISTINCT、UNION、MINUS、INTERSECT、ORDER BY、group by、SMJ、created index)

帶有DISTINCT,UNION,MINUS,INTERSECT,ORDER BY的SQL語句會(huì)啟動(dòng)SQL引擎執(zhí)行耗費(fèi)資源的排序(SORT)功能. DISTINCT需要一次排序操作, 而其他的至少需要執(zhí)行兩次排序.

例如,一個(gè)UNION查詢,其中每個(gè)查詢都帶有GROUP BY子句, GROUP BY會(huì)觸發(fā)嵌入排序(NESTED SORT) ; 這樣, 每個(gè)查詢需要執(zhí)行一次排序, 然后在執(zhí)行UNION時(shí), 又一個(gè)唯一排序(SORT UNIQUE)操作被執(zhí)行而且它只能在前面的嵌入排序結(jié)束后才能開始執(zhí)行. 嵌入的排序的深度會(huì)大大影響查詢的效率.

通常, 帶有UNION, MINUS , INTERSECT的SQL語句都可以用其他方式重寫.

ORDER BY語句決定了Oracle如何將返回的查詢結(jié)果排序。Order by語句對(duì)要排序的列沒有什么特別的限制,也可以將函數(shù)加入列中(象聯(lián)接或者附加等)。任何在Order by語句的非索引項(xiàng)或者有計(jì)算表達(dá)式都將降低查詢速度。

仔細(xì)檢查order by語句以找出非索引項(xiàng)或者表達(dá)式,它們會(huì)降低性能。解決這個(gè)問題的辦法就是重寫order by語句以使用索引,也可以為所使用的列建立另外一個(gè)索引,同時(shí)應(yīng)絕對(duì)避免在order by子句中使用表達(dá)式。

●在頻繁進(jìn)行排序或分組(即進(jìn)行g(shù)roup by或order by操作)的列上建立索引。

●如果待排序的列有多個(gè),可以在這些列上建立復(fù)合索引(compound index)。

磁盤排序的開銷是很大的,有幾個(gè)方面的原因。首先,和內(nèi)存排序相比較,它們特別慢;而且磁盤排序會(huì)消耗臨時(shí)表空間中的資源。Oracle還必須分配緩沖池塊來保持臨時(shí)表空間中的塊。無論什么時(shí)候,內(nèi)存排序都比磁盤排序好,磁盤排序?qū)?huì)令任務(wù)變慢,并且會(huì)影響Oracle實(shí)例的當(dāng)前任務(wù)的執(zhí)行。還有,過多的磁盤排序?qū)?huì)令free buffer waits的值變高,從而令其它任務(wù)的數(shù)據(jù)塊由緩沖中移走。

一.3.1 總結(jié)

(1)采用索引避免排序:排序數(shù)據(jù)較多時(shí)

(2)去掉不必要的distinct,很多distinct是由于程序員對(duì)數(shù)據(jù)的了解不自信而多加的。

總而言之,排序是非常耗費(fèi)CPU資源的,能不排序就不要排序,如果非得排序,可以考慮在排序列上建立合適的索引。

記得之前有個(gè)SQL,不加排序的話,秒級(jí)可以出結(jié)果,即響應(yīng)速度很快,但是加上排序后得5或6分鐘才可以,看了下是結(jié)果集很大,又得排序造成的。

這里簡單舉個(gè)例子吧:

一.3.2 例子

[oracle@rhel6_lhr ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Thu May 14 10:55:26 2015

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

With the Partitioning, Automatic Storage Management, OLAP, Data Mining

and Real Application Testing options

10:55:26 SQL> conn lhr/lhr

Connected.

12:08:08 SQL> create table test_index_lhr as select * from dba_objects;

Table created.

Elapsed: 00:00:03.70

12:08:27 SQL> insert into test_index_lhr select * from test_index_lhr;

77241 rows created.

12:08:39 SQL> commit;

Commit complete.

Elapsed: 00:00:00.00

12:08:41 SQL> set autot traceonly explain stat

12:08:41 SQL> select  object_name from test_index_lhr where object_name is not null order by object_name;

154482 rows selected.

Elapsed: 00:00:01.18

Execution Plan

----------------------------------------------------------

Plan hash value: 1466335622

---------------------------------------------------------------------------------------------

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

---------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT   |                |   155K|     9M| |  3078   (1)| 00:00:37 |

|   1 |  SORT ORDER BY     |                |   155K|     9M|    10M|  3078   (1)| 00:00:37 |

|*  2 |   TABLE ACCESS FULL| TEST_INDEX_LHR |   155K|     9M| |   623   (1)| 00:00:08 |

---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   2 - filter("OBJECT_NAME" IS NOT NULL)

Note

-----

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

Statistics

----------------------------------------------------------

         10  recursive calls

          6  db block gets

       2808  consistent gets

        614  physical reads

      34996  redo size

    3787521  bytes sent via SQL*Net to client

     113801  bytes received via SQL*Net from client

      10300  SQL*Net roundtrips to/from client

         1  sorts (memory)

          0  sorts (disk)

     154482  rows processed

12:08:48 SQL> create index ind_test_inde on test_index_lhr(object_name) ;

Index created.

Elapsed: 00:00:02.45

12:08:58 SQL> EXEC dbms_stats.gather_table_stats(ownname => 'LHR', tabname=> 'test_index_lhr',   cascade => TRUE );

PL/SQL procedure successfully completed.

Elapsed: 00:00:02.62

12:09:04 SQL> select  object_name from test_index_lhr where object_name is not null order by object_name;

154482 rows selected.

Elapsed: 00:00:01.35

Execution Plan

----------------------------------------------------------

Plan hash value: 712275200

----------------------------------------------------------------------------------

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

----------------------------------------------------------------------------------

|   0 | SELECT STATEMENT |               |   154K|  3771K|   766   (1)| 00:00:10 |

|*  1 |  INDEX FULL SCAN | IND_TEST_INDE |   154K|  3771K|   766   (1)| 00:00:10 |

----------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   1 - filter("OBJECT_NAME" IS NOT NULL)

Note

-----

   - SQL plan baseline "SQL_PLAN_8kcy12j8f3s5n2a6f2b1f" used for this statement

Statistics

----------------------------------------------------------

        704  recursive calls

         64  db block gets

      11715  consistent gets

         37  physical reads

      33236  redo size

    3787521  bytes sent via SQL*Net to client

     113801  bytes received via SQL*Net from client

      10300  SQL*Net roundtrips to/from client

0  sorts (memory)

          0  sorts (disk)

     154482  rows processed

12:09:09 SQL> select  owner, object_name from test_index_lhr where object_name is not null order by object_name;

154482 rows selected.

Elapsed: 00:00:01.28

Execution Plan

----------------------------------------------------------

Plan hash value: 1466335622

---------------------------------------------------------------------------------------------

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

---------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT   |                |   154K|  4676K|       |  1947   (1)| 00:00:24 |

|   1 |  SORT ORDER BY     |                |   154K|  4676K|  6072K|  1947   (1)| 00:00:24 |

|*  2 |   TABLE ACCESS FULL| TEST_INDEX_LHR |   154K|  4676K|       |   623   (1)| 00:00:08 |

---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   2 - filter("OBJECT_NAME" IS NOT NULL)

Statistics

----------------------------------------------------------

          6  recursive calls

          6  db block gets

       2241  consistent gets

        895  physical reads

        680  redo size

    4232382  bytes sent via SQL*Net to client

     113801  bytes received via SQL*Net from client

      10300  SQL*Net roundtrips to/from client

          1  sorts (memory)

          0  sorts (disk)

     154482  rows processed

12:09:22 SQL> select /*+index(a,IND_TEST_INDE)*/ owner,  object_name from test_index_lhr a where object_name is not null order by object_name;

154482 rows selected.

Elapsed: 00:00:09.59

Execution Plan

----------------------------------------------------------

Plan hash value: 880046030

----------------------------------------------------------------------------------------------

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

----------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT            |                |   154K|  4676K|   109K  (1)| 00:21:57 |

|   1 |  TABLE ACCESS BY INDEX ROWID| TEST_INDEX_LHR |   154K|  4676K|   109K  (1)| 00:21:57 |

|*  2 |   INDEX FULL SCAN           | IND_TEST_INDE  |   154K|       |   766   (1)| 00:00:10 |

----------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   2 - filter("OBJECT_NAME" IS NOT NULL)

Statistics

----------------------------------------------------------

          6  recursive calls

          4  db block gets

     122955  consistent gets

       2198  physical reads

        724  redo size

    4392715  bytes sent via SQL*Net to client

     113801  bytes received via SQL*Net from client

      10300  SQL*Net roundtrips to/from client

  0  sorts (memory)

          0  sorts (disk)

     154482  rows processed

12:14:23 SQL>

相關(guān)連接:

【書評(píng):Oracle查詢優(yōu)化改寫】第一章 http://blog.itpub.net/26736162/viewspace-1652985/

...........................................................................................................................................................................................

本文作者:小麥苗,只專注于數(shù)據(jù)庫的技術(shù),更注重技術(shù)的運(yùn)用

ITPUB BLOG:http://blog.itpub.net/26736162

本文地址:http://blog.itpub.net/26736162/viewspace-1654252/

本文pdf版:http://yunpan.cn/QCwUAI9bn7g7w  提取碼:af2d

QQ:642808185 注明:ITPUB的文章標(biāo)題

<版權(quán)所有,文章允許轉(zhuǎn)載,但須以鏈接方式注明源地址,否則追究法律責(zé)任!>

...........................................................................................................................................................................................

網(wǎng)站欄目:【書評(píng):Oracle查詢優(yōu)化改寫】第二章
瀏覽地址:http://jinyejixie.com/article8/gpgoip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、全網(wǎng)營銷推廣響應(yīng)式網(wǎng)站、用戶體驗(yàn)、域名注冊外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司
石嘴山市| 永清县| 抚顺市| 蒲城县| 西城区| 通渭县| 桂平市| 象山县| 盐城市| 昌平区| 凌云县| 晋州市| 肥城市| 湟中县| 安泽县| 崇义县| 丁青县| 土默特左旗| 赤城县| 周宁县| 蒲江县| 彭山县| 银川市| 墨玉县| 同德县| 察雅县| 米泉市| 台中县| 昌宁县| 铜川市| 西青区| 大埔区| 泸西县| 修水县| 阜城县| 城市| 肃北| 潮安县| 龙江县| 红河县| 虹口区|