1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| CREATE TABLE IF NOT EXISTS salary ( `id` Int8, `name` String, `department_id` Int8, `salary` Float32 ) ENGINE = TinyLog;
insert into salary values (1,'xiaoming',1,200),(2,'xiaohong',2,100),(3,'xiaogang',1,300),(4,'xiaoli',2,150);
CREATE MATERIALIZED VIEW db1.view_salary_count_populate ENGINE = TinyLog POPULATE AS SELECT department_id, count(department_id) as employee_count, sum(salary) as salary_count FROM salary group by department_id;
select * from view_salary_count_populate;
┌─department_id─┬─employee_count─┬─salary_count─┐ │ 1 │ 2 │ 500 │ │ 2 │ 2 │ 250 │ └───────────────┴────────────────┴──────────────┘
CREATE MATERIALIZED VIEW db1.view_salary_count ENGINE = TinyLog AS SELECT department_id, count(department_id) as employee_count, sum(salary) as salary_count FROM salary group by department_id;
show tables;
┌─name───────────────────────────────────────────┐ │ .inner_id.e414e835-144b-4107-91f3-68a800a0afd0 │ │ salary │ │ view_salary_count │ └────────────────────────────────────────────────┘
select * from view_salary_count;
0 rows in set. Elapsed: 0.002 sec.
insert into view_salary_count SELECT department_id, count(department_id) as employee_count, sum(salary) as salary_count FROM salary group by department_id;
select * from view_salary_count;
┌─department_id─┬─employee_count─┬─salary_count─┐ │ 1 │ 2 │ 500 │ │ 2 │ 2 │ 250 │ └───────────────┴────────────────┴──────────────┘
insert into salary values (5,'xiaod',1,100), (6,'xiaoq',2,200), (7,'xiaoh',1,300), (8,'xiaot',2,450);
select * from view_salary_count;
┌─department_id─┬─employee_count─┬─salary_count─┐ │ 1 │ 2 │ 500 │ │ 2 │ 2 │ 250 │ │ 1 │ 2 │ 400 │ │ 2 │ 2 │ 650 │ └───────────────┴────────────────┴──────────────┘
|