Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ColumnDecimal, ColumnVector getPermutation performance using pdqsort with RadixSort #35961

Conversation

kitaisreal
Copy link
Collaborator

@kitaisreal kitaisreal commented Apr 5, 2022

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Improve performance of sorting for decimal columns. Improve performance of insertion into MergeTree if ORDER BY contains Decimal column. Improve performance of sorting when data is already sorted or almost sorted.

@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-performance Pull request with some performance improvements label Apr 5, 2022
@kitaisreal
Copy link
Collaborator Author

Insertion into MergeTree. Probably not common case.
Setup table:

CREATE TABLE test_table_insert (id Decimal64(8), value1 UInt64, value2 UInt64, value3 UInt64) ENGINE=MergeTree ORDER BY id;

Before:

INSERT INTO test_table_insert SELECT (rand64() % 50000) as a, a, a, a FROM system.numbers LIMIT 50000000;
0 rows in set. Elapsed: 6.039 sec. Processed 50.33 million rows, 402.64 MB (8.33 million rows/s., 66.68 MB/s.)

After:

INSERT INTO test_table_insert SELECT (rand64() % 50000) as a, a, a, a FROM system.numbers LIMIT 50000000;
0 rows in set. Elapsed: 2.893 sec. Processed 50.33 million rows, 402.64 MB (17.40 million rows/s., 139.17 MB/s.)

@kitaisreal
Copy link
Collaborator Author

Sort by decimal column.
Table setup:

CREATE TABLE test_table_sort (id UInt64, value Decimal64(4)) ENGINE=MergeTree ORDER BY id;
INSERT INTO test_table_sort SELECT number, rand64() % 50000 FROM system.numbers LIMIT 50000000;

Before:

SELECT * FROM test_table_sort ORDER BY value FORMAT Null;
0 rows in set. Elapsed: 1.404 sec. Processed 50.00 million rows, 800.00 MB (35.61 million rows/s., 569.68 MB/s.)

After:

SELECT * FROM test_table_sort ORDER BY value FORMAT Null;
0 rows in set. Elapsed: 1.321 sec. Processed 50.00 million rows, 800.00 MB (37.85 million rows/s., 605.62 MB/s.)

