엄청난 양의 데이터를 처리하고 있습니다. 다른 테이블에 새 항목을 삽입 한 후 테이블 레코드를 삽입/업데이트하는 것이 가장 좋습니다.엄청난 양의 데이터 삽입을 효율적으로 처리하는 가장 좋은 방법은 무엇입니까
거대한 것은 하루에 2 백만 행 이상을 얻음을 의미합니다. 이는 계속 진행되는 과정입니다.
복잡한 논리에 따라 데이터를 삽입하거나 업데이트하는 트리거를 만들려고했습니다. 하지만이 작업을 수행하는 데 좋은 방법이 아닌지 의심 스럽습니다. 데이터베이스에서 무거워 야합니다. 여기 트리거 헤더와 기능의 일부입니다
DECLARE
AVar integer;
AdateVar timestamp without time zone;
AnameVar character varying(40);
BEGIN
SELECT id INTO AVar FROM table1 WHERE ST_DWithin(NEW.position,ST_SetSRID(ST_MakePoint(longitudedecimal,latitudedecimal),4326) ,0.01447534783);
select Adate INTO AdateVar from table2 where id = NEW. id ORDER BY Adate DESC limit 1;
IF (aVar > 0) THEN
select name into AnameVar FROM table1 WHERE id = AVar;
INSERT into table2 (id,name,date) SELECT NEW.id,AnameVar,NEW.timestamp;
ELSE
……….
END IF;
RETURN NULL;
End;
편집
, 기능이 너무 깁니다 :
내가 좋아하는 현재의 트리거가 보이는 PostgreSQL의 9.1를 사용합니다. 내가 방아쇠는 하나 이상의 기능을 호출 할 수 없다는 것을 알고 coz! 그래서 나는 12에서 하나의 사건을 기록, 매우 길고 복잡하게되었다 : 대량 업데이트/삽입, 사용 배치에 대한
CREATE TRIGGER ts_trigger
AFTER INSERT
ON table1
FOR EACH ROW
EXECUTE PROCEDURE test_trigger();
declare
Aposition geometry;
Cposition geometry;
Plong double precision;
Plat double precision;
Adate timestamp without time zone;
Cdate timestamp without time zone;
startDate timestamp without time zone;
CnotifDate timestamp without time zone;
AnotifDate timestamp without time zone;
lastmsg timestamp without time zone;
InsideCircle integer;
InsideSquare integer;
insidePoint integer;
distance character varying(40);
-- this variables used to calculate the time in the table3
inAction character varying(40);
diff character varying(40);
days character varying(40);
hours character varying(40);
str character varying(40);
CinAction character varying(40);
AinAction character varying(40);
BEGIN
select time_stamp INTO Adate from table1 where userid=NEW.userid and time_stamp < NEW.time_stamp order by time_stamp desc limit 1 ;
select position INTO Aposition from table1 where userid=NEW.userid and time_stamp < NEW.time_stamp order by time_stamp desc limit 1;
select time_stamp INTO Cdate from table1 where userid=NEW.userid and time_stamp > NEW.time_stamp order by time_stamp limit 1;
select position INTO Cposition from table1 where userid=NEW.userid and time_stamp > NEW.time_stamp order by time_stamp limit 1;
SELECT p.num INTO InsideCircle FROM table3 p WHERE ST_DWithin(Aposition,ST_SetSRID(ST_MakePoint(p.longitudedecimal,p.latitudedecimal), 4326) ,0.02171302174) ORDER BY ST_Distance(ST_SetSRID(ST_MakePoint(p.longitudedecimal,p.latitudedecimal), 4326),Aposition) limit 1;
SELECT p.num INTO InsideSquare FROM table3 p WHERE ST_DWithin(NEW.position ,ST_SetSRID(ST_MakePoint(p.longitudedecimal,p.latitudedecimal), 4326) ,0.02171302174) ORDER BY ST_Distance(ST_SetSRID(ST_MakePoint(p.longitudedecimal,p.latitudedecimal), 4326),NEW.position) limit 1;
SELECT p.num INTO insidePoint FROM table3 p WHERE ST_DWithin(Cposition,ST_SetSRID(ST_MakePoint(p.longitudedecimal,p.latitudedecimal), 4326) ,0.02171302174) ORDER BY ST_Distance(ST_SetSRID(ST_MakePoint(p.longitudedecimal,p.latitudedecimal), 4326),Cposition) limit 1;
-
IF (InsideCircle >0 and (InsideCircle =InsideSquare or InsideSquare is null))THEN
select startDate INTO startDate from myTable where id=NEW.userid and num = InsideCircle and startDate =Adate;
IF (InsideSquare >0)then
if (Cdate is not null)then
if (insidePoint is null)THEN
diff = NEW.time_stamp -startDate;
str= split_part(diff,' ',2);
IF(str = '')then
hours= split_part(diff,':',1);
days = '0';
ELSE
str= split_part(diff,' ',3);
IF(str = '') then
hours ='00';
days = split_part(diff,' ',1);
ELSE
hours= split_part(split_part(diff,' ',3),':',1);
days = split_part(diff,' ',1);
END IF;
END IF;
inAction = days || ',' ||hours;
Update myTable SET notifDate = NEW.time_stamp , time_inAction=inAction WHERE id=NEW.userid and num =InsideSquare ;
END IF;
END IF;
Return Null;
END;
어떤 데이터베이스를 사용합니까? –
postgreSQL, 죄송합니다. – Shadin
하나의 관찰 : 실제로 Adatevar를 삽입물에 사용하지 마십시오 –