diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index cdf1b4228bc..d3d7470ad25 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -2501,7 +2501,12 @@ void InterpreterSelectQuery::executeFetchColumns(QueryProcessingStage::Enum proc max_block_size = std::max(1, max_block_limited); max_threads_execute_query = max_streams = 1; } - if (max_block_limited < local_limits.local_limits.size_limits.max_rows) + if (local_limits.local_limits.size_limits.max_rows != 0 && + max_block_limited < local_limits.local_limits.size_limits.max_rows) + { + query_info.limit = max_block_limited; + } + else { query_info.limit = max_block_limited; } diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index e2cdf146a69..095db09ffbd 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -645,7 +645,12 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(QueryTreeNodePtr table_expres max_threads_execute_query = 1; } - if (max_block_size_limited < select_query_info.local_storage_limits.local_limits.size_limits.max_rows) + if (select_query_info.local_storage_limits.local_limits.size_limits.max_rows != 0 && + max_block_size_limited < select_query_info.local_storage_limits.local_limits.size_limits.max_rows) + { + table_expression_query_info.limit = max_block_size_limited; + } + else { table_expression_query_info.limit = max_block_size_limited; } diff --git a/src/Processors/QueryPlan/ReadFromMergeTree.cpp b/src/Processors/QueryPlan/ReadFromMergeTree.cpp index aa1c463e4e6..bdb2f7ea009 100644 --- a/src/Processors/QueryPlan/ReadFromMergeTree.cpp +++ b/src/Processors/QueryPlan/ReadFromMergeTree.cpp @@ -418,7 +418,13 @@ Pipe ReadFromMergeTree::readFromPool( && settings.allow_prefetched_read_pool_for_local_filesystem && MergeTreePrefetchedReadPool::checkReadMethodAllowed(reader_settings.read_settings.local_fs_method); - if (allow_prefetched_remote || allow_prefetched_local) + /** Do not use prefetched read pool if query is trivial limit query. + * Because time spend during filling per thread tasks can be greater than whole query + * execution for big tables with small limit. + */ + bool use_prefetched_read_pool = query_info.limit != 0 && (allow_prefetched_remote || allow_prefetched_local); + + if (use_prefetched_read_pool) { pool = std::make_shared( std::move(parts_with_range),