Sorting optimization
AnalyticDB for PostgreSQL accelerates queries with compound sorting, which applies to queries whose equality or range conditions contain a fixed set of columns, or interleaved sorting, which applies to queries whose filter conditions do not contain a fixed set of columns.
Background
When you create a table, you can define one or more columns as sort keys. After data is written to the table, you can sort and reorganize the table by the sort keys.
Sorting a table can accelerate range-filtered queries. The database records the minimum and maximum values of every column in each fixed row group. When a query uses range filter conditions, the AnalyticDB for PostgreSQL query engine can use the minimum and maximum values to skip data blocks that do not meet the filter conditions during a table scan (SCAN).
For example, suppose that a table stores 7 years of data and the data is sorted and stored by a time column. To query the data of one month, only 1/(7*12) of the data needs to be scanned, and 98.8% of data blocks can be skipped during the scan. However, if the data is not sorted by time, all data blocks on the disk may need to be scanned.
AnalyticDB for PostgreSQL supports two sorting methods:
-
Compound sorting (SORT): suitable for scenarios where the filter conditions are a prefix subset of the sort key, such as queries whose filter conditions contain the leading sort key column.
-
Interleaved sorting (Multisort): assigns the same weight to each sort key column. This method is more suitable for scenarios where queries contain filter conditions on any subset of columns.
Choose a sort key
-
If the equality or range conditions in your query SQL statements frequently contain a fixed set of columns, you can use these columns as sort keys. In this way, sorted data and rough indexes can accelerate the queries. In general, compound sorting is recommended in this case.
-
If the filter conditions in your query SQL statements do not contain a fixed set of columns, you can use interleaved sorting to accelerate the queries. Interleaved sorting requires additional data organization during the sorting process, so it generally takes longer than compound sorting.
NoteInterleaved sorting supports up to 8 columns.
-
If your query SQL statements frequently use a fixed set of columns as JOIN conditions, you can set the JOIN columns as both distribution keys and sort keys. In this way, MergeJoin is used instead of HashJoin. Because the underlying data is already sorted by the JOIN columns, the time-consuming sorting phase of MergeJoin can be skipped.
Performance comparison between compound and interleaved sorting
The following content compares the performance impact of compound sorting and interleaved sorting on different queries in different scenarios by sorting two identical tables with the two methods.
-
Create two test tables and set the sort keys. Each table contains four columns: id, num1, num2, and value. The sort keys are id, num1, and num2. The statements are as follows:
CREATE TABLE test(id int, num1 int, num2 int, value varchar) with(APPENDONLY=TRUE, ORIENTATION=column) DISTRIBUTED BY(id) ORDER BY(id, num1, num2); CREATE TABLE test_multi(id int, num1 int, num2 int, value varchar) with(APPENDONLY=TRUE, ORIENTATION=column) DISTRIBUTED BY(id) ORDER BY(id, num1, num2); -
Insert 10 million rows of test data. The statements are as follows:
INSERT INTO test(id, num1, num2, value) select g, (random()*10000000)::int, (random()*10000000)::int, (array['foo', 'bar', 'baz', 'quux', 'boy', 'girl', 'mouse', 'child', 'phone'])[floor(random() * 10 +1)] FROM generate_series(1, 10000000) as g; INSERT INTO test_multi SELECT * FROM test;Query the total number of rows in the two tables:
SELECT count(*) FROM test;The following information is returned:
count ---------- 10000000 (1 row)SELECT count(*) FROM test_multi;The following information is returned:
count ---------- 10000000 (1 row)
-
Perform compound sorting and interleaved sorting on the two tables.
Perform compound sorting on the test table:
SORT test;Perform interleaved sorting on the test_multi table:
MULTISORT test_multi; -
The equality query performance comparison is as follows:
-
Q1: filter on the leading sort key column.
SELECT * FROM test WHERE id = 100000; SELECT * FROM test_multi WHERE id = 100000; -
Q2: filter on the second sort key column.
SELECT * FROM test WHERE num1 = 8766963; SELECT * FROM test_multi WHERE num1 = 8766963; -
Q3: filter on the second and third sort key columns.
SELECT * FROM test WHERE num1 = 100000 AND num2=2904114; SELECT * FROM test_multi WHERE num1 = 100000 AND num2=2904114;
The performance comparison results are as follows:
Sorting method
Q1
Q2
Q3
Compound sorting
0.026s
3.95s
4.21s
Interleaved sorting
0.55s
0.42s
0.071s
-
-
The range query performance comparison is as follows:
-
Q1: filter on the leading sort key column.
SELECT count(*) FROM test WHERE id>5000 AND id < 100000; SELECT count(*) FROM test_multi WHERE id>5000 AND id < 100000; -
Q2: filter on the second sort key column.
SELECT count(*) FROM test WHERE num1 >5000 AND num1 <100000; SELECT count(*) FROM test_multi WHERE num1 >5000 AND num1 <100000; -
Q3: filter on the second and third sort key columns.
SELECT count(*) FROM test WHERE num1 >5000 AND num1 <100000 AND num2 < 100000; SELECT count(*) FROM test_multi WHERE num1 >5000 AND num1 <100000 AND num2 < 100000;
The performance comparison results are as follows:
Sorting method
Q1
Q2
Q3
Compound sorting
0.07s
3.35s
3.64s
Interleaved sorting
0.44s
0.28s
0.047s
-
The conclusions are as follows:
-
In the Q1 scenario, compound sorting performs well because the predicates contain the leading sort key column, whereas interleaved sorting performs slightly worse.
-
In the Q2 scenario, compound sorting is basically ineffective because the predicates do not contain the leading sort key column, whereas interleaved sorting maintains a stable performance improvement.
-
In the Q3 scenario, compound sorting is still ineffective because the predicates do not contain the leading sort key column. In addition, the increased comparison conditions require extra comparison overhead, so the query takes longer. Interleaved sorting shows better performance because the more interleaved sort key columns are contained in the query predicates, the better the performance.
Sorting acceleration
After you run SORT <tablename>, the system sorts the table data. After the data is sorted, AnalyticDB for PostgreSQL can use the physical order of the data to push SORT operators down to the storage layer for computation acceleration. If your SQL statements can take advantage of the physical order of the underlying data, you can benefit from the acceleration. This feature accelerates SORT, AGG, and JOIN operators based on the sort key.
-
Sorting acceleration requires the data to be fully ordered. After you write data, run
SORT <tablename>again to sort the data. -
Sorting acceleration is enabled by default.
In the following example, the same query statement is executed on the test table far to compare the query time before and after sorting acceleration.
-
Create the test table far. The statement is as follows:
CREATE TABLE far(a int, b int) WITH (APPENDONLY=TRUE, COMPRESSTYPE=ZSTD, COMPRESSLEVEL=5) DISTRIBUTED BY (a) --Distribution key ORDER BY (a); --Sort key -
Write 1,000,000 rows of data. The statement is as follows:
INSERT INTO far VALUES(generate_series(0, 1000000), 1); -
After the data is imported, sort the data. The statement is as follows:
SORT far;
The query performance comparison is as follows:
The query times in this example are for reference only. Query time is affected by multiple factors such as data volume, computing resources, and network conditions. The actual query time prevails.
-
ORDER BY acceleration
-
Before sorting acceleration (unsorted data), the ORDER BY query takes 323.980 ms:
postgres=# select * from far order by a limit 1; a | b ---+--- 0 | 1 (1 row) Time: 323.980 ms -
After sorting acceleration, the query performance is improved.
postgres=# select * from far order by a limit 1; a | b ----+---- 0 | 1 (1 row) Time: 6.971 ms
-
-
GROUP BY acceleration
-
Before sorting acceleration (unsorted data):
postgres=# select a,count(*) from far group by a limit 1; a | count ---------+------- 579229 | 1 (1 row) Time: 779.368 ms -
After sorting acceleration:
postgres=# select a, count(*) from far group by a limit 1; a | count ---+------- 0 | 1 (1 row) Time: 6.859 ms
-
-
JOIN acceleration
-
Before sorting acceleration (unsorted data):
postgres=# select * from far t1, far t2 where t1.a = t2.a limit 1; a | b | a | b ---+---+---+--- 2 | 1 | 2 | 1 (1 row) Time: 289.075 ms -
After sorting acceleration:
NoteTo enable JOIN sorting acceleration, disable ORCA and enable merge join. The statements are as follows:
SET enable_mergejoin TO on; SET optimizer TO off;The JOIN query is executed after sorting acceleration, and the query takes 12.315 ms.
postgres=# select * from far t1, far t2 where t1.a = t2.a limit 1; a | b | a | b ---+---+---+--- 2 | 1 | 2 | 1 (1 row) Time: 12.315 ms
-
|
- |
ORDER BY |
GROUP BY |
JOIN |
|
Before acceleration |
323.980 ms |
779.368 ms |
289.075 ms |
|
After acceleration |
6.971 ms |
6.859 ms |
12.315 ms |