Projections in ClickHouse

Projections in ClickHouse are commonly used to speed up the queries on the MergeTree table at the cost of storage. The speedup achieved is due to how the data is stored (different from the original table). Projections will create a new hidden table with the data based on the CREATE statement. This will cause increased storage requirements compared to the original table. Let us look at managing projections in ClickHouse.

Let us begin by creating a MergeTree table for which we will add a projection.

CREATE TABLE projections_mergetree
(
   `ID` UInt64,
   `Name` String,
   `Value` Float64,
   `phone` String
)
ENGINE = MergeTree()
ORDER BY ID;

Let us create a projection with different sorting keys.

ALTER TABLE projections_mergetree ADD PROJECTION projections_mergetree_projection1 (
SELECT * ORDER BY phone);

This will add a new projection called projections_mergetree_projection1 to the table. Next, we will materialize the projection.

ALTER TABLE projections_mergetree MATERIALIZE PROJECTION projections_mergetree_projection1;

We can use the CLEAR PROJECTION statement to clear the files without deleting the projection files.

ALTER TABLE projections_mergetree CLEAR PROJECTION projections_mergetree_projection1;

Finally, we can use the DROP PROJECTION statement to delete this projection altogether.

ALTER TABLE projections_mergetree DROP PROJECTION projections_mergetree_projection1;

 

References

https://percona.community/events/percona-live/altinity-room/1/

https://clickhouse.com/docs/en/sql-reference/statements/alter/projection

Was this article helpful?

Related Articles

CHISTADATA IS COMMITTED TO OPEN SOURCE SOFTWARE AND BUILDING HIGH PERFORMANCE COLUMNSTORES

In the spirit of freedom, independence and innovation. ChistaDATA Corporation is not affiliated with ClickHouse Corporation 

Contents

Need Support?

Can't find the answer you're looking for?
Contact Support

ChistaDATA Inc. Knowledge base is licensed under the Apache License, Version 2.0 (the “License”)

Copyright 2022 ChistaDATA Inc

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.