Yandex Cloud
Search
Discuss with expertTry it for free
  • Customer Stories
  • Documentation
  • Blog
  • All Services
    • Cloud Interconnect
    • Cloud Backup
    • Cloud Registry
    • Yandex AI Studio
    • Compute Cloud
    • Object Storage
    • Managed Service for Kubernetes®
    • Yandex BareMetal
    • Smart Web Security
    • Security Deck
    • Managed Service for PostgreSQL
    • Managed Service for ClickHouse®
    • Monium
    • Cloud CDN
    • Network Load Balancer
    • Virtual Private Cloud
    • Cloud DNS
    • Application Load Balancer
    • Yandex Cloud Video
    • Stackland
    • Yandex Cloud Router
    • Yandex Managed Service for Trino
    • Managed Service for MySQL®
    • Managed Service for Valkey™
    • Managed Service for Apache Spark™
    • Yandex StoreDoc
    • Managed Service for OpenSearch
    • Managed Service for Apache Kafka®
    • Data Transfer
    • Yandex MPP Analytics Engine for PostgreSQL
    • Yandex Managed Service for Apache Airflow®
    • Data Processing
    • Yandex MetaData Hub
    • Managed Service for YDB
    • Managed Service for Sharded PostgreSQL
    • Managed Service for YTsaurus
    • Yandex WebSQL
    • DataLens
    • Yandex Search API
    • SpeechSense
    • SpeechKit
    • DataSphere
    • Vision OCR
    • Translate
    • Yandex Identity Hub
    • Key Management Service
    • Certificate Manager
    • Yandex Lockbox
    • Audit Trails
    • SmartCaptcha
    • Cloud Desktop
    • Yandex SIEM
    • SourceCraft Code Assistant
    • Container Registry
    • Managed Service for GitLab
    • Managed Service for Prometheus®
    • Cloud Functions
    • API Gateway
    • Yandex Cloud Postbox
    • Message Queue
    • Serverless Integrations
    • IoT Core
    • Data Streams
    • Serverless Containers
    • Cloud Notification Service
    • Yandex Query
    • Identity and Access Management
    • Yandex Cloud Console
    • Resource Manager
    • Yandex Cloud Billing
    • Yandex Cloud Quota Manager
    • Cloud Apps
  • 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
    • Executing analytical queries in Managed Service for PostgreSQL with processing in Managed Service for ClickHouse®
  • Access management
  • Inspections and recommendations
  • Pricing policy
  • Terraform reference
  • Yandex Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes

In this article:

  • Required paid resources
  • Getting started
  • Set up your infrastructure
  • Set up the clickhouse-client
  • Create tables with data
  • All shards
  • Single shard group
  • Two shard groups
  • Test the tables
  • Delete the resources you created
  1. Tutorials
  2. Sharding tables

Table sharding in ClickHouse®

Written by
Yandex Cloud
Updated at September 14, 2026
View in Markdown
  • Required paid resources
  • Getting started
    • Set up your infrastructure
    • Set up the clickhouse-client
  • Create tables with data
    • All shards
    • Single shard group
    • Two shard groups
  • Test the tables
  • Delete the resources you created

In a sharded ClickHouse® cluster, you can distribute table data across all cluster shards or only some of them, i.e., a shard group.

You can select one of the following three topologies to implement distributed storage of table data on cluster shards:

  • All shards: the local and distributed tables are created across all shards within a cluster.
  • Single shard group: the local and distributed tables are created on all shards within a group.
  • Two shard groups: the local table is created on the shards of one group, while the distributed table is deployed on shards of the second group.

To split table data into shards:

  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

  • Managed Service for ClickHouse® cluster: use of computing resources allocated to hosts (including ZooKeeper hosts) and disk space (see Managed Service for ClickHouse® pricing).
  • Public IP addresses if public access is enabled for cluster hosts (see Virtual Private Cloud pricing).

Getting startedGetting started

Set up your infrastructureSet up your infrastructure

Manually
Terraform
  1. Create a Managed Service for ClickHouse® cluster with publicly available hosts and configure it as follows:

    • 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.

    Note

    Public access to cluster hosts is required if you plan to connect to the cluster via the internet. This connection option is simpler and is recommended for the purposes of this guide. You can connect to non-public hosts as well but only from Yandex Cloud virtual machines located in the same cloud network as the cluster.

  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. Validate your Terraform configuration files using this command:

    terraform validate
    

    Terraform will display any configuration errors detected in your 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 we need to distribute the hits_v1 table data across shards.

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

In the following examples, we use the rand() expression as a sharding key to randomly distribute data across shards.

All shardsAll shards

In this example, the distributed and local tables are created on all cluster shards: shard1, shard2, and shard3.

Before creating a distributed table:

  1. Connect to the tutorial database.

  2. Create the hits_v1 MergeTree table on all cluster hosts:

    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.

Single shard groupSingle shard group

In this example, the distributed and local tables are created within a single shard group, sgroup.

Before creating a distributed table:

  1. Connect to the tutorial database.

  2. Create the hits_v1 MergeTree table on all hosts within the sgroup shard group:

    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.

Two shard groupsTwo shard groups

In this example:

  • The distributed table is created within the sgroup shard group.
  • The local table is created within the sgroup_data shard group.

Before creating a distributed table:

  1. Connect to the tutorial database.

  2. Create the hits_v1 ReplicatedMergeTree local table on all hosts within the sgroup_data shard group:

    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 the Distributed table on all hosts within the sgroup shard group:

    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.