Skip to content

Commit 7a91d12

Browse files
committed
Use only one thread when merging count() without group by
1 parent b769206 commit 7a91d12

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/Planner/Planner.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
#include <Storages/StorageDummy.h>
5252
#include <Storages/StorageMerge.h>
5353

54+
#include <AggregateFunctions/IAggregateFunction.h>
55+
5456
#include <Analyzer/Utils.h>
5557
#include <Analyzer/ColumnNode.h>
5658
#include <Analyzer/ConstantNode.h>
@@ -564,13 +566,21 @@ void addMergingAggregatedStep(QueryPlan & query_plan,
564566
* but it can work more slowly.
565567
*/
566568

567-
auto keys = aggregation_analysis_result.aggregation_keys;
569+
const auto & keys = aggregation_analysis_result.aggregation_keys;
570+
571+
/// For count() without parameters try to use just one thread
572+
/// Typically this will either be a trivial count or a really small number of states
573+
size_t max_threads = settings[Setting::max_threads];
574+
if (keys.empty() && aggregation_analysis_result.aggregate_descriptions.size() == 1
575+
&& aggregation_analysis_result.aggregate_descriptions[0].function->getName() == String{"count"}
576+
&& aggregation_analysis_result.grouping_sets_parameters_list.empty())
577+
max_threads = 1;
568578

569579
Aggregator::Params params(
570580
keys,
571581
aggregation_analysis_result.aggregate_descriptions,
572582
query_analysis_result.aggregate_overflow_row,
573-
settings[Setting::max_threads],
583+
max_threads,
574584
settings[Setting::max_block_size],
575585
settings[Setting::min_hit_rate_to_use_consecutive_keys_optimization]);
576586

0 commit comments

Comments
 (0)