我有一个大型分区表,用于存储帐户之间的货币交易。
CREATE TABLE "transactions" (
"from" BYTEA NOT NULL -- sender account
,"to" BYTEA NOT NULL -- receiver account
,"type" INTEGER NOT NULL -- type of transfer
,"ts" TIMESTAMP NOT NULL -- timestamp of transfer
) PARTITION BY RANGE ("ts");
Run Code Online (Sandbox Code Playgroud)
“transactions”在“ts”、“from”和“to”上有索引(“ts”用于排序)。
CREATE INDEX "transactions_ts_idx" ON "transactions" USING BTREE ("ts");
CREATE INDEX "transactions_from_idx" ON "transactions" USING BTREE ("from", "ts");
CREATE INDEX "transactions_to_idx" ON "transactions" USING BTREE ("to", "ts");
Run Code Online (Sandbox Code Playgroud)
我想查询涉及给定帐户或没有帐户的所有交易,例如:
-- given an account <account>
SELECT * FROM "transactions"
WHERE "from" = …
Run Code Online (Sandbox Code Playgroud) postgresql index execution-plan index-tuning query-performance
我有一个范围分区表,我希望能够快速交换同一范围内的分区(即重新创建表并为其交换现有分区表)。但是,即使分区具有所有必需的索引,附加分区也需要很长时间。
-- create a partitioned table
create table "partitioned" (
"k1" INTEGER NOT NULL,
"k2" INTEGER NOT NULL,
PRIMARY KEY("k1", "k2")
) PARTITION BY RANGE "k1";
-- create the table to be added as a partition
create table "segment_1_000_000_to_2_000_000" (
"k1" INTEGER NOT NULL,
"k2" INTEGER NOT NULL,
PRIMARY KEY("k1", "k2")
);
-- insert 300,000,000 rows into table "segment_1_000_000_to_2_000_000" with "k1" values between values 1,000,000 and 2,000,000
-- ...
-- takes 5 minutes:
alter table "partitioned" attach partition "segment_1_000_000_to_2_000_000 for …
Run Code Online (Sandbox Code Playgroud)