less load under mutex

This commit is contained in:
Anton Popov 2024-06-29 02:31:24 +00:00
parent e1b1c454e2
commit db0beabb1c

View File

@ -8608,13 +8608,14 @@ void MergeTreeData::unloadPrimaryKeys()
size_t MergeTreeData::unloadPrimaryKeysOfOutdatedParts()
{
size_t total_unloaded = 0;
/// If the method is already called from another thread, then we don't need to do anything.
std::unique_lock lock(unload_primary_key_mutex, std::defer_lock);
if (!lock.try_lock())
return total_unloaded;
return 0;
DataPartsVector parts_to_unload_index;
{
auto parts_lock = lockParts();
auto parts_range = getDataPartsStateRange(DataPartState::Outdated);
@ -8622,13 +8623,17 @@ size_t MergeTreeData::unloadPrimaryKeysOfOutdatedParts()
{
/// Outdated part may be hold by SELECT query and still needs the index.
if (part.unique())
parts_to_unload_index.push_back(part);
}
}
for (const auto & part : parts_to_unload_index)
{
++total_unloaded;
const_cast<IMergeTreeDataPart &>(*part).unloadIndex();
LOG_TEST(log, "Unloaded primary key for outdated part {}", part->name);
}
}
return total_unloaded;
return parts_to_unload_index.size();
}
void MergeTreeData::verifySortingKey(const KeyDescription & sorting_key)