Oracle的NLS_COMP和NLS_SORT參數

來源:互聯網
上載者:User

NLS_COMP和NLS_SORT參數
Oracle預設是採用binary進行排序,這對於例如中文的排序來說,是不恰當的。
使用這兩個參數可以指定排序的方法,比如拼音或是,要注意可能會引起效能問題。
解決方案是使用NLSSORT函數來建立一個函數索引。

NLS_COMP = { BINARY | LINGUISTIC | ANSI }
BINARY
Normally, comparisons in the WHERE clause and in PL/SQL blocks is binary unless you specify theNLSSORT function.
LINGUISTIC
Comparisons for all SQL operations in the WHERE clause and in PL/SQL blocks should use the linguistic sort specified in theNLS_SORT parameter. To improve the performance, you can also define a linguistic index on the column for which you want linguistic comparisons.
ANSI
A setting of ANSI is for backwards compatibility; in general, you should setNLS_COMP to LINGUISTIC


NLS_SORT = { BINARY | linguistic_definition
}
NLS_SORT specifies the collating sequence for character value comparison in various SQL operators and clauses, for example, ORDER BY, GROUP BY, comparison conditions (=, <>, <=, >=), IN, BETWEEN, LIKE, MIN/MAX, GREATEST/LEAST, and INSTR.

If the value is BINARY, then comparison is based directly on byte values in the binary encoding of the character values being compared. The ordering depends on the character set of the compared values, which is either the database character set (for VARCHAR2, CHAR, LONG, and CLOB) or the national character set (for NVARCHAR2, NCHAR, and NCLOB).

If the value is a named linguistic sort, then comparison is defined by this sort. A linguistic sort uses various rules to achieve ordering expected by speakers of one or more natural languages. This is usually the same ordering that is used in dictionaries and/or telephone directories in those languages.

The BINARY comparison is faster and uses less resources than any linguistic comparison but for text in a natural language, it does not provide ordering expected by users.
The value of NLS_SORT affects execution plans of queries. Because a standard index cannot be used as a source of values sorted in a linguistic order, an explicit sort operation must usually be performed instead of an index range scan. A functional index on the NLSSORT function may be defined to provide values sorted in a linguistic order and reintroduce the index range scan to the execution plan.

下面做個測試:
首先來看看可用的中文排序方法:
select value from v$nls_valid_values where parameter='SORT' and value like '%SCHINESE%';
返回可用的中文排序方法
SCHINESE_PINYIN_M    -- 按照拼音排序
SCHINESE_STROKE_M -- 按照筆劃(第一順序)、部首(第二順序)排序
SCHINESE_RADICAL_M -- 按照部首(第一順序)、筆劃(第二順序)排序

下面來做測試。
create index emp_ename_idx on emp(ename);
現在察看select * from emp where ename='Mike';的執行計畫,
explain plan for select * from emp where ename='Mike';
select * from table(dbms_xplan.display);
可以看到oracle會走索引emp_ename_idx。

  1. ---------------------------------------------------------------------------------------------   
  2. | Id  | Operation                   | Name          | Rows  | Bytes | Cost (%CPU)| Time     |  
  3. ---------------------------------------------------------------------------------------------   
  4. |   0 | SELECT STATEMENT            |               |     1 |    46 |     2   (0)| 00:00:01 |  
  5. |   1 |  TABLE ACCESS BY INDEX ROWID| EMP           |     1 |    46 |     2   (0)| 00:00:01 |  
  6. |*  2 |   INDEX RANGE SCAN          | EMP_ENAME_IDX |     1 |       |     1   (0)| 00:00:01 |  
  7. ---------------------------------------------------------------------------------------------   
  8.   
  9. Predicate Information (identified by operation id):  
  10. ---------------------------------------------------   
  11.   
  12.    2 - access("ENAME"=U'Mike')  
alter session set nls_comp='LINGUISTIC';
alter session set nls_sort='SCHINESE_PINYIN_M';
之後再查看執行計畫,得到結果:
  1. --------------------------------------------------------------------------   
  2. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  3. --------------------------------------------------------------------------   
  4. |   0 | SELECT STATEMENT  |      |     1 |    46 |     3   (0)| 00:00:01 |  
  5. |*  1 |  TABLE ACCESS FULL| EMP  |     1 |    46 |     3   (0)| 00:00:01 |  
  6. --------------------------------------------------------------------------   
  7.   
  8. Predicate Information (identified by operation id):  
  9. ---------------------------------------------------   
  10.   
  11.    1 - filter(NLSSORT("ENAME",'nls_sort=''SCHINESE_PINYIN_M''')=HEXTORAW  
  12.               ('0230021B022301FE0000020202020007020202') )  
可以看到,現在走全表掃描了。
接下來,如果建立如下的索引
create index emp_ename_idx_nlssort on emp(nlssort(ename,'NLS_SORT=SCHINESE_PINYIN_M'));
再查看執行計畫,發現又走索引emp_ename_idx_nlssort了。
  1. -----------------------------------------------------------------------------------------------------   
  2. | Id  | Operation                   | Name                  | Rows  | Bytes | Cost (%CPU)| Time     |  
  3. -----------------------------------------------------------------------------------------------------   
  4. |   0 | SELECT STATEMENT            |                       |     1 |    46 |     2   (0)| 00:00:01 |  
  5. |   1 |  TABLE ACCESS BY INDEX ROWID| EMP                   |     1 |    46 |     2   (0)| 00:00:01 |  
  6. |*  2 |   INDEX RANGE SCAN          | EMP_ENAME_IDX_NLSSORT |     1 |       |     1   (0)| 00:00:01 |  
  7. -----------------------------------------------------------------------------------------------------   
  8.   
  9. Predicate Information (identified by operation id):  
  10. ---------------------------------------------------   
  11.   
  12.    2 - access(NLSSORT("ENAME",'nls_sort=''SCHINESE_PINYIN_M''')=HEXTORAW('0230021B022301FE000  
  13.               0020202020007020202') )  

聯繫我們

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