Yandex Cloud
Search
Contact UsTry it for free
  • Customer Stories
  • Documentation
  • Blog
  • All Services
  • System Status
  • Marketplace
    • Featured
    • Infrastructure & Network
    • Data Platform
    • AI for business
    • Security
    • DevOps tools
    • Serverless
    • Monitoring & Resources
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Center for Technologies and Society
    • Yandex Cloud Partner program
    • Price calculator
    • Pricing plans
  • Customer Stories
  • Documentation
  • Blog
© 2026 Direct Cursus Technology L.L.C.
Yandex Managed Service for ClickHouse®
  • Getting started
    • All tutorials
    • Adding data to a database
    • Migrating data to Managed Service for ClickHouse® using ClickHouse®
    • Migrating data to Managed Service for ClickHouse® using Data Transfer
    • Sharding tables
    • Using hybrid storage
    • Fetching data from Managed Service for Apache Kafka®
    • Fetching data from RabbitMQ
    • Exchanging data with Yandex Data Processing
    • Configuring Yandex Cloud DNS for cluster access from other cloud networks
    • Analyzing Yandex Object Storage logs using Yandex DataLens
    • Configuring Managed Service for ClickHouse® for Graphite
    • Saving a data stream from Yandex Data Streams to Managed Service for ClickHouse®
    • Migrating a database from Google BigQuery
    • Delivering data from Managed Service for Apache Kafka® using Yandex Data Transfer
    • Migrating data from Yandex Direct using Yandex Cloud Functions, Yandex Object Storage, and Yandex Data Transfer
    • Loading data from Yandex Object Storage to Managed Service for ClickHouse® using Yandex Data Transfer
    • Migrating a database from Greenplum® to ClickHouse®
    • Migrating a database from MySQL® to ClickHouse® using Yandex Data Transfer
    • Asynchronously replicating data from PostgreSQL to ClickHouse®
    • Loading data from Yandex Managed Service for YDB to Managed Service for ClickHouse® using Yandex Data Transfer
    • Copying data from Managed Service for OpenSearch to Managed Service for ClickHouse® using Yandex Data Transfer
    • Ingesting data into storage systems
    • Using parameters
    • Examples of creating QL charts
    • Web analytics with funnels and cohorts calculated based on Yandex Metrica data
    • AppMetrica: direct connection
    • AppMetrica: data export, post-processing, and visualization
    • Loading data from Yandex Metrica to a ClickHouse® data mart
    • Yandex Tracker: data export and visualization
    • Retail chain's dashboard based on a ClickHouse® database
    • Analyzing sales and locations of pizzerias based on data from a ClickHouse® database and Marketplace
    • Geocoding with the Yandex Maps API for data visualization in DataLens
    • Importing data from Object Storage, processing, and exporting it to Managed Service for ClickHouse®
    • Working with data using Query
    • Federated data queries using Query
    • Integration with an external Microsoft SQL Server database via ClickHouse® JDBC Bridge
    • Integration with an external Oracle database via ClickHouse® JDBC Bridge
  • Access management
  • Pricing policy
  • Terraform reference
  • Yandex Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes

In this article:

  • Required paid resources
  • Getting started
  • Set up the infrastructure
  • Set up the clickhouse-client
  • Create tables with data
  • Classic sharding
  • Group-based sharding
  • Advanced group-based sharding
  • Test the tables
  • Delete the resources you created
  1. Tutorials
  2. Sharding tables

Table sharding in ClickHouse®

Written by
Yandex Cloud
Updated at January 22, 2026
  • Required paid resources
  • Getting started
    • Set up the infrastructure
    • Set up the clickhouse-client
  • Create tables with data
    • Classic sharding
    • Group-based sharding
    • Advanced group-based sharding
  • Test the tables
  • Delete the resources you created

Sharding provides a number of benefits when dealing with high query rates and massive datasets. It works by creating a distributed table that routes queries to underlying tables. You can access data in sharded tables both directly or through the distributed table.

There are three primary sharding strategies:

  • Classic approach, where the distributed table uses all shards in the cluster.
  • Group-based approach, where some shards are grouped together.
  • Advanced group-based approach, where shards are divided into two groups: one for the distributed table and the other for the underlying tables.

Below are configuration examples for all three sharding methods.

For more information, see Sharding in Managed Service for ClickHouse®.

To set up sharding:

  1. Create tables with data.
  2. Test the tables.

If you no longer need the resources you created, delete them.

Required paid resourcesRequired paid resources

The support cost for this solution includes:

  • Managed Service for ClickHouse® cluster fee, which covers the use of computing resources allocated to hosts (including ZooKeeper hosts) and disk space (see Managed Service for ClickHouse® pricing).
  • Fee for using public IP addresses if public access is enabled for cluster hosts (see Virtual Private Cloud pricing).

Getting startedGetting started

Set up the infrastructureSet up the infrastructure

Manually
Terraform
  1. Create a Managed Service for ClickHouse® cluster:

    • Cluster name: chcluster.

    • Disk type: Select the required disk type.

      It will determine the minimum number of hosts per shard:

      • Two hosts, if you select local SSDs (local-ssd).
      • Three hosts, if you select network non-replicated SSDs (network-ssd-nonreplicated).

      To ensure fault tolerance using these disk types, you must add redundant hosts.

      For more information, see Storage in Managed Service for ClickHouse®.

    • DB name: tutorial.

    Cluster hosts must be accessible from the internet.

  2. Create two additional shards named shard2 and shard3.

  3. Add three ZooKeeper hosts to the cluster.

  4. Create shard groups. The number of shard groups depends on the sharding type:

    • Group-based sharding requires one group named sgroup that will include shard1 and shard2.
    • Advanced group-based sharding requires two groups:
      • sgroup including shard1 and shard2.
      • sgroup_data including shard3.

    No shard groups are needed for classic sharding.

  5. If using security groups, configure them to allow internet access to your cluster.

  1. If you do not have Terraform yet, install it.

  2. Get the authentication credentials. You can add them to environment variables or specify them later in the provider configuration file.

  3. Configure and initialize a provider. There is no need to create a provider configuration file manually, you can download it.

  4. Place the configuration file in a separate working directory and specify the parameter values. If you did not add the authentication credentials to environment variables, specify them in the configuration file.

  5. Download one of the following sharding example configuration files to your current working directory:

    • simple-sharding.tf: Classic sharding.
    • sharding-with-groups.tf: Group-based sharding.
    • advanced-sharding-with-groups.tf: Advanced group-based sharding.

    Each file describes the following:

    • Network.
    • Subnet.
    • Default security group and rules for connecting to the cluster from the internet.
    • Managed Service for ClickHouse® cluster with the required hosts and shards.
  6. In the configuration file, specify the username and password that will be used to access the Managed Service for ClickHouse® cluster.

  7. Make sure the Terraform configuration files are correct using this command:

    terraform validate
    

    Terraform will show any errors found in your configuration files.

  8. Create the required infrastructure:

    1. Run this command to view the planned changes:

      terraform plan
      

      If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.

    2. If everything looks correct, apply the changes:

      1. Run this command:

        terraform apply
        
      2. Confirm updating the resources.

      3. Wait for the operation to complete.

    All the required resources will be created in the specified folder. You can check resource availability and their settings in the management console.

Set up the clickhouse-clientSet up the clickhouse-client

Install and configure the clickhouse-client for database access.

Create tables with dataCreate tables with data

Let’s assume you need to enable sharding for the hits_v1 table. The create table statement text will depend on your chosen sharding strategy.

Replace the <table_structure> placeholder with column descriptions from this ClickHouse® guide.

Once you enable sharding (by any method), you will be able to send SELECT and INSERT queries to the distributed table. These queries will be processed according to the specified configuration.

In the following examples, we use a random number, rand(), as a sharding key.

Classic shardingClassic sharding

In this example, the distributed table built from hits_v1 uses every shard in the chcluster cluster: shard1, shard2, and shard3.

Before operating the distributed table:

  1. Connect to the tutorial database.

  2. Create the hits_v1 table on every host in the cluster using the MergeTree engine:

    CREATE TABLE tutorial.hits_v1 ON CLUSTER '{cluster}' ( <table_structure> )
    ENGINE = MergeTree()
    PARTITION BY toYYYYMM(EventDate)
    ORDER BY (CounterID, EventDate, intHash32(UserID))
    SAMPLE BY intHash32(UserID)
    SETTINGS index_granularity = 8192
    

To create a distributed table named hits_v1_distributed in the cluster:

  1. Connect to the tutorial database.

  2. Create a Distributed table:

    CREATE TABLE tutorial.hits_v1_distributed ON CLUSTER '{cluster}' AS tutorial.hits_v1
    ENGINE = Distributed('{cluster}', tutorial, hits_v1, rand())
    

    Here, you can use the AS tutorial.hits_v1 expression instead of explicitly stating the table structure since both tables, hits_v1_distributed and hits_v1, reside on the same cluster hosts.

    When creating a Distributed table, use chcluster as the cluster ID. You can get it with the list of clusters in the folder.

    Tip

    You can use the {cluster} macro instead of the cluster ID. When running the query, ClickHouse will automatically substitute it with the ID of the cluster where the CREATE TABLE statement is running.

Group-based shardingGroup-based sharding

In this example:

  • We use a single shard group named sgroup.
  • The distributed table and its underlying table, hits_v1, are in the same sgroup shard group within the cluster.

Before operating the distributed table:

  1. Connect to the tutorial database.

  2. Create the hits_v1 table on every host in the sgroup shard group, using the MergeTree engine:

    CREATE TABLE tutorial.hits_v1 ON CLUSTER sgroup ( <table_structure> )
    ENGINE = MergeTree()
    PARTITION BY toYYYYMM(EventDate)
    ORDER BY (CounterID, EventDate, intHash32(UserID))
    SAMPLE BY intHash32(UserID)
    SETTINGS index_granularity = 8192
    

To create a distributed table named tutorial.hits_v1_distributed in the cluster:

  1. Connect to the tutorial database.

  2. Create a Distributed table:

    CREATE TABLE tutorial.hits_v1_distributed ON CLUSTER sgroup AS tutorial.hits_v1
    ENGINE = Distributed(sgroup, tutorial, hits_v1, rand())
    

    Here, you can use the AS tutorial.hits_v1 expression instead of explicitly stating the table structure since both tables, hits_v1_distributed and hits_v1, reside on the same cluster hosts within a single shard.

Advanced group-based shardingAdvanced group-based sharding

In this example:

  1. We use the sgroup and sgroup_data shard groups.
  2. The distributed table resides in the sgroup shard group.
  3. The hits_v1 underlying table resides in the sgroup_data shard group.

Before operating the distributed table:

  1. Connect to the tutorial database.

  2. Create the hits_v1 table on every host of the sgroup_data shard group, using the ReplicatedMergeTree engine:

    CREATE TABLE tutorial.hits_v1 ON CLUSTER sgroup_data ( <table_structure> )
    ENGINE = ReplicatedMergeTree('/tables/{shard}/hits_v1', '{replica}')
    PARTITION BY toYYYYMM(EventDate)
    ORDER BY (CounterID, EventDate, intHash32(UserID))
    SAMPLE BY intHash32(UserID)
    SETTINGS index_granularity = 8192
    

    The ReplicatedMergeTree engine will provide fault tolerance to this solution.

To create a distributed table named tutorial.hits_v1_distributed in the cluster:

  1. Connect to the tutorial database.

  2. Create a Distributed table:

    CREATE TABLE tutorial.hits_v1_distributed ON CLUSTER sgroup ( <table_structure> )
    ENGINE = Distributed(sgroup_data, tutorial, hits_v1, rand())
    

    Here you must explicitly specify the table structure because the hits_v1_distributed and hits_v1 tables reside on different hosts in separate shards.

Test the tablesTest the tables

To test your new distributed table named tutorial.hits_v1_distributed:

  1. Load the hits_v1 test dataset:

    curl https://storage.yandexcloud.net/doc-files/managed-clickhouse/hits_v1.tsv.xz | unxz --threads=`nproc` > hits_v1.tsv
    
  2. Populate the table with test data:

    clickhouse-client \
       --host "<FQDN_of_any_host_with_distributed_table>" \
       --secure \
       --port 9440 \
       --user "<username>" \
       --password "<user_password>" \
       --database "tutorial" \
       --query "INSERT INTO tutorial.hits_v1_distributed FORMAT TSV" \
       --max_insert_block_size=100000 < hits_v1.tsv
    

    You can get the host names with the list of ClickHouse® hosts in the cluster.

  3. Run one or multiple test queries against this table. For example, you can get the table row count:

    SELECT count() FROM tutorial.hits_v1_distributed
    

    Result:

    8873898
    

Delete the resources you createdDelete the resources you created

Delete the resources you no longer need to avoid paying for them:

Manually
Terraform
  1. Delete the Managed Service for ClickHouse® cluster.
  2. If you used static public IP addresses for cluster access, release and delete them.
  1. In the terminal window, go to the directory containing the infrastructure plan.

    Warning

    Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.

  2. Delete resources:

    1. Run this command:

      terraform destroy
      
    2. Confirm deleting the resources and wait for the operation to complete.

    All the resources described in the Terraform manifests will be deleted.

ClickHouse® is a registered trademark of ClickHouse, Inc.

Was the article helpful?

Previous
Migrating data to Managed Service for ClickHouse® using Data Transfer
Next
Using hybrid storage
© 2026 Direct Cursus Technology L.L.C.