Skip to content

Optimize performance with lazy projection to avoid reading unused columns#55518

Merged
devcrafter merged 140 commits intoClickHouse:masterfrom
wudidapaopao:wudidapaopao/optimize_lazy_projection
Mar 25, 2025
Merged

Optimize performance with lazy projection to avoid reading unused columns#55518
devcrafter merged 140 commits intoClickHouse:masterfrom
wudidapaopao:wudidapaopao/optimize_lazy_projection

Conversation

@wudidapaopao
Copy link
Copy Markdown
Contributor

@wudidapaopao wudidapaopao commented Oct 11, 2023

See #45868

Changelog category (leave one):

  • Performance Improvement

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

Optimize performance with lazy projection to avoid reading unused columns.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Oct 11, 2023

CLA assistant check
All committers have signed the CLA.

@wudidapaopao
Copy link
Copy Markdown
Contributor Author

wudidapaopao commented Oct 11, 2023

@alexey-milovidov Based on #45868, lazy projection has been implemented to improve performance.
The current code implementation is quite rudimentary, but it still has some effectiveness for som queries.
For example, performance of query Q23 in ClickBench has been significantly improved.

SELECT * FROM hits WHERE URL LIKE '%google%' ORDER BY EventTime LIMIT 10;
Before:Elapsed: 1.676 sec. Processed 8.87 million rows
After:Elapsed: 0.846 sec. Processed 8.87 million rows

@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-performance Pull request with some performance improvements label Oct 11, 2023
@robot-ch-test-poll3
Copy link
Copy Markdown
Contributor

robot-ch-test-poll3 commented Oct 11, 2023

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

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

Check nameDescriptionStatus
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure
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
BuildsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
BuzzHouse (asan)There's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
BuzzHouse (debug)There's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
BuzzHouse (msan)There's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
BuzzHouse (tsan)There's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
BuzzHouse (ubsan)There's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
ClickBenchRuns ClickBench with instant-attach table✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ 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
Install packagesChecks that the built packages are installable in a clear environment✅ success
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ 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
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✅ success

Copy link
Copy Markdown

@xingyingone xingyingone left a comment

Choose a reason for hiding this comment

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

LGTM

M(Bool, query_plan_aggregation_in_order, true, "Use query plan for aggregation-in-order optimisation", 0) \
M(Bool, query_plan_remove_redundant_sorting, true, "Remove redundant sorting in query plan. For example, sorting steps related to ORDER BY clauses in subqueries", 0) \
M(Bool, query_plan_remove_redundant_distinct, true, "Remove redundant Distinct step in query plan", 0) \
M(Bool, query_plan_optimize_lazy_projection, false, "Use query plan for lazy projection optimisation", 0) \
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Better to use query_plan_optimize_lazy_materialization

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you again for taking the time to review my work. I have renamed it to query_plan_optimize_lazy_materialization.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 13, 2023
@baibaichen
Copy link
Copy Markdown
Contributor

@alexey-milovidov Based on #45868, lazy projection has been implemented to improve performance. The current code implementation is quite rudimentary, but it still has some effectiveness for som queries. For example, performance of query Q23 in ClickBench has been significantly improved.

SELECT * FROM hits WHERE URL LIKE '%google%' ORDER BY EventTime LIMIT 10;
Before:Elapsed: 0.846 sec. Processed 8.87 million rows
After:Elapsed: 1.676 sec. Processed 8.87 million rows

Before 0.8 vs after 1.6, more slow?

@wudidapaopao
Copy link
Copy Markdown
Contributor Author

@alexey-milovidov Based on #45868, lazy projection has been implemented to improve performance. The current code implementation is quite rudimentary, but it still has some effectiveness for som queries. For example, performance of query Q23 in ClickBench has been significantly improved.

SELECT * FROM hits WHERE URL LIKE '%google%' ORDER BY EventTime LIMIT 10;
Before:Elapsed: 0.846 sec. Processed 8.87 million rows
After:Elapsed: 1.676 sec. Processed 8.87 million rows

Before 0.8 vs after 1.6, more slow?

I apologize for my mistake. It has been corrected.

@lgbo-ustc
Copy link
Copy Markdown
Contributor

lgbo-ustc commented Oct 16, 2023

Out of curiosity, Could the Columnlazy be implemented by ColumnFunction ?

@wudidapaopao wudidapaopao force-pushed the wudidapaopao/optimize_lazy_projection branch from 9303fc9 to ce68887 Compare October 16, 2023 15:39
@wudidapaopao
Copy link
Copy Markdown
Contributor Author

wudidapaopao commented Oct 16, 2023

Out of curiosity, Could the Columnlazy be implemented by ColumnFunction ?

Both 'ColumnLazy' and 'ColumnFunction' imply delayed execution. While it is possible to achieve the same result using 'ColumnFunction', it may not appear as intuitive, convenient, and concise as using 'ColumnLazy' directly.

@baibaichen
Copy link
Copy Markdown
Contributor

It looks like Columnlazy could be more general, for example:

  1. In case of a join b join c, we could materialize column after all joins are done, which could benefit from reduced materialization.
  2. Beyond reading from mergetree, it also useful for reading from Parquet or ORC

private:
friend class COWHelper<IColumn, ColumnLazy>;

WrappedPtr part_nums;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I thought it would store a callback that encapsulates all the details, and you initialize the column with this callback. It will be useful for any kind of delayed reading or calculations.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If I understand correctly, I need to encapsulate row_nums and part_nums into an array. Different delayed executions may require different column arrays. It is similar to ColumnFunction.

@alexey-milovidov
Copy link
Copy Markdown
Member

This is a very good feature, that I dreamed of! But we need to make ColumnLazy more generic - containing an opaque callback to transform it to a full column.

@wudidapaopao
Copy link
Copy Markdown
Contributor Author

It looks like Columnlazy could be more general, for example:

  1. In case of a join b join c, we could materialize column after all joins are done, which could benefit from reduced materialization.
  2. Beyond reading from mergetree, it also useful for reading from Parquet or ORC

Yes, although currently only the lazy read of MergeTree has been implemented.

@wudidapaopao
Copy link
Copy Markdown
Contributor Author

This is a very good feature, that I dreamed of! But we need to make ColumnLazy more generic - containing an opaque callback to transform it to a full column.

Reimplemented ColumnLazy, adding the IColumnLazyHelper callback interface to abstract away related details.

@devcrafter devcrafter enabled auto-merge March 22, 2025 17:53
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Mar 22, 2025

Workflow [PR], commit [73ad4c2]

@devcrafter devcrafter disabled auto-merge March 23, 2025 15:16
@devcrafter devcrafter enabled auto-merge March 23, 2025 15:16
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Mar 23, 2025

Workflow [PR], commit [5f94c77]

@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Mar 24, 2025

Workflow [PR], commit [2b9a0d9]

@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Mar 24, 2025

Workflow [PR], commit [53a186d]

@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Mar 24, 2025

Workflow [PR], commit [0dc1c3f]

@devcrafter
Copy link
Copy Markdown
Member

@devcrafter devcrafter added this pull request to the merge queue Mar 25, 2025
Merged via the queue into ClickHouse:master with commit 704e91c Mar 25, 2025
122 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 25, 2025
baibaichen added a commit to Kyligence/gluten that referenced this pull request Mar 26, 2025
baibaichen added a commit to Kyligence/gluten that referenced this pull request Mar 26, 2025
baibaichen added a commit to apache/gluten that referenced this pull request Mar 26, 2025
* [GLUTEN-1632][CH]Daily Update Clickhouse Version (20250326)

* Fix build due to ClickHouse/ClickHouse#55518

* Fix Build due to ClickHouse/ClickHouse#77013

* Fix gtest build due to ClickHouse/ClickHouse#77895

* disbable query_plan_optimize_lazy_materialization due to #9141

---------

Co-authored-by: kyligence-git <[email protected]>
Co-authored-by: Chang chen <[email protected]>
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-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.