[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for ClickHouse®](../index.md) > [Step-by-step guides](index.md) > Storing and processing data > Working with hybrid storage

# Working with hybrid storage

[Hybrid storage](../concepts/storage.md#hybrid-storage-features) enables you to store frequently used data in cluster storage (on the Managed Service for ClickHouse® cluster's network drives) while keeping rarely used data in [Yandex Object Storage](../../storage/index.md).


## Enabling hybrid storage for a cluster {#enable-hybrid-storage}

{% note warning %}

Once hybrid storage is enabled, you cannot disable it.

{% endnote %}


### Enabling hybrid storage when creating a cluster {#cluster-create}

{% list tabs group=instructions %}

- Management console {#console}

    1. In the [management console](https://console.yandex.cloud), select the folder where you want to create a ClickHouse® cluster.
    1. [Navigate](https://console.yandex.cloud/link/managed-clickhouse) to **Managed Service for&nbsp;ClickHouse**.
    1. Click **Create cluster** and set the cluster parameters. To learn more about parameters, see the [guide on creating a cluster](cluster-create.md).
    1. Enable **Hybrid storage** under **DBMS settings** and, optionally, set the hybrid storage settings:

        * **Data caching**: Enables caching data accessed from the object storage in the cluster storage. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing.
        
            By default, data caching is enabled.
        
        * **Cache size**: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
        
        * **Min. share of free space on cluster disks**: Sets the minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
        
            Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.

    1. Click **Create cluster**.

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`.
    
    If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    To create a cluster with hybrid storage enabled and, optionally, configure its settings:

    1. View the description of the CLI command for creating a cluster:

        ```bash
        yc managed-clickhouse cluster create --help
        ```

    1. Run this command, specifying hybrid storage settings:

        ```bash
        yc managed-clickhouse cluster create \
           ...
           --cloud-storage=true \
           --cloud-storage-data-cache=<cache_data> \
           --cloud-storage-data-cache-max-size=<maximum_memory_in_bytes> \
           --cloud-storage-move-factor=<share_of_free_space> \
           --cloud-storage-prefer-not-to-merge=<disable_merging_data_parts>
          ...
        ```

        Where:

        * `--cloud-storage`: Enables hybrid storage.

        * `--cloud-storage-data-cache`: Enables caching data accessed from the object storage in the cluster storage. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
        
        * `--cloud-storage-data-cache-max-size`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
        
        * `--cloud-storage-move-factor`: Sets the minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to Yandex Object Storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
        
            Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
        
        * `--cloud-storage-prefer-not-to-merge`: Disables [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in cluster and object storages. To disable merges, set the parameter to `true` or provide it with no value. To keep merges enabled, set the parameter to `false` or do not provide it in the CLI command when creating a cluster.
        
            Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

        To learn more about the `yc managed-clickhouse cluster create` command's other parameters, see the [guide on creating a cluster](cluster-create.md).

- Terraform {#tf}

    1. Create a Terraform configuration file describing your [infrastructure](cluster-create.md).
    1. Add the `cloud_storage` section to the configuration file:

        ```hcl
        resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
          ...
          cloud_storage = {
            enabled             = true
            move_factor         = <share_of_free_space>
            data_cache_enabled  = <cache_data>
            data_cache_max_size = <maximum_memory_in_bytes>
            prefer_not_to_merge = <disable_merging_data_parts>
          }
        }
        ```

        Where:

        * `enabled`: Enables hybrid storage.

        * `move_factor`: Minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
        
            Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
        
        * `data_cache_enabled`: Allows caching data accessed from the object storage in the cluster storage, `true` or `false`. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
        
        * `data_cache_max_size`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
        
        * `prefer_not_to_merge`: Disable [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in the cluster and object storage, `true` or `false`. To disable merging, set to `true`. To leave merging enabled, set to `false`.
        
            Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

    1. Make sure the Terraform configuration file is correct:

        1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
        1. Run this command:
        
           ```bash
           terraform validate
           ```
        
           Terraform will show any errors found in your configuration files.

    1. Create a cluster:

        1. Run this command to view the intended changes:
        
           ```bash
           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.
        
        1. If everything looks correct, apply the changes:
        
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

    {% note warning "Timeouts" %}
    
    The Terraform provider sets the following timeouts for Managed Service for ClickHouse® cluster operations:
    
    * Creating a cluster, including by restoring from a backup: 60 minutes.
    * Updating a cluster: 90 minutes.
    * Deleting a cluster: 30 minutes.
    
    Operations exceeding the timeout are aborted.
    
    {% cut "How to change these limits" %}
    
    Add the `timeouts` section to your cluster description, such as the following:
    
    ```hcl
    resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
      ...
      timeouts = {
        create = "1h30m" # 1 hour 30 minutes
        update = "2h"    # 2 hours
        delete = "30m"   # 30 minutes
      }
    }
    ```
    
    {% endcut %}
    
    {% endnote %}

- REST API {#api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Call the [Cluster.Create](../api-ref/Cluster/create.md) method, e.g., via the following [cURL](https://curl.se/) request:

        1. Create a file named `body.json` and paste the following code into it:

            ```json
            {
              <cluster_parameters>
              ...
              "configSpec": {
                "cloudStorage": {
                  "enabled": true,
                  "moveFactor": "<share_of_free_space>",
                  "dataCacheEnabled": <cache_data>,
                  "dataCacheMaxSize": "<maximum_memory_in_bytes>",
                  "preferNotToMerge": <disable_merging_data_parts>
                },
                ...
              }
            }
            ```

            Where:

            * `enabled`: Enables hybrid storage.

            * `moveFactor`: Minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
            
                Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
            
            * `dataCacheEnabled`: Allows caching data accessed from the object storage in the cluster storage, `true` or `false`. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
            
            * `dataCacheMaxSize`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
            
            * `preferNotToMerge`: Disable [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in the cluster and object storage, `true` or `false`. To disable merging, set to `true`. To leave merging enabled, set to `false`.
            
                Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

            To learn more about other parameters, see the [guide on creating a cluster](cluster-create.md).

        1. Run this request:

            ```bash
            curl \
              --request POST \
              --header "Authorization: Bearer $IAM_TOKEN" \
              --header "Content-Type: application/json" \
              --url 'https://mdb.api.cloud.yandex.net/managed-clickhouse/v1/clusters' \
              --data '@body.json'
            ```

    1. View the [server response](../api-ref/Cluster/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
       
       ```bash
       cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
       ```
       
       Below, we assume that the repository contents reside in the `~/cloudapi/` directory.

    1. Call the [ClusterService.Create](../api-ref/grpc/Cluster/create.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

        1. Create a file named `body.json` and paste the following code into it:

            ```json
            {
              <cluster_parameters>
              ...
              "config_spec": {
                "cloud_storage": {
                  "enabled": true,
                  "move_factor": "<share_of_free_space>",
                  "data_cache_enabled": <cache_data>,
                  "data_cache_max_size": "<maximum_memory_in_bytes>",
                  "prefer_not_to_merge": <disable_merging_data_parts>
                },
                ...
              }
            }
            ```

            Where:

            * `enabled`: Enables hybrid storage.

            * `move_factor`: Minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
            
                Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
            
            * `data_cache_enabled`: Allows caching data accessed from the object storage in the cluster storage, `true` or `false`. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
            
            * `data_cache_max_size`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
            
            * `prefer_not_to_merge`: Disable [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in the cluster and object storage, `true` or `false`. To disable merging, set to `true`. To leave merging enabled, set to `false`.
            
                Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

            To learn more about other parameters, see the [guide on creating a cluster](cluster-create.md).

        1. Run this request:

            ```bash
            grpcurl \
              -format json \
              -import-path ~/cloudapi/ \
              -import-path ~/cloudapi/third_party/googleapis/ \
              -proto ~/cloudapi/yandex/cloud/mdb/clickhouse/v1/cluster_service.proto \
              -rpc-header "Authorization: Bearer $IAM_TOKEN" \
              -d @ \
              mdb.api.cloud.yandex.net:443 \
              yandex.cloud.mdb.clickhouse.v1.ClusterService.Create \
              < body.json
            ```

    1. Check the [server response](../api-ref/grpc/Cluster/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}


### Enabling hybrid storage for an existing cluster {#cluster-update}

{% list tabs group=instructions %}

- Management console {#console}

    1. In the [management console](https://console.yandex.cloud), select the folder containing the cluster.
    1. [Navigate](https://console.yandex.cloud/link/managed-clickhouse) to **Managed Service for&nbsp;ClickHouse**.
    1. Select your cluster and click **Edit** in the top panel.
    1. Enable **Hybrid storage** under **DBMS settings** and, optionally, set the hybrid storage settings:

        * **Data caching**: Enables caching data accessed from the object storage in the cluster storage. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing.
        
            By default, data caching is enabled.
        
        * **Cache size**: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
        
        * **Min. share of free space on cluster disks**: Sets the minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
        
            Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.

    1. Click **Save changes**.

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`.
    
    If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    To enable hybrid storage in a cluster and, optionally, configure its settings, run this command:

    ```bash
    yc managed-clickhouse cluster update <cluster_name_or_ID> \
       --cloud-storage=true \
       --cloud-storage-data-cache=<cache_data> \
       --cloud-storage-data-cache-max-size=<maximum_memory_in_bytes> \
       --cloud-storage-move-factor=<share_of_free_space> \
       --cloud-storage-prefer-not-to-merge=<disable_merging_data_parts>
    ```

    Where:

    * `<cluster_name_or_ID>`: Cluster name or ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.
    * `--cloud-storage`: Enables hybrid storage.

    * `--cloud-storage-data-cache`: Enables caching data accessed from the object storage in the cluster storage. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
    
    * `--cloud-storage-data-cache-max-size`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
    
    * `--cloud-storage-move-factor`: Sets the minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to Yandex Object Storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
    
        Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
    
    * `--cloud-storage-prefer-not-to-merge`: Disables [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in cluster and object storages. To disable merges, set the parameter to `true` or provide it with no value. To keep merges enabled, set the parameter to `false` or do not provide it in the CLI command when creating a cluster.
    
        Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

- Terraform {#tf}

    To enable hybrid storage in a cluster and, optionally, configure its settings:

    1. Open the current Terraform configuration file with the infrastructure plan.

        To learn how to create this file, see [Creating a cluster](cluster-create.md).

        For the complete list of configurable Managed Service for ClickHouse® cluster fields, see [this Terraform provider guide](../../terraform/resources/mdb_clickhouse_cluster.md).

    1. Add the `cloud_storage` section to the Managed Service for ClickHouse® cluster description:

        ```hcl
        resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
          ...
          cloud_storage = {
            enabled             = true
            move_factor         = <share_of_free_space>
            data_cache_enabled  = <cache_data>
            data_cache_max_size = <maximum_memory_in_bytes>
            prefer_not_to_merge = <disable_merging_data_parts>
          }
        }
        ```

        Where:

        * `enabled`: Enables hybrid storage.

        * `move_factor`: Minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
        
            Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
        
        * `data_cache_enabled`: Allows caching data accessed from the object storage in the cluster storage, `true` or `false`. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
        
        * `data_cache_max_size`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
        
        * `prefer_not_to_merge`: Disable [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in the cluster and object storage, `true` or `false`. To disable merging, set to `true`. To leave merging enabled, set to `false`.
        
            Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

    1. Make sure the Terraform configuration file is correct:

        1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
        1. Run this command:
        
           ```bash
           terraform validate
           ```
        
           Terraform will show any errors found in your configuration files.

    1. Confirm resource changes:

        1. Run this command to view the intended changes:
        
           ```bash
           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.
        
        1. If everything looks correct, apply the changes:
        
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

    {% note warning "Timeouts" %}
    
    The Terraform provider sets the following timeouts for Managed Service for ClickHouse® cluster operations:
    
    * Creating a cluster, including by restoring from a backup: 60 minutes.
    * Updating a cluster: 90 minutes.
    * Deleting a cluster: 30 minutes.
    
    Operations exceeding the timeout are aborted.
    
    {% cut "How to change these limits" %}
    
    Add the `timeouts` section to your cluster description, such as the following:
    
    ```hcl
    resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
      ...
      timeouts = {
        create = "1h30m" # 1 hour 30 minutes
        update = "2h"    # 2 hours
        delete = "30m"   # 30 minutes
      }
    }
    ```
    
    {% endcut %}
    
    {% endnote %}

- REST API {#api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Call the [Cluster.Update](../api-ref/Cluster/update.md) method, e.g., via the following [cURL](https://curl.se/) request:

        {% note warning %}
        
        The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `updateMask` parameter as a single comma-separated string.
        
        {% endnote %}

        ```bash
        curl \
            --request PATCH \
            --header "Authorization: Bearer $IAM_TOKEN" \
            --header "Content-Type: application/json" \
            --url 'https://mdb.api.cloud.yandex.net/managed-clickhouse/v1/clusters/<cluster_ID>' \
            --data '{
                      "updateMask": "<list_of_settings_to_update>",
                      "configSpec": {
                        "cloudStorage": {
                          "enabled": true,
                          "moveFactor": "<share_of_free_space>",
                          "dataCacheEnabled": <cache_data>,
                          "dataCacheMaxSize": "<maximum_memory_in_bytes>",
                          "preferNotToMerge": <disable_merging_data_parts>
                        }
                      }
                    }'
        ```

        Where:

        * `<cluster_ID>`: Cluster ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.
        * `updateMask`: Comma-separated string of settings to update.
        * `configSpec.cloudStorage`: Hybrid storage settings:

            * `enabled`: Enables hybrid storage.

            * `moveFactor`: Minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
            
                Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
            
            * `dataCacheEnabled`: Allows caching data accessed from the object storage in the cluster storage, `true` or `false`. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
            
            * `dataCacheMaxSize`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
            
            * `preferNotToMerge`: Disable [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in the cluster and object storage, `true` or `false`. To disable merging, set to `true`. To leave merging enabled, set to `false`.
            
                Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

    1. Check the [server response](../api-ref/Cluster/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
       
       ```bash
       cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
       ```
       
       Below, we assume that the repository contents reside in the `~/cloudapi/` directory.

    1. Call the [ClusterService.Update](../api-ref/grpc/Cluster/update.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

        {% note warning %}
        
        The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `update_mask` parameter as an array of `paths[]` strings.
        
        {% cut "Format for listing settings" %}
        
        ```yaml
        "update_mask": {
            "paths": [
                "<setting_1>",
                "<setting_2>",
                ...
                "<setting_N>"
            ]
        }
        ```
        
        {% endcut %}
        
        {% endnote %}

        ```bash
        grpcurl \
            -format json \
            -import-path ~/cloudapi/ \
            -import-path ~/cloudapi/third_party/googleapis/ \
            -proto ~/cloudapi/yandex/cloud/mdb/clickhouse/v1/cluster_service.proto \
            -rpc-header "Authorization: Bearer $IAM_TOKEN" \
            -d '{
                  "cluster_id": "<cluster_ID>",
                  "update_mask": {
                    "paths": [
                      <list_of_settings_to_update>
                    ]
                  },
                  "config_spec": {
                    "cloud_storage": {
                      "enabled": true,
                      "move_factor": "<share_of_free_space>",
                      "data_cache_enabled": <cache_data>,
                      "data_cache_max_size": "<maximum_memory_in_bytes>",
                      "prefer_not_to_merge": <disable_merging_data_parts>
                    }
                  }
                }' \
            mdb.api.cloud.yandex.net:443 \
            yandex.cloud.mdb.clickhouse.v1.ClusterService.Update
        ```

        Where:

        * `<cluster_ID>`: Cluster ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.
        * `update_mask`: List of settings to update as an array of strings (`paths[]`).
        * `config_spec.cloud_storage`: Hybrid storage settings:

            * `enabled`: Enables hybrid storage.

            * `move_factor`: Minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
            
                Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
            
            * `data_cache_enabled`: Allows caching data accessed from the object storage in the cluster storage, `true` or `false`. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
            
            * `data_cache_max_size`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
            
            * `prefer_not_to_merge`: Disable [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in the cluster and object storage, `true` or `false`. To disable merging, set to `true`. To leave merging enabled, set to `false`.
            
                Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

        You can get the cluster ID with the [list of clusters](cluster-list.md#list-clusters) in the folder.

    1. Check the [server response](../api-ref/grpc/Cluster/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}


## Changing the hybrid storage settings {#change-hybrid-storage}

If a Managed Service for ClickHouse® cluster has hybrid storage enabled, you can change its settings.

{% list tabs group=instructions %}

- Management console {#console}

    1. In the [management console](https://console.yandex.cloud), select the folder containing the cluster.
    1. [Navigate](https://console.yandex.cloud/link/managed-clickhouse) to **Managed Service for&nbsp;ClickHouse**.
    1. Select your cluster and click **Edit** in the top panel.
    1. Under **DBMS settings**, edit the hybrid storage settings:

        * **Data caching**: Enables caching data accessed from the object storage in the cluster storage. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing.
        
            By default, data caching is enabled.
        
        * **Cache size**: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
        
        * **Min. share of free space on cluster disks**: Sets the minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
        
            Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.

    1. Click **Save changes**.

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`.
    
    If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    To edit hybrid storage settings, run this command:

    ```bash
    yc managed-clickhouse cluster update <cluster_name_or_ID> \
        --cloud-storage-data-cache=<cache_data> \
        --cloud-storage-data-cache-max-size=<maximum_memory_in_bytes> \
        --cloud-storage-move-factor=<share_of_free_space> \
        --cloud-storage-prefer-not-to-merge=<disable_merging_data_parts>
    ```

    Where:

    * `<cluster_name_or_ID>`: Cluster name or ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.

    * `--cloud-storage-data-cache`: Enables caching data accessed from the object storage in the cluster storage. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
    
    * `--cloud-storage-data-cache-max-size`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
    
    * `--cloud-storage-move-factor`: Sets the minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to Yandex Object Storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
    
        Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
    
    * `--cloud-storage-prefer-not-to-merge`: Disables [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in cluster and object storages. To disable merges, set the parameter to `true` or provide it with no value. To keep merges enabled, set the parameter to `false` or do not provide it in the CLI command when creating a cluster.
    
        Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

- Terraform {#tf}

    1. Open the current Terraform configuration file with the infrastructure plan.

        To learn how to create this file, see [Creating a cluster](cluster-create.md).

        For the complete list of configurable Managed Service for ClickHouse® cluster fields, see [this Terraform provider guide](../../terraform/resources/mdb_clickhouse_cluster.md).

    1. In the Managed Service for ClickHouse® cluster description, edit the `cloud_storage` section attributes:

        ```hcl
        resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
          ...
          cloud_storage = {
            ...
            move_factor         = <share_of_free_space>
            data_cache_enabled  = <cache_data>
            data_cache_max_size = <maximum_memory_in_bytes>
            prefer_not_to_merge = <disable_merging_data_parts>
          }
        }
        ```

        Where:

        * `move_factor`: Minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
        
            Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
        
        * `data_cache_enabled`: Allows caching data accessed from the object storage in the cluster storage, `true` or `false`. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
        
        * `data_cache_max_size`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
        
        * `prefer_not_to_merge`: Disable [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in the cluster and object storage, `true` or `false`. To disable merging, set to `true`. To leave merging enabled, set to `false`.
        
            Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

    1. Make sure the Terraform configuration file is correct:

        1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
        1. Run this command:
        
           ```bash
           terraform validate
           ```
        
           Terraform will show any errors found in your configuration files.

    1. Confirm resource changes:

        1. Run this command to view the intended changes:
        
           ```bash
           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.
        
        1. If everything looks correct, apply the changes:
        
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

    {% note warning "Timeouts" %}
    
    The Terraform provider sets the following timeouts for Managed Service for ClickHouse® cluster operations:
    
    * Creating a cluster, including by restoring from a backup: 60 minutes.
    * Updating a cluster: 90 minutes.
    * Deleting a cluster: 30 minutes.
    
    Operations exceeding the timeout are aborted.
    
    {% cut "How to change these limits" %}
    
    Add the `timeouts` section to your cluster description, such as the following:
    
    ```hcl
    resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
      ...
      timeouts = {
        create = "1h30m" # 1 hour 30 minutes
        update = "2h"    # 2 hours
        delete = "30m"   # 30 minutes
      }
    }
    ```
    
    {% endcut %}
    
    {% endnote %}

- REST API {#api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Call the [Cluster.Update](../api-ref/Cluster/update.md) method, e.g., via the following [cURL](https://curl.se/) request:

        {% note warning %}
        
        The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `updateMask` parameter as a single comma-separated string.
        
        {% endnote %}

        ```bash
        curl \
            --request PATCH \
            --header "Authorization: Bearer $IAM_TOKEN" \
            --header "Content-Type: application/json" \
            --url 'https://mdb.api.cloud.yandex.net/managed-clickhouse/v1/clusters/<cluster_ID>' \
            --data '{
                      "updateMask": "<list_of_settings_to_update>",
                      "configSpec": {
                        "cloudStorage": {
                          "moveFactor": "<share_of_free_space>",
                          "dataCacheEnabled": <cache_data>,
                          "dataCacheMaxSize": "<maximum_memory_in_bytes>",
                          "preferNotToMerge": <disable_merging_data_parts>
                        }
                      }
                    }'
        ```

        Where:

        * `<cluster_ID>`: Cluster ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.
        * `updateMask`: Comma-separated string of settings to update.
        * `configSpec.cloudStorage`: Hybrid storage settings:

            * `moveFactor`: Minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
            
                Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
            
            * `dataCacheEnabled`: Allows caching data accessed from the object storage in the cluster storage, `true` or `false`. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
            
            * `dataCacheMaxSize`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
            
            * `preferNotToMerge`: Disable [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in the cluster and object storage, `true` or `false`. To disable merging, set to `true`. To leave merging enabled, set to `false`.
            
                Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

    1. Check the [server response](../api-ref/Cluster/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
       
       ```bash
       cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
       ```
       
       Below, we assume that the repository contents reside in the `~/cloudapi/` directory.

    1. Call the [ClusterService.Update](../api-ref/grpc/Cluster/update.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

        {% note warning %}
        
        The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `update_mask` parameter as an array of `paths[]` strings.
        
        {% cut "Format for listing settings" %}
        
        ```yaml
        "update_mask": {
            "paths": [
                "<setting_1>",
                "<setting_2>",
                ...
                "<setting_N>"
            ]
        }
        ```
        
        {% endcut %}
        
        {% endnote %}

        ```bash
        grpcurl \
            -format json \
            -import-path ~/cloudapi/ \
            -import-path ~/cloudapi/third_party/googleapis/ \
            -proto ~/cloudapi/yandex/cloud/mdb/clickhouse/v1/cluster_service.proto \
            -rpc-header "Authorization: Bearer $IAM_TOKEN" \
            -d '{
                  "cluster_id": "<cluster_ID>",
                  "update_mask": {
                    "paths": [
                      <list_of_settings_to_update>
                    ]
                  },
                  "config_spec": {
                    "cloud_storage": {
                      "move_factor": "<share_of_free_space>",
                      "data_cache_enabled": <cache_data>,
                      "data_cache_max_size": "<maximum_memory_in_bytes>",
                      "prefer_not_to_merge": <disable_merging_data_parts>
                    }
                  }
                }' \
            mdb.api.cloud.yandex.net:443 \
            yandex.cloud.mdb.clickhouse.v1.ClusterService.Update
        ```

        Where:

        * `<cluster_ID>`: Cluster ID which you can get with the [list of clusters](cluster-list.md#list-clusters) in the folder.
        * `update_mask`: List of settings to update as an array of strings (`paths[]`).
        * `config_spec.cloud_storage`: Hybrid storage settings:

            * `move_factor`: Minimum percentage of free space in the cluster storage. If your free space percentage is below this value, the data will be moved to object storage. The minimum value is `0`, the maximum value is `1`, and the default value is `0.01`.
            
                Data parts are queued up in descending order by size. The number of data parts that will be moved is determined by the condition regarding the specified minimum share of free space in cluster storage.
            
            * `data_cache_enabled`: Allows caching data accessed from the object storage in the cluster storage, `true` or `false`. As a result, the cold data accessed from the object storage is moved to high-speed drives for faster processing. The default value is `true` (allowed).
            
            * `data_cache_max_size`: Sets the maximum cache size, in bytes, allocated in the cluster storage for data accessed from the object storage. If no value is set, the maximum cache size defaults to half the size of the cluster storage.
            
            * `prefer_not_to_merge`: Disable [merging of data parts](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key) in the cluster and object storage, `true` or `false`. To disable merging, set to `true`. To leave merging enabled, set to `false`.
            
                Once inserted into the table, the data is saved as a data part and sorted by its primary key. Then, ClickHouse® runs a background merge of data parts belonging to the same partition into a larger data part within 10 to 15 minutes after the insertion. You can use the [system.parts](https://clickhouse.com/docs/en/operations/system-tables/parts#system_tables-parts) system table to view the merged data parts and partitions.

    1. Check the [server response](../api-ref/grpc/Cluster/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}

## Setting data storage policies {#policies-create}

In a Managed Service for ClickHouse® cluster with hybrid storage enabled, three [storage policies](../concepts/storage.md#storage-policies) are available: `default`, `local`, and `object_storage`. For DB tables, you can specify one of them. You cannot create new storage policies or update the existing ones.

### Getting information about policies {#get-info}

To view policy settings, run this query:

{% list tabs group=instructions %}

- SQL {#sql}

    ```sql
    SELECT *
    FROM system.storage_policies;
    ```

{% endlist %}


### Setting a policy when creating a table {#table-create}

To set a policy when creating a table, run this query:

{% list tabs group=instructions %}

- SQL {#sql}

    ```sql
    CREATE TABLE <table_name> (
        <table_schema>
    ) ENGINE = MergeTree
    ...
    SETTINGS storage_policy = '<storage_policy>';
    ```

{% endlist %}

### Setting a policy for an existing a table {#table-update}

To set or edit a policy for an existing a table, run this query:

{% list tabs group=instructions %}

- SQL {#sql}

    ```sql
    ALTER TABLE <table_name>
    MODIFY SETTING storage_policy = '<storage_policy>';
    ```

{% endlist %}


## Monitoring space taken up by data in Object Storage {#metrics}

To monitor the amount of space [MergeTree](https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree) table parts occupy in Object Storage, use the `ch_s3_disk_parts_size` metric in Yandex Monitoring.

{% list tabs group=instructions %}

- Management console {#console}

    1. Open the [management console](https://console.yandex.cloud).
    1. [Navigate](https://console.yandex.cloud/link/monitoring) to **Monitoring**.
    1. Open the **Metric Explorer** section.
    1. Run this request:

        ```text
        "ch_s3_disk_parts_size"{service="managed-clickhouse", resource_type="cluster", node="by_host", resource_id="<cluster_ID>", subcluster_name="clickhouse_subcluster"}
        ```

{% endlist %}


## Use cases {#examples}

* [Using hybrid storage in Managed Service for ClickHouse®](../tutorials/hybrid-storage.md)