Sql 高效处理大量数据插入的最佳方法是什么

Sql 高效处理大量数据插入的最佳方法是什么,sql,postgresql,Sql,Postgresql,我正在处理大量的数据输入。我需要知道在另一个表中插入新条目后插入/更新表记录的最佳实践 我的意思是说,我们每天要排200多万行,这是一个持续的过程 我尝试创建一个触发器,根据复杂的逻辑插入或更新数据。 但我怀疑这是一个很好的方法,我担心这将是沉重的数据库 我使用PostgreSQL 9.1 当前触发器如下所示: DECLARE AVar integer; AdateVar timestamp without time zone; AnameVar character varying(40);

我正在处理大量的数据输入。我需要知道在另一个表中插入新条目后插入/更新表记录的最佳实践

我的意思是说,我们每天要排200多万行,这是一个持续的过程

我尝试创建一个触发器,根据复杂的逻辑插入或更新数据。 但我怀疑这是一个很好的方法,我担心这将是沉重的数据库

我使用PostgreSQL 9.1

当前触发器如下所示:

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;
编辑

这是一个触发器头和函数,函数太长了。因为我知道触发器不能调用多个函数!所以它变得非常漫长和复杂,我只发布了12个案例中的一个:

CREATE TRIGGER ts_trigger
  AFTER INSERT
  ON table1
  FOR EACH ROW
  EXECUTE PROCEDURE test_trigger();

对于批量更新/插入,请使用批处理。这将减少数据库的负载,并在启用时防止WAL过载。

您使用的是什么数据库?postgreSQL,很抱歉忘了提及它。一个观察结果是:您从未在插入中实际使用Adatevar,这是一次性数据更新或正在进行的过程。如果这是一个批量加载:为什么不使用普通SQL,而不是使用这种每次一行的处理方法,触发器基本上是在这里添加了详细信息dba.stackexchange.com/questions/75479/…如果您能看一下,我将不胜感激。
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;