Slightly better performance on index granularity

This commit is contained in:
alesapin 2019-03-27 20:33:55 +03:00
parent e449760fde
commit ad16c7c931
3 changed files with 38 additions and 35 deletions

View File

@ -13,14 +13,11 @@ namespace ErrorCodes
IndexGranularity::IndexGranularity(const std::vector<size_t> & marks_to_rows_)
: marks_to_rows(marks_to_rows_)
{
for (size_t rows_count : marks_to_rows)
total_rows += rows_count;
}
IndexGranularity::IndexGranularity(size_t marks_count, size_t fixed_granularity)
: marks_to_rows(marks_count, fixed_granularity)
, total_rows(marks_count * fixed_granularity)
{
}
@ -29,16 +26,15 @@ size_t IndexGranularity::getAvgGranularity() const
if (marks_to_rows.empty())
throw Exception("Trying to compute average index granularity with zero marks", ErrorCodes::LOGICAL_ERROR);
return total_rows / marks_to_rows.size();
return marks_to_rows.back() / marks_to_rows.size();
}
size_t IndexGranularity::getMarkStartingRow(size_t mark_index) const
{
size_t result = 0;
for (size_t i = 0; i < mark_index; ++i)
result += marks_to_rows[i];
return result;
if (mark_index == 0)
return 0;
return marks_to_rows[mark_index - 1];
}
size_t IndexGranularity::getMarksCount() const
@ -48,31 +44,33 @@ size_t IndexGranularity::getMarksCount() const
size_t IndexGranularity::getTotalRows() const
{
return total_rows;
if (marks_to_rows.empty())
return 0;
return marks_to_rows.back();
}
void IndexGranularity::appendMark(size_t rows_count)
{
total_rows += rows_count;
if (marks_to_rows.empty())
marks_to_rows.push_back(rows_count);
else
marks_to_rows.push_back(marks_to_rows.back() + rows_count);
}
//size_t IndexGranularity::getMarkRows(size_t mark_index) const
//{
//
// if (mark_index >= marks_to_rows.size())
// throw Exception("Trying to get mark rows for mark " + toString(mark_index) + " while marks count is " + toString(marks_to_rows.size()), ErrorCodes::LOGICAL_ERROR);
// return marks_to_rows[mark_index];
//}
size_t IndexGranularity::getMarkPositionInRows(const size_t mark_index) const
{
if (mark_index == 0)
return 0;
return marks_to_rows[mark_index - 1];
}
size_t IndexGranularity::getRowsCountInRange(const MarkRange & range) const
{
size_t rows_count = 0;
for (size_t i = range.begin; i < range.end; ++i)
rows_count += getMarkRows(i);
return rows_count;
size_t subtrahend = 0;
if (range.begin != 0)
subtrahend = marks_to_rows[range.begin - 1];
return marks_to_rows[range.end - 1] - subtrahend;
}
size_t IndexGranularity::getRowsCountInRanges(const MarkRanges & ranges) const
{
@ -86,8 +84,14 @@ size_t IndexGranularity::getRowsCountInRanges(const MarkRanges & ranges) const
void IndexGranularity::resizeWithFixedGranularity(size_t size, size_t fixed_granularity)
{
marks_to_rows.resize(size, fixed_granularity);
total_rows += size * fixed_granularity;
if (marks_to_rows.empty())
{
marks_to_rows.push_back(fixed_granularity);
--size;
}
for (size_t i = 0; i < size; ++i)
marks_to_rows.push_back(marks_to_rows.back() + fixed_granularity);
}
}

View File

@ -20,6 +20,7 @@ public:
size_t getRowsCountInRange(const MarkRange & range) const;
size_t getRowsCountInRanges(const MarkRanges & ranges) const;
size_t getMarkPositionInRows(const size_t mark_index) const;
size_t getAvgGranularity() const;
@ -27,12 +28,16 @@ public:
size_t getTotalRows() const;
inline size_t getMarkRows(size_t mark_index) const
{
return marks_to_rows[mark_index];
if (mark_index == 0)
return marks_to_rows[0];
else
return marks_to_rows[mark_index] - marks_to_rows[mark_index - 1];
}
size_t getMarkStartingRow(size_t mark_index) const;
size_t getLastMarkRows() const
{
return marks_to_rows.back();
size_t last = marks_to_rows.size() - 1;
return getMarkRows(last);
}
bool empty() const
{

View File

@ -23,10 +23,7 @@ MergeTreeRangeReader::DelayedStream::DelayedStream(
size_t MergeTreeRangeReader::DelayedStream::position() const
{
size_t num_rows_before_current_mark = 0;
for (size_t i = 0; i < current_mark; ++i)
num_rows_before_current_mark += index_granularity->getMarkRows(i);
size_t num_rows_before_current_mark = index_granularity->getMarkPositionInRows(current_mark);
return num_rows_before_current_mark + current_offset + num_delayed_rows;
}
@ -54,10 +51,7 @@ size_t MergeTreeRangeReader::DelayedStream::readRows(Block & block, size_t num_r
size_t MergeTreeRangeReader::DelayedStream::read(Block & block, size_t from_mark, size_t offset, size_t num_rows)
{
size_t num_rows_before_from_mark = 0;
for (size_t i = 0; i < from_mark; ++i)
num_rows_before_from_mark += index_granularity->getMarkRows(i);
size_t num_rows_before_from_mark = index_granularity->getMarkPositionInRows(from_mark);
/// We already stand accurately in required position,
/// so because stream is lazy, we don't read anything
/// and only increment amount delayed_rows