Skip to content

Commit c83014b

Browse files
Merge pull request ClickHouse#11114 from azat/max_threads-simple-query-optimization-fix
Do not reserve extra threads after max threads optimization for simple queries
2 parents 42f72d5 + aa656f9 commit c83014b

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

src/Interpreters/InterpreterSelectQuery.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ void InterpreterSelectQuery::executeFetchColumns(
15331533
if constexpr (pipeline_with_processors)
15341534
{
15351535
if (streams.size() == 1 || pipes.size() == 1)
1536-
pipeline.setMaxThreads(streams.size());
1536+
pipeline.setMaxThreads(1);
15371537

15381538
/// Unify streams. They must have same headers.
15391539
if (streams.size() > 1)

src/Interpreters/InterpreterSelectWithUnionQuery.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ QueryPipeline InterpreterSelectWithUnionQuery::executeWithProcessors()
271271
{
272272
auto common_header = getCommonHeaderForUnion(headers);
273273
main_pipeline.unitePipelines(std::move(pipelines), common_header);
274+
275+
// nested queries can force 1 thread (due to simplicity)
276+
// but in case of union this cannot be done.
277+
UInt64 max_threads = context->getSettingsRef().max_threads;
278+
main_pipeline.setMaxThreads(std::min<UInt64>(nested_interpreters.size(), max_threads));
274279
}
275280

276281
main_pipeline.addInterpreterContext(context);

tests/queries/0_stateless/01283_max_threads_simple_query_optimization.reference

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
DROP TABLE IF EXISTS data_01283;
2+
3+
CREATE TABLE data_01283 engine=MergeTree()
4+
ORDER BY key
5+
PARTITION BY key
6+
AS SELECT number key FROM numbers(10);
7+
8+
SET log_queries=1;
9+
SELECT * FROM data_01283 LIMIT 1 FORMAT Null;
10+
SET log_queries=0;
11+
SYSTEM FLUSH LOGS;
12+
13+
-- 1 for PullingAsyncPipelineExecutor::pull
14+
-- 1 for AsynchronousBlockInputStream
15+
SELECT
16+
throwIf(count() != 1, 'no query was logged'),
17+
throwIf(length(thread_ids) != 2, 'too many threads used')
18+
FROM system.query_log
19+
WHERE type = 'QueryFinish' AND query LIKE '%data_01283 LIMIT 1%'
20+
GROUP BY thread_ids
21+
FORMAT Null;

0 commit comments

Comments
 (0)