1. 程式人生 > >Oracle中針對中文進行排序

Oracle中針對中文進行排序

session ica tro dex 生效 cep exception 進制 pinyin

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

  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‘));

以下是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.

Oracle中針對中文進行排序