使用PostgreSQL外部数据包装器时,批量插入行更有效吗?

时间:2015-10-04 02:05:48

标签: sql postgresql bulkinsert postgresql-9.3 foreign-data-wrapper

所以,如果我想要插入到外表中的一些记录,我可以这样做(伪代码):

DO
    INSERT INTO my_fdw_table VALUES next_row;
LOOP

或者这个:

INSERT INTO my_fdw_table SELECT all_the_rows;

根据我的直觉,第二个似乎更有效率,因为所有数据都是一次性发送的,SQL关键字重复次数较少。

然而,我不知道第二次是否会真正导致往返次数。如果有很多行,我也不知道或者第二个是否会冒某种语句长度限制的风险?

我正在使用postgres 9.3

1 个答案:

答案 0 :(得分:0)

差异不会很大,因为即使是批量 SELECT distinct(reservation.reservationid),reservation.name,reservation.age as p_age,reservation.gender as p_gender,reservation.lessonregistrationno,reservation.mobile,reservation.email,reservation.arrivaldate,reservation.arrivaltime,reservation.departuredate,reservation.departuretime,centers.name as centername,reservation.purposeofvisitid,reservation.otherpurpose,reservation.additionalinformation,reservation.status,reservation.membertype as type,reservation.iskriyaban,reservation.sharedacco,reservation.citizenship,reservation.total_guest,reservation.created,reservationmember.name as member_name,reservationmember.age,reservationmember.lessonregistrationnumber,reservationmember.gender,reservationmember.membertype,relationships.relationship,countries.name as country,purposeofvisits.purpose FROM reservation LEFT JOIN centers ON reservation.centerid = centers.centerid LEFT JOIN purposeofvisits on purposeofvisits.purposeofvisitid = reservation.purposeofvisitid LEFT JOIN countries ON countries.id = reservation.countryid LEFT JOIN reservationmember on reservationmember.reservationid = reservation.reservationid LEFT JOIN relationships on reservationmember.relationshipid = relationships.relationshipid WHERE 1 AND ( (reservation.arrivaldate >= '2016-10-01') ) AND ( (reservation.arrivaldate <= '2016-10-10') ) ORDER BY reservation.status desc, reservation.arrivaldate asc, reservation.reservationid asc LIMIT 0, 25 也会在外部服务器上产生许多单INSERT个,因此在这两种情况下都会有相同数量的往返行程。 / p>

如果存在可衡量的差异,我怀疑是因为处理INSERT循环的客户端或PL / pgSQL代码需要一些时间。当然,如果你在自己的事务中运行每个INSERT或者在INSERT语句中使用动态SQL,那么你会因此而变慢。