Query condition cache: introduce selectivity threshold#86076
Query condition cache: introduce selectivity threshold#86076rschu1ze merged 5 commits intoClickHouse:masterfrom
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
|
Workflow [PR], commit [557014a] Summary: ❌
|
This comment was marked as resolved.
This comment was marked as resolved.
4223236 to
21e88ff
Compare
This comment was marked as resolved.
This comment was marked as resolved.
deb1803 to
ced5a0e
Compare
47386db to
e088409
Compare
| /// Marks potentially need to be updated. We don't know for sure because another thread could have updated them in the meantime. | ||
| /// Acquire a write lock and attempt the update. | ||
| bool is_insert = false; | ||
| if (!need_update_marks) |
There was a problem hiding this comment.
I don't understand why we need to code in l. 146-172. Could you explain a bit more? Is it really needed for correctness or performance?
There was a problem hiding this comment.
- When the query QPS is very high, if the query condition cache already contains the complete result, there is no need to continue collecting matching marks. This can help avoid frequent acquire of the
mutexand thecache_entry->mutexwrite lock, which improves performance. - For queries like
LIMIT, execution may terminate early before all parts are fully scanned, resulting in incomplete results in the query condition cache. This piece of code can fill in the missing marks.
9cc4598 to
1f86902
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
1f86902 to
aaacef8
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This reverts commit 827bd37.
aaacef8 to
21aaed1
Compare
|
@zhongyuankai Sorry for the delay. I have too many other things going on. I'll try to check today. |
|
@zhongyuankai I needed to simplify the logic in The PR message says:
This time I wanted to be more careful with regards to performance regressions. I ran ClickBench on a current master (on my 48 vCPUs system): Then I ran ClickBench on this PR: No performance deterioration. PS: Let's try to avoid force-pushing PRs once someone reviewed them. A force-push potentially rewrites the complete commit history and the reviewer needs to start reviewing from scratch (in principle). |
The introduction of the
I think the key to improving performance is to leverage the results already cached in the query cache to check whether the marks need to be updated. Under high QPS, the query condition cache already contains the complete results, so there is no need to write them repeatedly, which also helps avoid frequent write lock acquisitions. If only this PR #80247 is introduced, it will not improve performance, as your tests have shown, because marks almost always need to be updated. Perhaps you could rerun ClickBench based on this commit; my understanding is that it brings significant performance improvements.
Okay, I understand. |
|
@zhongyuankai Thanks for the reply.
I wonder where in the current PR (my last commit) we "fetch results from the query cache every time". Do you mean function
This confuses me. As far as I understand, each query has its own
The goal of this PR is not to improve performance, the goal is to not deteriorate performance over current |
1f99292
|
@rschu1ze Thanks for the reply.
No, I was referring to this line of code
I mainly want to address the case where an entry with the same key already exists in the cache. In This seems somewhat contradictory. Do you have a better idea? |
|
Nevermind, let's do this in a separate PR (you can add me as reviewer). Thanks! |
|
According to internal performance testing, this PR introduced regressions in ClickBench queries Q20 (by 2x), Q21 (by 8x) and Q22 (by 3x) despite the lockless implementation (compared to the original implementation #86076) and my performance testing (here). I have no idea what caused this and how to check deeper (perhaps try to reproduce with ClickBench again, maybe #87337 will help), but for now I need to revert this PR as it blocks the 25.9 release. |
I think introducing PR #87337 can resolve the performance regression. |
|
Is there any real scenario in which the memory consumption of the query condition cache is a problem? |
There is none. The real point of this PR is its new |
This is a revert of #85965 which reverted #85253 because of a performance regression.
This PR improves performance by reducing lock contention.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added setting
query_condition_cache_selectivity_threshold(default value: 1.0) which excludes scan results of predicates with low selectivity from insertion into the query condition cache. This allows to reduce the memory consumption of the query condition cache at the cost of a worse cache hit rate.