[Enhancement](topn) support two phase read for topn query (#15642)

This PR optimize topn query like `SELECT * FROM tableX ORDER BY columnA ASC/DESC LIMIT N`.

TopN is is compose of SortNode and ScanNode, when user table is wide like 100+ columns the order by clause is just a few columns.But ScanNode need to scan all data from storage engine even if the limit is very small.This may lead to lots of read amplification.So In this PR I devide TopN query into two phase:
1. The first phase we just need to read `columnA`'s data from storage engine along with an extra RowId column called `__DORIS_ROWID_COL__`.The other columns are pruned from ScanNode.
2. The second phase I put it in the ExchangeNode beacuase it's the central node for topn nodes in the cluster.The ExchangeNode will spawn a RPC to other nodes using the RowIds(sorted and limited from SortNode) read from the first phase and read row by row from storage engine.

After the second phase read, Block will contain all the data needed for the query
This commit is contained in:
lihangyu
2023-01-19 10:01:33 +08:00
committed by GitHub
parent c7a72436e6
commit 3894de49d2
53 changed files with 829 additions and 33 deletions

View File

@ -20,6 +20,8 @@
#include <memory>
#include "exec/exec_node.h"
#include "exec/tablet_info.h" // DorisNodesInfo
#include "runtime/descriptors.h"
#include "vec/common/sort/vsort_exec_exprs.h"
namespace doris {
@ -47,6 +49,9 @@ public:
// Status collect_query_statistics(QueryStatistics* statistics) override;
void set_num_senders(int num_senders) { _num_senders = num_senders; }
// final materializtion, used only in topn node
Status _second_phase_fetch_data(RuntimeState* state, Block* final_block);
private:
int _num_senders;
bool _is_merging;
@ -61,6 +66,10 @@ private:
VSortExecExprs _vsort_exec_exprs;
std::vector<bool> _is_asc_order;
std::vector<bool> _nulls_first;
// for fetch data by rowids
DorisNodesInfo* _nodes_info = nullptr;
bool _use_two_phase_read = false;
};
} // namespace vectorized
} // namespace doris