Add total_bytes for RocksDB storage

Show total_bytes in system tables for RocksDB storage

Previously it did not work because total_rows was calculated only when optimize_trivial_approximate_count_query=1
This commit is contained in:
Aleksandr Musorin 2023-11-10 14:40:55 +01:00
parent 4cc2d6baa5
commit 38133692bb
5 changed files with 32 additions and 13 deletions

View File

@ -545,7 +545,7 @@ class IColumn;
M(Bool, database_atomic_wait_for_drop_and_detach_synchronously, false, "When executing DROP or DETACH TABLE in Atomic database, wait for table data to be finally dropped or detached.", 0) \
M(Bool, enable_scalar_subquery_optimization, true, "If it is set to true, prevent scalar subqueries from (de)serializing large scalar values and possibly avoid running the same subquery more than once.", 0) \
M(Bool, optimize_trivial_count_query, true, "Process trivial 'SELECT count() FROM table' query from metadata.", 0) \
M(Bool, optimize_trivial_approximate_count_query, false, "Use an approximate value for trivial count optimization of storages that support such estimations.", 0) \
M(Bool, optimize_trivial_approximate_count_query, true, "Use an approximate value for trivial count optimization of storages that support such estimations.", 0) \
M(Bool, optimize_count_from_files, true, "Optimize counting rows from files in supported input formats", 0) \
M(Bool, use_cache_for_count_from_files, true, "Use cache to count the number of rows in files", 0) \
M(Bool, optimize_respect_aliases, true, "If it is set to true, it will respect aliases in WHERE/GROUP BY/ORDER BY, that will help with partition pruning/secondary indexes/optimize_aggregation_in_order/optimize_read_in_order/optimize_trivial_count", 0) \

View File

@ -703,17 +703,26 @@ void registerStorageEmbeddedRocksDB(StorageFactory & factory)
std::optional<UInt64> StorageEmbeddedRocksDB::totalRows(const Settings & settings) const
{
if (settings.optimize_trivial_approximate_count_query)
{
std::shared_lock lock(rocksdb_ptr_mx);
if (!rocksdb_ptr)
return {};
UInt64 estimated_rows;
if (!rocksdb_ptr->GetIntProperty("rocksdb.estimate-num-keys", &estimated_rows))
return {};
return estimated_rows;
}
return {};
if (!settings.optimize_trivial_approximate_count_query)
return {};
std::shared_lock lock(rocksdb_ptr_mx);
if (!rocksdb_ptr)
return {};
UInt64 estimated_rows;
if (!rocksdb_ptr->GetIntProperty("rocksdb.estimate-num-keys", &estimated_rows))
return {};
return estimated_rows;
}
std::optional<UInt64> StorageEmbeddedRocksDB::totalBytes(const Settings & /*settings*/) const
{
std::shared_lock lock(rocksdb_ptr_mx);
if (!rocksdb_ptr)
return {};
UInt64 estimated_bytes;
if (!rocksdb_ptr->GetIntProperty("rocksdb.estimate-live-data-size", &estimated_bytes))
return {};
return estimated_bytes;
}
}

View File

@ -5,6 +5,7 @@
#include <Storages/IStorage.h>
#include <Interpreters/IKeyValueEntity.h>
#include <rocksdb/status.h>
#include <Storages/RocksDB/EmbeddedRocksDBSink.h>
namespace rocksdb
@ -89,6 +90,8 @@ public:
std::optional<UInt64> totalRows(const Settings & settings) const override;
std::optional<UInt64> totalBytes(const Settings & settings) const override;
private:
const String primary_key;
using RocksDBPtr = std::unique_ptr<rocksdb::DB>;

View File

@ -1 +1,6 @@
-- { echoOn }
SELECT count() FROM dict SETTINGS optimize_trivial_approximate_count_query = 0, max_rows_to_read = 1; -- { serverError TOO_MANY_ROWS }
SELECT count() FROM dict SETTINGS max_rows_to_read = 1;
121
SELECT total_rows FROM system.tables WHERE database = currentDatabase() AND name = 'dict';
121

View File

@ -2,5 +2,7 @@
CREATE TABLE dict (key UInt64, value String) ENGINE = EmbeddedRocksDB PRIMARY KEY key;
INSERT INTO dict SELECT number, toString(number) FROM numbers(121);
-- { echoOn }
SELECT count() FROM dict SETTINGS optimize_trivial_approximate_count_query = 0, max_rows_to_read = 1; -- { serverError TOO_MANY_ROWS }
SELECT count() FROM dict SETTINGS optimize_trivial_approximate_count_query = 1, max_rows_to_read = 1;
SELECT count() FROM dict SETTINGS max_rows_to_read = 1;
SELECT total_rows FROM system.tables WHERE database = currentDatabase() AND name = 'dict';