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 -- 按照部首(第一顺序)、笔划(第二顺序)排序

========================

NLS_SORT specifies the collating sequence for ORDER BY queries.

  • If the value is BINARY, then the collating sequence for ORDER BY queries is based on the numeric value of characters (a binary sort that requires less system overhead).
  • If the value is a named linguistic sort, sorting is based on the order of the defined linguistic sort. Most (but not all) languages supported by the NLS_LANGUAGE parameter also support a linguistic sort with the same name.

    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.

You must use the NLS_SORT operator with comparison operations if you want the linguistic sort behavior.

根据上文中标红部分的注视,如果NLS_SORT不是设置为"Binary",那么就会引起全表扫描,是不会使用索引的.

NLS_COMP specifies the collation behavior of the database session.

Values:

  • BINARY

Normally, comparisons in the WHERE clause and in PL/SQL blocks is binary unless you specify the NLSSORT function.

  • LINGUISTIC

Comparisons for all SQL operations in the WHERE clause and in PL/SQL blocks should use the linguistic sort specified in the NLS_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 set NLS_COMP to LINGUISTIC

《FROM:http://www.linuxidc.com/Linux/2012-09/70931.htm http://www.cnblogs.com/yanrongpi/archive/2008/04/11/1149144.html

时间: 2024-10-14 04:57:54

NLS_COMP和NLS_SORT参数的相关文章

oracle函数详解

Oracle 数据库中 SQL 分析的主要优势 Oracle 数据库中分析功能和特性提供以下主要优势: 1. 提高开发人员产能 — 开发人员可以通过更清晰.更简洁的 SQL 代码执行复杂分析.现在可以使用一条 SQL 语句表示复杂任务,编写和维护速度更快.效率更高. 2. 提高查询速度 — 数据库中分析支持的处理优化可大幅提高查询性能.以前需要自联接或复杂过程处理的操作现在可以用原生 SQL 执行. 3. 改善可管理性 — 应用程序共享一个公共的关系环境,而非数据结构不兼容的计算引擎组合,因此可

[转]Show parameter &amp; Table Not exists

本文转自:http://www.cnblogs.com/fangwenyu/archive/2011/01/06/1926774.html 问题描述 在尝试通过show parameter来查看一个参数的时候,遇到了ora-00942: table or view does not exist的错误提示,如下, SQL> show user USER is "SCOTT" SQL> show parameter nls_sort; ORA-00942: table or v

Oracle 数据类型比较规则

数值 较大的值被认为大于较小的值.所有负数都小于零,所有正数都小于零.因此,-1小于100:-100小于-1. 浮点值NaN(not a number))大于任何其他数值,且等于自身. 日期时间值 较迟的日期或时间戳被认为大于较早的日期或时间戳.例如,等价于 '29-MAR-2005' 的日期小于 '05-JAN-2006' ,等价于 '05-JAN-2006 1:35pm'的时间戳大于 '05-JAN-2005 10:09am'. 当比较两个带有时区的时间戳时,它们首先被标准化为UTC,即时区

OCP读书笔记(25) - 题库(ExamE)

401.Which of the following are correct about block media recovery? (Choose all that apply.)A. Physical and logical block corruption is recorded automatically in V$DATABASE_BLOCK_CORRUPTION.B. Logical corruptions are repairable by BMR.C. Physical corr

053-461

The NLS_SORT parameter sets the default sort method for which of the following operations?(Choose all that apply.)A. WHERE clauseB. ORDER BY clauseC. BETWEEN clauseD. NLSSORT functionE. NLS_SORT function The NLS_SORT parameter defines the default sor

javacript按照汉语拼音排序

以前做了一个需求,要求按照人名的拼音进行排序,然后通过oracle的NLS_SORT参数在后台进行了排序,然后将结果返回给前端.现在想看看如果前端对字符串按照汉语拼音进行排序该怎么做呢?遂查找资料,将方法记录如下. ['张三','李四','王五'].sort((a, b) => a.localeCompare(b, 'zh-Hans-CN', {sensitivity: 'accent'})) localeCompare函数的语法如下: referenceStr.localeCompare(co

oracle之 sqlplus prelim 参数介绍 ( 处理hang )

从Oracle10g开始,sqlplus提供了一个参数选项-prelim,用这个参数,在系统已经hang的时候.我们可以连接到SGA而不是数据库,也就是说没有session被创建. 一. 通过以下步骤可以获取系统信息:sqlplus -prelim / as sysdbaoradebug setmypidoradebug unlimit;oradebug dump systemstate 10 对于9i数据库,可以通过安装10g的客户端连接 9iR2工作正常. 二. prelim参数特点1.首先

同时丢失参数文件、控制文件及redo log file的不完全恢复(有数据文件的热备和归档)

转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/30839817 ##########恢复前的准备工作 1.做个热备 select 'alter tablespace '||tablespace_name|| ' begin backup;' ||chr(10)|| 'host cp '||file_name||' /backup' ||chr(10)|| 'alter tablespace '||tablespace_name|| '

ORACLE推导参数Derived Parameter介绍

Oracle的推导参数(Derived Parameters)其实是初始化参数的一种.推导参数值通常来自于其它参数的运算,依赖其它参数计算得出.官方文档关于推导参数(Derived Parameters)的概念如下: Derived Parameters Some initialization parameters are derived, meaning that their values are calculated from the values of other parameters.