简体   繁体   English

如何在Clickhouse数据库的同一选择查询中查询多个数据库?

[英]How to query multiple databases in the same select query in clickhouse database?

I have a clickhouse database. 我有一个Clickhouse数据库。 It contains multiple databases. 它包含多个数据库。 The tables inside the databases are identical. 数据库内部的表是相同的。

For example DB1 has the table "Table1", DB2 also has the table "Table1" (Here the databases are different, Tables are also different but they have identical schema and contain similar type of information). 例如,DB1具有表“ Table1”,DB2也具有表“ Table1”(这里的数据库不同,表也不同,但是它们具有相同的架构并包含相似的信息类型)。

Is there a way that i can write a query to get the information from all the different tables from all the different databases optimally? 有没有一种方法可以编写查询以最佳地从所有不同数据库的所有不同表中获取信息? Currently i am querying each table individually and doing an Union among them. 目前,我正在分别查询每个表,并在它们之间进行合并。

You could set up a special table with Merge engine (not to be confused with MergeTree). 您可以使用Merge引擎设置一个特殊表(不要与MergeTree混淆)。 Querying it will do the the same UNION ALL transparently. 查询它会透明地执行相同的UNION ALL It does not support spanning through multiple databases though, so you still might end up having multiple of those or moving the tables into one db. 但是,它不支持跨越多个数据库,因此您仍然可能最终拥有多个数据库或将表移动到一个数据库中。

I would propose to create one more DB: 我建议再创建一个数据库:

create database <your_db_name> on cluster <your_cluster_name>;

Then create a Merge table for each of you existing DB: 然后为每个现有数据库创建一个合并表:

CREATE TABLE <your_db_name>.<merge_table_name> On cluster <your_cluster_name> (<fields list you need to deal with>)  ENGINE=Merge(<existing_db_name>, '<regex>');

Create a View for each of the Merge tables (using a view you can execute any aggregate functions, group by and etc.): 为每个合并表创建一个视图 (使用视图,您可以执行任何聚合函数,分组依据等):

CREATE VIEW <your_db_name>.view_<merge_table_name> on cluster <your_cluster_name> (<list of fields>) AS SELECT <list of fields> FROM <your_db_name>.<merge_table_name>;

Finally create a "result" Merge table that combines all the views newly created: 最后,创建一个“结果”合并表,该表合并了所有新创建的视图:

CREATE TABLE <your_db_name>.<result_table_name> on cluster <your_cluster_name> (<list of fields>)  ENGINE=Merge(<your_db_name>, '<regex>');

Name the tables and views in such a way that your regular expression matches all you need and make sure you avoid loops. 命名表和视图的方式应使您的正则表达式匹配您所需要的所有内容,并确保避免循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM