Giter VIP home page Giter VIP logo

ch-partitions-dropper's Introduction

CH partitions dropper

Bash-script that drops all the partitions of all tables in one database in ClickHouse EXCEPT the latest one.

Before you run partitions dropper you should put your ClickHouse credentials into .secret file.

Than you should run db_partitions_dropper.sh with chosen database name as an argument.

Example

Let's say we have .secret file with your ClickHouse username and its password

cat .secret
CH_user CH_pass

And we have database called test_partitions. There are 3 tables and they all have different amount of partitions. Let's see how many:

SELECT
    table,
    uniqExact(partition) AS partitions_cnt,
    MAX(partition) AS latest_partition
FROM system.parts
WHERE active AND (database = 'test_partitions')
GROUP BY table
ORDER BY table ASC;

┌─table────────────┬─partitions_cnt─┬─latest_partition─┐
│ test_1_partition │              1 │ 2                │
│ test_2_partition │              3 │ 2                │
│ test_3_partition │            100 │ 99               │
└──────────────────┴────────────────┴──────────────────┘

All of them have partition key based on the number, db_partitions_dropper.sh script will leave one partition per table with the highest number assuming that it is the latest. If your partition key is toYYYYMM(dt) than the latest partition name corresponds to the highest number.

After running the script we expect to see only one partition in each table.

./db_partitions_dropper.sh test_partitions

After running the same ClickHouse query we see the expected results

SELECT
    table,
    uniqExact(partition) AS partitions_cnt,
    MAX(partition) AS latest_partition
FROM system.parts
WHERE active AND (database = 'test_partitions')
GROUP BY table
ORDER BY table ASC;

┌─table────────────┬─partitions_cnt─┬─latest_partition─┐
│ test_1_partition │              1 │ 2                │
│ test_2_partition │              1 │ 2                │
│ test_3_partition │              1 │ 99               │
└──────────────────┴────────────────┴──────────────────┘

ch-partitions-dropper's People

Contributors

cmbcksrl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.