0

0

Oracle的NLS_COMP和NLS_SORT参数

php中文网

php中文网

发布时间:2016-06-07 17:59:14

|

1813人浏览过

|

来源于php中文网

原创

NLS_COMP和NLS_SORT参数 Oracle默认是采用binary进行排序,这对于例如中文的排序来说,是不恰当的。 使用这两个参数可以指定排序的方法,比如拼音或是,要注意可能会引起性能问题。 解决方法是使用NLSSORT函数来建立一个函数索引。 NLS_COMP = { BINARY | LIN

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') )  

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

65

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

43

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

35

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

41

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

204

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

9

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

8

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
SQL 教程
SQL 教程

共61课时 | 3.2万人学习

Java 教程
Java 教程

共578课时 | 40.3万人学习

oracle知识库
oracle知识库

共0课时 | 0人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号