Comment on lines 172 to 176
if (!limit)
{
/// A case for radix sort
/// LSD RadixSort is stable
if constexpr (is_arithmetic_v<NativeT> && !is_big_int_v<NativeT>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can reverse the 2 ifs and do the if (!limit) check only for arithmetic types.

I assume compiler is smart enough to remove the branch with no code, but I think it's good to be explicit in any case.

src/Columns/ColumnDecimal.cpp Outdated Show resolved Hide resolved
::partial_sort(res.begin(), res.begin() + limit, res.end(), greater_stable(*this, nan_direction_hint));
}
else
if (!limit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same things as above, if you reverse the checks, you can eliminate a branch for arithmetic type.

@antonio2368 antonio2368 self-assigned this Apr 6, 2022
@kitaisreal kitaisreal force-pushed the decimal-column-improve-get-permutation branch 2 times, most recently from b687c9e to aa19007 Compare June 14, 2022 11:34
@kitaisreal kitaisreal added the can be tested Allows running workflows for external contributors label Jun 14, 2022
@alexey-milovidov
Copy link
Member

Did not speed up:

Screenshot_20221126_141902

@alexey-milovidov
Copy link
Member

But the numbers in #35961 (comment) look good.

@zhanglistar
Copy link
Contributor

@kitaisreal May I ask what happened to this PR for not merging?

@kitaisreal
Copy link
Collaborator Author

@zhanglistar it can provide degradation in a couple of cases, for example when data is completely sorted.

@alexey-milovidov alexey-milovidov self-assigned this Feb 19, 2023
@alexey-milovidov
Copy link
Member

Continued in #46559.

@robot-clickhouse
Copy link
Member

robot-clickhouse commented Jun 5, 2023

This is an automated comment for commit f7494a5 with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Successful checks
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR✅ success
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker image for serversThe check to build and optionally push the mentioned image to docker hub✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integrational tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Mergeable CheckChecks if all other necessary checks are successful✅ success
Push to DockerhubThe check for building and pushing the CI related docker images to docker hub✅ success
SQLTestThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool✅ success
SqllogicRun clickhouse on the sqllogic test set against sqlite and checks that all statements are passed✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style CheckRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Check nameDescriptionStatus
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests❌ failure
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests❌ failure
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts❌ failure

@kitaisreal kitaisreal force-pushed the decimal-column-improve-get-permutation branch 3 times, most recently from 2b1d414 to 228957a Compare June 8, 2023 19:02
@robot-ch-test-poll3 robot-ch-test-poll3 added the submodule changed At least one submodule changed in this PR. label Jun 11, 2023
@alexey-milovidov alexey-milovidov removed their assignment Jun 29, 2023
@kitaisreal kitaisreal changed the title DecimalColumn improve getPermutation performance using RadixSort Improve ColumnDecimal, ColumnVector getPermutation performance using pdqsort with RadixSort Jun 29, 2023
@kitaisreal kitaisreal force-pushed the decimal-column-improve-get-permutation branch 2 times, most recently from eee5cd2 to 1b71e7f Compare July 1, 2023 14:16
@alexey-milovidov alexey-milovidov self-assigned this Aug 1, 2023
@alexey-milovidov
Copy link
Member

@kitaisreal it does not build.

@kitaisreal kitaisreal force-pushed the decimal-column-improve-get-permutation branch from 0ffcb39 to 6df6155 Compare September 13, 2023 09:22
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-status-❌ PR with some error/faliure statuses and removed pr-status-⏳ PR with some pending statuses labels Sep 13, 2023
@kitaisreal kitaisreal force-pushed the decimal-column-improve-get-permutation branch from 6df6155 to 6513892 Compare September 13, 2023 11:07
@robot-ch-test-poll1 robot-ch-test-poll1 added pr-status-⏳ PR with some pending statuses and removed pr-status-❌ PR with some error/faliure statuses labels Sep 13, 2023
@robot-ch-test-poll3 robot-ch-test-poll3 added pr-status-❌ PR with some error/faliure statuses and removed pr-status-⏳ PR with some pending statuses labels Sep 13, 2023
@kitaisreal kitaisreal force-pushed the decimal-column-improve-get-permutation branch from 6513892 to 720b717 Compare September 15, 2023 17:22
@robot-clickhouse robot-clickhouse added pr-status-⏳ PR with some pending statuses and removed pr-status-❌ PR with some error/faliure statuses labels Sep 15, 2023
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-status-❌ PR with some error/faliure statuses and removed pr-status-⏳ PR with some pending statuses labels Sep 15, 2023
@kitaisreal kitaisreal force-pushed the decimal-column-improve-get-permutation branch 3 times, most recently from c9753d7 to 64f5032 Compare September 22, 2023 09:25
@kitaisreal kitaisreal force-pushed the decimal-column-improve-get-permutation branch from 64f5032 to 0057f29 Compare September 25, 2023 08:40
@kitaisreal kitaisreal force-pushed the decimal-column-improve-get-permutation branch from 0057f29 to f7494a5 Compare September 25, 2023 14:29
@alexey-milovidov alexey-milovidov merged commit edc9253 into ClickHouse:master Sep 26, 2023
270 of 274 checks passed
@kitaisreal
Copy link
Collaborator Author

kitaisreal commented Sep 28, 2023

Insert into MergeTree improvements.

Setup table:

CREATE TABLE test_table_insert (id Decimal64(8), value1 UInt64, value2 UInt64, value3 UInt64) ENGINE=MergeTree ORDER BY id;

Before:

INSERT INTO test_table_insert SELECT (rand64() % 50000) as a, a, a, a
FROM system.numbers LIMIT 50000000;
0 rows in set. Elapsed: 6.032 sec. Processed 50.33 million rows, 402.60 MB (8.34 million rows/s., 66.74 MB/s.)

After:

INSERT INTO test_table_insert SELECT (rand64() % 50000) as a, a, a, a FROM system.numbers LIMIT 50000000;
0 rows in set. Elapsed: 3.040 sec. Processed 50.33 million rows, 402.60 MB (16.56 million rows/s., 132.45 MB/s.)

Before:

INSERT INTO test_table_insert SELECT (rand64() % 500000) as a, a, a, a
FROM system.numbers LIMIT 50000000;
0 rows in set. Elapsed: 7.593 sec. Processed 50.33 million rows, 402.60 MB (6.63 million rows/s., 53.02 MB/s.)

After:

INSERT INTO test_table_insert SELECT (rand64() % 500000) as a, a, a, a
FROM system.numbers LIMIT 50000000;
0 rows in set. Elapsed: 4.644 sec. Processed 50.33 million rows, 402.60 MB (10.84 million rows/s., 86.69 MB/s.)

@kitaisreal
Copy link
Collaborator Author

Sorting already sorted data improvements:

Setup table:

CREATE TABLE sequential_UInt64(key UInt64, value UInt64) Engine = Memory;
INSERT INTO sequential_UInt64 SELECT number, number FROM numbers(500000000);

Before:

SELECT key, value FROM sequential_UInt64 ORDER BY key FORMAT Null
0 rows in set. Elapsed: 1.269 sec. Processed 500.00 million rows, 8.00 GB (394.04 million rows/s., 6.30 GB/s.)

SELECT key, value FROM sequential_UInt64 ORDER BY key, value FORMAT Null
0 rows in set. Elapsed: 1.332 sec. Processed 500.00 million rows, 8.00 GB (375.26 million rows/s., 6.00 GB/s.)

SELECT key, value FROM sequential_UInt64 ORDER BY key DESC FORMAT Null
0 rows in set. Elapsed: 2.143 sec. Processed 500.00 million rows, 8.00 GB (233.33 million rows/s., 3.73 GB/s.)

SELECT key, value FROM sequential_UInt64 ORDER BY key DESC, value DESC FORMAT Null
0 rows in set. Elapsed: 2.438 sec. Processed 500.00 million rows, 8.00 GB (205.05 million rows/s., 3.28 GB/s.)

After:

SELECT key, value FROM sequential_UInt64 ORDER BY key FORMAT Null
0 rows in set. Elapsed: 0.647 sec. Processed 500.00 million rows, 8.00 GB (772.51 million rows/s., 12.36 GB/s.)

SELECT key, value FROM sequential_UInt64 ORDER BY key, value FORMAT Null
0 rows in set. Elapsed: 0.657 sec. Processed 500.00 million rows, 8.00 GB (761.02 million rows/s., 12.18 GB/s.)

SELECT key, value FROM sequential_UInt64 ORDER BY key DESC FORMAT Null
0 rows in set. Elapsed: 1.480 sec. Processed 500.00 million rows, 8.00 GB (337.84 million rows/s., 5.41 GB/s.)

SELECT key, value FROM sequential_UInt64 ORDER BY key DESC, value DESC FORMAT Null
0 rows in set. Elapsed: 1.631 sec. Processed 500.00 million rows, 8.00 GB (306.63 million rows/s., 4.91 GB/s.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can be tested Allows running workflows for external contributors pr-performance Pull request with some performance improvements pr-status-❌ PR with some error/faliure statuses submodule changed At least one submodule changed in this PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants