在前一篇《postgresql中間件pgoneproxy支持冷熱數據分離查詢》中講解了按照id來進行數據的分離,針對時間至少稍微的提了一下。本篇這專門針對時間來進行講解和測試下。sql
在個人數據庫中新建了一張表bigtest,其中字段狀況以下所示:數據庫
Table "public.bigtest_0" Column | Type | Modifiers --------+-----------------------------+----------- id | integer | name | character varying(1024) | age | integer | tt | timestamp without time zone |
如今按照tt字段來進行數據的分離插入和查詢。下面是bigtest表的分表的配置狀況:post
{ "table" : "bigtest", "pkey" : "tt", "type" : "timestamp", "method" : "buffer", "partitions": [ {"suffix":"_0", "group":"data1", "minval":"2004-01-01 00:00:00", "maxval":"2015-01-01 00:00:00"}, {"suffix":"_1", "group":"data1", "minval":"2015-01-01 00:00:01","maxval":"2037-01-01 00:00:00"} ] }
從上面配置能夠看出,時間在2004-01-01 00:00:00~2015-01-01 00:00:00的數據存放到bigtest_0的表中,時間在2015-01-01 00:00:01 ~2037-01-01 00:00:00的數據存放到bigtest_1的表中。測試
在配置好pgoneproxy的proxy-part-tables選項後,啓動中間件pgoneproxy。進行表的建立,插入數據,查詢數據的操做,狀況以下所示:spa
直接執行創表語句,pgoneproxy就會根據配置狀況自動建立兩張分表,狀況以下所示:.net
pgbench=> \dt; List of relations Schema | Name | Type | Owner --------+------------------+-------+---------- public | pgbench_accounts | table | postgres public | pgbench_branches | table | postgres public | pgbench_history | table | postgres public | pgbench_tellers | table | postgres (4 rows) pgbench=> create table bigtest(id int, name varchar(1024), age int, tt timestamp); CREATE 0 pgbench=> \dt; List of relations Schema | Name | Type | Owner --------+------------------+-------+---------- public | bigtest_0 | table | db_user public | bigtest_1 | table | db_user public | pgbench_accounts | table | postgres public | pgbench_branches | table | postgres public | pgbench_history | table | postgres public | pgbench_tellers | table | postgres (6 rows) pgbench=>
2. 插入數據postgresql
下面插入兩條語句,看是否可以根據要求插入到不一樣的數據表中code
pgbench=> select * from bigtest_0; id | name | age | tt ----+------+-----+---- (0 rows) pgbench=> select * from bigtest_1; id | name | age | tt ----+------+-----+---- (0 rows) pgbench=> insert into bigtest(id, name, age, tt) values (10, 'name10', 10, '2024-01-01 00:00:00'); INSERT 0 1 pgbench=> insert into bigtest(id, name, age, tt) values (10, 'name10', 10, '2014-01-01 00:00:00'); INSERT 0 1 pgbench=> select * from bigtest_0; id | name | age | tt ----+--------+-----+--------------------- 10 | name10 | 10 | 2014-01-01 00:00:00 (1 row) pgbench=> select * from bigtest_1; id | name | age | tt ----+--------+-----+--------------------- 10 | name10 | 10 | 2024-01-01 00:00:00 (1 row) pgbench=>
3. 查詢數據中間件
根據各類條件進行數據查詢,狀況以下所示:blog
pgbench=> select * from bigtest where tt < '2015-01-01 00:00:00'; id | name | age | tt ----+--------+-----+--------------------- 10 | name10 | 10 | 2014-01-01 00:00:00 (1 row) pgbench=> select * from bigtest where tt > '2015-01-01 00:00:00'; id | name | age | tt ----+--------+-----+--------------------- 10 | name10 | 10 | 2024-01-01 00:00:00 (1 row) pgbench=> select * from bigtest where tt < '2035-01-01 00:00:00'; id | name | age | tt ----+--------+-----+--------------------- 10 | name10 | 10 | 2014-01-01 00:00:00 10 | name10 | 10 | 2024-01-01 00:00:00 (2 rows) pgbench=> select * from bigtest where tt < '2035-01-01 00:00:00' and tt > '2016-01-01 00:00:00'; id | name | age | tt ----+--------+-----+--------------------- 10 | name10 | 10 | 2024-01-01 00:00:00 (1 row)
則從上面的查詢狀況看,可以根據時間進行準確的查詢。故pgoneproxy也可以根據時間進行冷熱數據的分離存儲和查詢。