TRUNCATE TABLE system.error_log;

SELECT
    table,
    count() AS active_parts,
    avg(data_uncompressed_bytes) AS avg_part_size,
    sum(bytes_on_disk) AS total_size
FROM system.parts
WHERE active
GROUP BY table
ORDER BY active_parts DESC LIMIT 100;

OPTIMIZE TABLE quotes.quotes FINAL;

SELECT *
FROM system.merges
WHERE table = 'quotes' AND database = 'quotes';

CHECK TABLE quotes.quotes;

SELECT count(*) FROM quotes.quotes;


SELECT 
    count() AS active_parts,
    sum(data_uncompressed_bytes) AS total_size,
    avg(data_uncompressed_bytes) AS avg_part_size
FROM system.parts
WHERE active AND table = 'quotes';

SELECT name, value
FROM system.settings
WHERE name LIKE 'parts_%' OR name LIKE 'merge_%';

SET parts_to_delay_insert = 1000;
SET parts_to_throw_insert = 3000;
SET log_processors_profiles = 0;


SELECT value FROM system.settings WHERE name LIKE 'background_pool_size';


SELECT value FROM system.settings WHERE name='background_pool_size';



CREATE TABLE quotes.quotes_new
(
    `exchange` LowCardinality(String),
    `pair` LowCardinality(String),
    `price` Decimal(18, 8) CODEC(Delta(8), ZSTD(1)),
    `timestamp` DateTime64(3),
    `deal_count` Decimal(18, 4) CODEC(Delta(8), ZSTD(1)),
    INDEX idx_timestamp timestamp TYPE minmax GRANULARITY 4,
    INDEX idx_exchange exchange TYPE set(100) GRANULARITY 4,
    INDEX idx_pair pair TYPE set(1000) GRANULARITY 4
)
ENGINE = MergeTree
PARTITION BY toYYYYMMDD(timestamp)
ORDER BY (exchange, pair, timestamp)
TTL toDateTime(timestamp) + toIntervalMonth(12)
SETTINGS index_granularity = 8192;

SELECT name, partition, active
FROM system.parts
WHERE table = 'quotes' AND database = 'quotes' AND active = 1;

INSERT INTO quotes_new
SELECT *
FROM quotes.quotes
WHERE timestamp NOT BETWEEN '2025-12-29 00:00:00' AND '2025-12-29 23:59:59';

DROP TABLE quotes.quotes;

RENAME TABLE quotes.quotes_new TO quotes.quotes;

OPTIMIZE TABLE system.query_log FINAL;
OPTIMIZE TABLE system.error_log FINAL;
OPTIMIZE TABLE system.part_log FINAL;
OPTIMIZE TABLE system.processors_profile_log FINAL;

OPTIMIZE TABLE quotes.quotes FINAL;