您好,登錄后才能下訂單哦!
在sql排序中,oracle默認(rèn)采用二進(jìn)制的排序方法。大小寫(xiě)有不同的值,大寫(xiě)的值排在前面。有時(shí)候,我們需要處理的情況是,希望忽略大小寫(xiě)來(lái)進(jìn)行排序。有多種方法可以實(shí)現(xiàn):
設(shè)置NLS環(huán)境變量
alter session set NLS_SORT = 'BINARY_CI';
使用UPPER和LOWER函數(shù)
用UPPER函數(shù)和LOWER函數(shù)把要比較的字段名、文字都轉(zhuǎn)換成大寫(xiě)或者小寫(xiě)后再比較。這種方法的不足之處在于,使用函數(shù)后,標(biāo)準(zhǔn)的索引就不能再使用了,優(yōu)化器無(wú)法正常工作,應(yīng)對(duì)的方式是使用基于功能的索引(function-based index)。
注意:NLS_SORT僅僅影響排序的結(jié)果,并不對(duì)其他大小寫(xiě)操作造成影響。若要解決不區(qū)分大小寫(xiě)的比較操作,我們同樣可以采用設(shè)置NLS環(huán)境變量的方式來(lái)完成:
alter session set NLS_COMP = 'LINGUISTIC';
官方文檔中關(guān)于NLS_SORT和NLS_COMP有這樣一段話:
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.
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.
根據(jù)上文中標(biāo)紅部分的注視,如果NLS_SORT不是設(shè)置為"Binary",那么就會(huì)引起全表掃描,是不會(huì)使用索引的,在我們的系統(tǒng)中變更單涉及到的數(shù)據(jù)都是數(shù)據(jù)龐大的表,如果不使用到索引,查詢的效率會(huì)受到影響。
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.
根據(jù)標(biāo)紅的部分,要提高性能可以在需要比較的列上建立一個(gè)linguistic index。若想使NLS_COMP參數(shù)值為L(zhǎng)INGUISTIC生效,需要設(shè)置NLS_SORT為L(zhǎng)INGUISTIC 排序。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。