尝试在 Vitess
里面创建 Materialize View
,命令如下:
vtctlclient Materialize '{"workflow": "aa1", "source_keyspace": "commerce", "target_keyspace": "commerce", "table_settings": [{"target_table": "product_count", "source_expression": "select count(*) AS count, price from product2 group by price", "create_ddl": "copy"}]}
结果 product_count
表中并没有数据,打开 master tablet
的状态页面,对应 workflow
提示错误:
primary key column count is not allowed to reference an aggregate expression
查看官方文档:
The select expressions can be any non-aggregate MySQL expression, or they can also be count or sum as aggregate expressions. Aggregate expressions combined with the corresponding group by clauses will allow you to materialize real-time rollups of the source table, which can be used for analytics.
应该是可以支持 count
和 sum
的,于是去 slack
频道寻求帮助,得到回复:
Aggregate expressions are allowed and yours looks good. One issue I see is that create_ddl is set as copy. Is there a product2 in the source? The error you see is due to the fact that vreplication finds the count column as the primary key of the target table.
Can you try specifying "create_ddl", say as, "create table product_count(count int, price int, primary key (price))"
发现本地创建的 product_count
表缺少 primary key
,加上后便一切正常,在随后的交流中,PlanetScale
的 CTO sougou
同学回复:
if there's no primary key, we treat all columns as pk. But it's a hack. It handles some common use cases, but will break, like if there are null values, etc.
We could add an additional check to print a better error message though
PlanetScale
团队在特别积极地维护 Vitess
社区,有问必答 👍🙏