在具有许多公司的数据库中,使用schema和search_path
几乎所有访问都来自公司级别,即仅限于单个模式。一些数据仓库和会计功能需要扫描所有模式。
对于这个问题,根据@horse_with_no_name 的最佳建议,使用模式本身作为分区。例如
create table global.usr(
usr_id int primary key, // these will be unique across all partitions
name varchar );
create table cmp1.usr(
usr_id int primary key,
) inherits( global.usr );
create table cmp2.usr(
....
Run Code Online (Sandbox Code Playgroud)
如果我们填充
insert into cmp1 usr values( 11, 'eleven' );
insert into cmp1 usr values( 12, 'twelve' );
insert into cmp2 usr values( 21, 'twenty-one' );
insert into cmp2 usr values( 22, 'twenty-two' );
insert into cmp3 usr values( 31, 'thirty-one' );
insert into cmp3 usr values( 32, 'thirty-two' );
Run Code Online (Sandbox Code Playgroud)
这在模式级别上工作得很好:
set search_path = 'global';
select count(1) from t; => 6
set search_path = 'cmp1, global';
select count(1) from t; => 2
set search_path = 'cmp1, cmp2, global';
select count(1) from t; => 4
set search_path = 'global, cmp1';
select count(1) from t; => 6
Run Code Online (Sandbox Code Playgroud)
问题是是否有任何方法可以在全球一级进行优化。例如,是否可以在 global.usr_id 上创建一个跨越所有分区的索引?
| 归档时间: |
|
| 查看次数: |
503 次 |
| 最近记录: |