Skip to content

Instantly share code, notes, and snippets.

View redsquare's full-sized avatar
📞
OnCall

steve flitcroft redsquare

📞
OnCall
View GitHub Profile
:) CREATE TABLE ranges (id UInt64, date_start Date, date_end Date) ENGINE = TinyLog;
:) INSERT INTO ranges SELECT rowNumberInAllBlocks() AS id, least(dates[1], dates[2]) AS date_start, greatest(dates[1], dates[2]) AS date_end FROM (SELECT arrayJoin(arrayMap(d -> [today() - d - rand64(d) % 10, yesterday() - d - rand(d) % 10], range(10))) AS dates);
:) CREATE TABLE dates (id UInt64, date Date) ENGINE = TinyLog;
:) INSERT INTO dates SELECT rowNumberInAllBlocks() AS id, date FROM (SELECT arrayJoin(arrayMap(d -> today() - rand64(d) % 10, range(10))) AS date);
@redsquare
redsquare / dump_restore_clickhouse.sql
Last active March 14, 2024 16:11
Dump Clickhouse Table Schemas
#dump table ddl's
clickhouse-client --host your_clickhouse_host -q "SHOW TABLES FROM db_name" | while read -r table_name; do
clickhouse-client --host your_clickhouse_host -q "SHOW CREATE TABLE db_name.\`$table_name\`" >> db_structure.sql
done
#restore
clickhouse-client --host target_clickhouse_host < db_structure.sql
#also in /var/lib/clickhouse/metadata/
SELECT
table,
partition_id,
count(name) AS parts,
formatReadableSize(sum(bytes_on_disk)) AS size
FROM system.parts
WHERE (table = 'qw_3_requests_quotes') AND active AND (toUInt32(partition_id) < 20231001)
GROUP BY
table,
partition_id
@redsquare
redsquare / clickhouse_parts.sql
Last active March 15, 2024 13:24
clickhouse parts query
SELECT
table,
partition_id,
count(name) AS parts,
formatReadableSize(sum(bytes_on_disk)) AS size
FROM system.parts
WHERE (table = 'qw_3_requests_quotes') AND active AND (toUInt32(partition_id) < 20231001)
GROUP BY
table,
partition_id