The postgresql database timescaledb time series library converts large data tables into super tables

The postgresql database timescaledb time series library converts large data tables into super tables

Foreword When
I worked in the past few days, I found that some large data tables in the timescaledb time series database were not super tables. It is estimated that there were no changes when the database was built at that time, which affected the insertion and query efficiency. Therefore, it needs to be changed to super tables
due to work reasons. Confidential

Create a new table

First of all, because the creation of a super table in the timescaledb time series library must be a table without data, the
first step is to create a table that is exactly the same as the original table (add _cs after the table name). You can directly copy the table creation statement, or you can Copy table structure directly with tools

Change the new watch to super watch

Change the newly-created table to a super table, partition every 7 days

--我是7天一分区
SELECT create_hypertable('表名_cs', 'alarm_time', chunk_time_interval => INTERVAL '7 day');
-- 加个索引
CREATE INDEX "表名_cs_create_time_idx" ON "hrmw"."表名_cs" USING btree (
  "create_time" "code"."列名" DESC NULLS FIRST
);

Three insert data

1. You can insert directly when the amount of data is not large

INSERT into 表名_cs SELECT * from 表名;

2. If the amount of data is relatively large, you can insert it day by day or insert it together in a month or months.

INSERT into 表名_cs SELECT * from 表名 where alarm_time >= '2020-9-1';
INSERT into 表名_cs SELECT * from 表名 where alarm_time >= '2020-10-1' and  alarm_time < '2020-11-1'   ;

3. Use functions (stored procedures)

For the large amount of table data, you can use stored procedures (postgresql database called functions) for inserting day by day

Below is the function I wrote. Everyone can learn from

CREATE 
	OR REPLACE PROCEDURE "hrmw"."sp_into_表名_pt" ( ) AS $BODY$ BEGIN
	-- 一般按照表名来建函数
	DECLARE --我声明的变量有点多 按实际的来
		target_text TEXT;
	sqltext TEXT;
	sqltext1 TEXT;
	sqltext2 TEXT;
	rd record;
	nloop INT;
	isexist TEXT;
	datestr TEXT;
	begindate TEXT;
	n INT;
	BEGIN
			--查询最早一天的时间
			sqltext := 'select to_char(min(alarm_time),''yyyy-mm-dd'') datestr from tb_hrmw_moni_target';       
		EXECUTE sqltext INTO begindate;
		n := date_part( 'day', now( ) - begindate :: DATE );
--enddate:=(to_char(now(),'yyyy-mm-dd'))::text;
		FOR nloop IN 0..n
		LOOP
		datestr := ( begindate :: DATE + nloop ) :: TEXT;
		sqltext2 := '
		insert into 表名_cs 
		select
		列名1,
		code,
		列名2,
		alarm_time,
		列名3,
		列名4
		from 表名
		where alarm_time >= ''' || datestr || ' 00:00:00''
		and alarm_time <= ''' || datestr || ' 23:59:59''
		'; 
		EXECUTE sqltext2;
		COMMIT;
		
	END loop;
RETURN;

END;

END $BODY$ LANGUAGE plpgsql

Four view effect

Under the mode _timescaledb_internal is the partition
Insert picture description here

Guess you like

Origin blog.csdn.net/yang_z_1/article/details/113582352