数据仓库服务 GAUSSDB(DWS)-GaussDB(DWS)排序规则:示例

时间:2024-10-28 14:23:05

示例

语句中显示指定COLLATE子句。

1
2
3
4
5
SELECT 'a' = 'A', 'a' = 'A' COLLATE case_insensitive;
 ?column? | ?column?
----------+----------
 f        | t
(1 row)

建表时指定列属性为case_insensitive。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
CREATE TABLE t1 (a text collate case_insensitive);
NOTICE:  The 'DISTRIBUTE BY' clause is not specified. Using round-robin as the distribution mode by default.
HINT:  Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
CREATE TABLE
\d t1
            Table "public.t1"
 Column | Type |        Modifiers
--------+------+--------------------------
 a      | text | collate case_insensitive

INSERT INTO t1 values('a'),('A'),('b'),('B');
INSERT 0 4

建表时指定,查询时无需指定。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
SELECT a, a='a' FROM t1;
 a | ?column?
---+----------
 A | t
 B | f
 a | t
 b | f
(4 rows)
SELECT a, count(1) FROM t1 GROUP BY a;
 a | count
---+-------
 a |     2
 B |     2
(2 rows)

CASE表达式,以WHEN子句中的COLLATE设置为准。

1
2
3
4
5
6
7
8
SELECT a,case a when 'a' collate case_insensitive then 'case1' when 'b' collate "C" then 'case2' else 'case3' end FROM t1;
 a | case
---+-------
 A | case1
 B | case3
 a | case1
 b | case2
(4 rows)

跨子查询隐式派生。

1
2
3
4
5
6
7
8
9
SELECT * FROM (SELECT a collate "C" from t1) WHERE a in ('a','b');
 a
---
 a
 b
(2 rows)
SELECT * FROM t1,(SELECT a collate "C" from t1) t2 WHERE t1.a=t2.a;
ERROR:  could not determine which collation to use for string hashing
HINT:  Use the COLLATE clause to set the collation explicitly.
  • 由于collate case_insensitive为不敏感排序,结果集不确定,再使用敏感排序筛选,会有结果集不稳定的问题,因此语句中避免出现敏感排序和不敏感排序混用。
  • 使用collate case_insensitive指定字符类型为大小写不敏感后,性能较使用前会有所下降,因此性能敏感场景需谨慎评估后使用。
support.huaweicloud.com/devg-dws/dws_04_0984.html