Oracle中針對中文進行排序

在oracle 9i以前,對中文的排序,是默認按2進制編碼來進行排序的. 9i時增長了幾種新的選擇:sql

  1. 按中文拼音進行排序:SCHINESE_PINYIN_M
  2. 按中文部首進行排序:SCHINESE_RADICAL_M
  3. 按中文筆畫進行排序:SCHINESE_STROKE_M

而oracle 9i是對中文的排序是默認按拼音排序(並非指NLS_SORT = SCHINESE_PINYIN_M,而是說SQL中不指定NLS_SORT時對中文列排序時默認按拼音)的,跟以前的2進制編碼排序有所不一樣.具體用法以下:數據庫

  1. 直接寫在sql中,例如:
    1. SELECT * FROM TEAM ORDER BY NLSSORT(排序字段名,'NLS_SORT = SCHINESE_PINYIN_M');
    2. SELECT * FROM TEAM ORDER BY NLSSORT(排序字段名,'NLS_SORT = SCHINESE_STROKE_M');
    3. SELECT * FROM TEAM ORDER BY NLSSORT(排序字段名,'NLS_SORT = SCHINESE_RADICAL_M');
  2. 配置在初始化參數NLS_SORT中,這能夠在數據庫建立時指定,也能夠經過alter session來修改.若是是前者,則在全部session中生效.例如:
    1. 使用select * from NLS_SESSION_PARAMETERS;語句能夠看到NLS_SORT的值.
    2. 更改配置文件:alter system set nls_sort='SCHINESE_PINYIN_M' scope=spfile;
    3. 更改session:alter SESSION set NLS_SORT = SCHINESE_PINYIN_M;

 這裏要額外注意一下性能問題,按oracle官方文檔的解釋,oracle在對中文列創建索引時,是按照2進制編碼進行排序的,因此若是NLS_SORT被設置爲BINARY時,排序則能夠利用索引.若是不是2進制排序,而是使用上面介紹的3種針對中文的特殊排序,則oracle沒法使用索引,會進行全表掃描.這點必定要注意,多用plsql工具比較一下執行效率.解決方法是,在此列上創建linguistic index.例如:CREATE INDEX nls_index ON my_table (NLSSORT(name, 'NLS_SORT = SCHINESE_PINYIN_M'));session

如下是oracle文檔中的原文:oracle

Note:
Setting NLS_SORT to anything other than BINARY causes a sort to use a full table scan, regardless of the path chosen by the optimizer. BINARY is the exception because indexes are built according to a binary order of keys. Thus the optimizer can use an index to satisfy the ORDER BY clause when NLS_SORT is set to BINARY. If NLS_SORT is set to any linguistic sort, the optimizer must include a full table scan and a full sort in the execution plan.less

相關文章
相關標籤/搜索