SQL learning uses order by to sort in a specified order or order by its own definition

Source: Internet
Author: User
Tags create index

We usually need to according to customer demand for the results of the query to provide customers with their own definition of the sort, then we usually need to implement the SQL is what, many other information summarized such as the following (not good and wrong to point out):

first, let's say that we just need to sort the application in a particular program in the following way, we just have to set the sort at the SQL statement level:   

1, in accordance with the default mode of oracled:select * from table_name ORDER by Col_name (DESC|ASC);( The default is that ascending or unordered is only in the number field for lifting or descending;

2, according to their own definition of the order of ordering:SELECT * FROM table_name ORDER by decode (col_name, ' value1 ', 1,value2 ', 2,value3 ', 3, value4 ', 4,... Valuen ', N);

< Span style= "LINE-HEIGHT:25.2000007629395PX; Color:rgb (255,0,0) "> second, let's say that we just need to sort the application in a program in a way such as the following settings, we just set the sort method at the SQL statement level ( generally not set before the Chinese default sorting method of our system is in accordance with the phonetic sorting   &NBSP ): &NBSP;&NBSP;

1, according to pinyin sort: select * FROMtable_name ORDER by Nlssort (col_name, ' nls_sort=schinese_pinyin_m ');
2. Sort According to stroke: SELECT * FROM table_name ORDER by Nlssort (col_name, ' nls_sort=schinese_stroke_m ');
3, according to the radical sort: SELECT * FROM table_name ORDER by Nlssort (col_name, ' nls_sort=schinese_radical_m ');
Attention:However, when the data volume is relatively large, the query speed is very slow and needs to be further optimized. as explained in Oracle's official documentation, Oracle sorts the Chinese columns as they are indexed by 2 encoding, so assume that Nls_sort is set to binary. The sort is able to take advantage of the index. Suppose it's not a 2 binary sort, but Oracle is unable to use the index using the 3 kinds of special sorting described above for Chinese. A full table scan is performed.The workaround is to establish linguistic index on this column. For example: Create INDEX Nls_index on my_table (nlssort (name, ' Nls_sort = Schinese_pinyin_m '));

Quote Original: NOTE:

Setting Nls_sort to anything and than BINARY causes a SORT to use a full table scan, regardless of the path chosen by th E Optimizer. Binary is the exception because indexes was built according to a binary order of keys. Thus the optimizer can use a index to satisfy, the ORDER by clause when Nls_sort are 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 p Lan.


If we want to sort in a particular way throughout the session, we need to sort the default settings field at the session level:
1. Press Pinyin: Alter session set nls_sort = Schinese_pinyin_m;
2. By stroke: Alter session set nls_sort = Schinese_stroke_m;
3. By the Radicals: Alter session set nls_sort = Nls_sort=schinese_radical_m;

Suppose we need to make a specified sort of the entire data, we need to change the Oracle Server system parameters (generally this way we use less):  
1. Win System change Registration form Hklc\software\oracle\home0\nls_sort

2. Other changes configuration set Nls_sort=schinese_radical_m export Nls_sort

SQL learning uses order by to sort in a specified order or order by its own definition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.