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

时间:2023-11-22 14:18:33

示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
--语句中显示指定COLLATE子句。
SELECT 'a' = 'A', 'a' = 'A' COLLATE case_insensitive;
 ?column? | ?column?
----------+----------
 f        | t
(1 row)
--建表时指定列属性为case_insensitive。
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
--建表时指定,查询时无需指定。
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设置为准。
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)
--跨子查询隐式派生。
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-820-dws/dws_04_0984.html