Skip to content

Instantly share code, notes, and snippets.

@den-crane
Last active March 13, 2020 15:45
Show Gist options
  • Save den-crane/80cb95e74f046be2d8ffae58d9b04e8f to your computer and use it in GitHub Desktop.
Save den-crane/80cb95e74f046be2d8ffae58d9b04e8f to your computer and use it in GitHub Desktop.
convert not replicated MV to replicated
CREATE TABLE test12345 (A Int64) ENGINE = MergeTree ORDER BY A;
CREATE MATERIALIZED VIEW test12345mv Engine=MergeTree order by A
AS SELECT A FROM test12345;
insert into test12345 select number from numbers(10);
#### maintanance / load stop
rename table test12345 to test12345_old;
CREATE TABLE test12345 (A Int64)
ENGINE = ReplicatedMergeTree ('/clickhouse/testCluster/tables/test12345','1')
ORDER BY A;
alter table test12345 ATTACH PARTITION tuple() FROM test12345_old;
detach table test12345mv;
rename table `.inner.test12345mv` to `inner_test12345_old`;
create table `.inner.test12345mv` as `inner_test12345_old` Engine=Log;
attach table test12345mv;
drop table test12345mv;
CREATE MATERIALIZED VIEW test12345mv Engine=ReplicatedMergeTree ('/clickhouse/testCluster/tables/test12345mv','1')
order by A
AS SELECT A FROM test12345;
alter table `.inner.test12345mv` ATTACH PARTITION tuple() FROM `inner_test12345_old`;
insert into test12345 select number from numbers(10,10);
select count() from test12345;
┌─count()─┐
│ 20 │
└─────────┘
select count() from test12345mv;
┌─count()─┐
│ 20 │
└─────────┘
select count() from test12345_old;
┌─count()─┐
│ 10 │
└─────────┘
select count() from inner_test12345_old;
┌─count()─┐
│ 10 │
└─────────┘
drop table `inner_test12345_old`;
drop table test12345;
drop table test12345_old;
drop table test12345mv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment