diff --git a/dbms/src/Storages/DeltaMerge/File/DMFileVectorIndexReader.cpp b/dbms/src/Storages/DeltaMerge/File/DMFileVectorIndexReader.cpp index 8f712d53860..ec7ab1a8af7 100644 --- a/dbms/src/Storages/DeltaMerge/File/DMFileVectorIndexReader.cpp +++ b/dbms/src/Storages/DeltaMerge/File/DMFileVectorIndexReader.cpp @@ -190,7 +190,7 @@ std::vector DMFileVectorIndexReader::loadVectorSearchRes void DMFileVectorIndexReader::read( MutableColumnPtr & vec_column, - const std::vector & row_ids, + const std::span & selected_rows, size_t start_offset, size_t column_size) { @@ -200,7 +200,7 @@ void DMFileVectorIndexReader::read( vec_column->reserve(column_size); std::vector value; size_t current_rowid = start_offset; - for (auto rowid : row_ids) + for (auto rowid : selected_rows) { vec_index->get(rowid, value); if (rowid > current_rowid) diff --git a/dbms/src/Storages/DeltaMerge/File/DMFileVectorIndexReader.h b/dbms/src/Storages/DeltaMerge/File/DMFileVectorIndexReader.h index 89083214ad1..70252eecc4a 100644 --- a/dbms/src/Storages/DeltaMerge/File/DMFileVectorIndexReader.h +++ b/dbms/src/Storages/DeltaMerge/File/DMFileVectorIndexReader.h @@ -77,7 +77,7 @@ class DMFileVectorIndexReader // return the real number of rows read. void read( MutableColumnPtr & vec_column, - const std::vector & row_ids, + const std::span & selected_rows, size_t start_offset, size_t column_size); diff --git a/dbms/src/Storages/DeltaMerge/File/DMFileWithVectorIndexBlockInputStream.cpp b/dbms/src/Storages/DeltaMerge/File/DMFileWithVectorIndexBlockInputStream.cpp index b1a86f0bbab..a6e76f8270e 100644 --- a/dbms/src/Storages/DeltaMerge/File/DMFileWithVectorIndexBlockInputStream.cpp +++ b/dbms/src/Storages/DeltaMerge/File/DMFileWithVectorIndexBlockInputStream.cpp @@ -168,16 +168,13 @@ std::tuple DMFileWithVectorIndexBlockInputStream::readByIndexRead size_t read_rows = index_reader_next_row_id - block_start_row_id; auto vec_column = vec_cd.type->createColumn(); - std::vector row_ids; - for (auto row_id : sorted_results) - { - if (row_id >= block_start_row_id && row_id < index_reader_next_row_id) - row_ids.push_back(row_id); - } - vec_index_reader->read(vec_column, row_ids, block_start_row_id, read_rows); + auto begin = std::lower_bound(sorted_results.cbegin(), sorted_results.cend(), block_start_row_id); + auto end = std::lower_bound(begin, sorted_results.cend(), index_reader_next_row_id); + const std::span block_selected_rows{begin, end}; + vec_index_reader->read(vec_column, block_selected_rows, block_start_row_id, read_rows); block.insert(ColumnWithTypeAndName{std::move(vec_column), vec_cd.type, vec_cd.name, vec_cd.id}); - return {block, row_ids.size()}; + return {block, block_selected_rows.size()}; } std::tuple DMFileWithVectorIndexBlockInputStream::readByFollowingOtherColumns() @@ -199,19 +196,16 @@ std::tuple DMFileWithVectorIndexBlockInputStream::readByFollowing auto vec_column = vec_cd.type->createColumn(); // Then read from vector index for the same pack. - std::vector row_ids; - for (auto row_id : sorted_results) - { - if (row_id >= block_others.startOffset() && row_id < block_others.startOffset() + read_rows) - row_ids.push_back(row_id); - } - vec_index_reader->read(vec_column, row_ids, block_others.startOffset(), read_rows); + auto begin = std::lower_bound(sorted_results.cbegin(), sorted_results.cend(), block_others.startOffset()); + auto end = std::lower_bound(begin, sorted_results.cend(), block_others.startOffset() + read_rows); + const std::span block_selected_rows{begin, end}; + vec_index_reader->read(vec_column, block_selected_rows, block_others.startOffset(), read_rows); // Re-assemble block using the same layout as header. // Insert the vector column into the block. auto index = header.getPositionByName(vec_cd.name); block_others.insert(index, ColumnWithTypeAndName(std::move(vec_column), vec_cd.type, vec_cd.name)); - return {block_others, row_ids.size()}; + return {block_others, block_selected_rows.size()}; } void DMFileWithVectorIndexBlockInputStream::load()