2016-03-04 63 views
5

(本this question一部分,但它是有點文不對題,所以我決定把它自身的問題。)符〜<〜Postgres裏

我找不到什麼操作~<~是。 Postgres手冊只提及~和類似的運算符here,但沒有符號~<~

當PSQL控制檯擺弄,我發現,這些命令給了相同的結果:

SELECT * FROM test ORDER BY name USING ~<~; 
SELECT * FROM test ORDER BY name COLLATE "C"; 

而且這些給出了相反的順序:

SELECT * FROM test ORDER BY name USING ~>~; 
SELECT * FROM test ORDER BY name COLLATE "C" DESC; 

而且在波浪運營商的一些信息:

\do ~*~ 
            List of operators 
    Schema | Name | Left arg type | Right arg type | Result type |  Description  
------------+------+---------------+----------------+-------------+------------------------- 
pg_catalog | ~<=~ | character  | character  | boolean  | less than or equal 
pg_catalog | ~<=~ | text   | text   | boolean  | less than or equal 
pg_catalog | ~<~ | character  | character  | boolean  | less than 
pg_catalog | ~<~ | text   | text   | boolean  | less than 
pg_catalog | ~>=~ | character  | character  | boolean  | greater than or equal 
pg_catalog | ~>=~ | text   | text   | boolean  | greater than or equal 
pg_catalog | ~>~ | character  | character  | boolean  | greater than 
pg_catalog | ~>~ | text   | text   | boolean  | greater than 
pg_catalog | ~~ | bytea   | bytea   | boolean  | matches LIKE expression 
pg_catalog | ~~ | character  | text   | boolean  | matches LIKE expression 
pg_catalog | ~~ | name   | text   | boolean  | matches LIKE expression 
pg_catalog | ~~ | text   | text   | boolean  | matches LIKE expression 
(12 rows) 

第3行和第4行是我正在查找的操作員,但是desc ription對我來說有點不足。

+0

該運營商正在使用的Postgres的查詢,如果你有一個的opclass指數。 http://www.postgresql.org/docs/9.5/static/indexes-opclass.html –

+0

在'test(name text_pattern_ops)'上創建一個索引並查看'name LIKE'的'EXPLAIN'輸出abc%'' –

回答

4

~>=~~<=~~>~~<~text圖案(或varchar,基本上是相同的)的運營商,它們各自的兄弟姐妹>=<=><的對應物。他們嚴格按字節值對字符數據進行排序,忽略任何排序規則設置(與其兄弟相對)。這使得它們更快,但對大多數語言/國家也無效。

「C」語言環境實際上與沒有語言環境相同,表示沒有整理規則。這就解釋了爲什麼ORDER BY name USING ~<~ORDER BY name COLLATE "C"最終會這樣做。

在這個相關答案對dba.SE的最後一章詳細解釋:


注意~~是用來實現SQL LIKE expression Postgres的運營商和與幾乎沒有關係以上。同樣,~~*執行ILIKE。更多:

+0

感謝您的回答,尤其是指出與「COLLATE」C「'的關係。 我甚至在問我的問題之前,在dba.SE上讀到了你的答案,但不知何故,我錯過了用〜〜〜〜操作符的部分。 –