【发布时间】:2013-05-29 06:10:05
【问题描述】:
我正在实施一种请求机制,用户必须批准请求。为此,我实现了一个临时表和主表。最初,当请求被添加时,数据将被插入到临时表中,经批准后,它将被复制到主表中。
问题是批准后将有超过 5k 行移动到主表 + 明细表中的每行另外 3-5 行(存储详细信息)。 我现在的实现是这样的
//Get the rows from temporary table (batch_temp)
//Loop through the data
//Insert the data to the main table (batch_main) and return the id
//Get the details row from the temporary detail table (batch_temp_detail) using detail_tempid
//Loop through the data
//Insert the details to the detail table (batch_main_detail) with the main table id amount_id
//End Loop
//End Loop
但这种实现至少需要 20k 次查询。有没有更好的方法来实现相同的。
我尝试创建一个 sqlfiddle,但无法创建一个。所以我把查询粘贴到pgsql.privatepaste.com
【问题讨论】:
-
你试过select into吗?
-
我没有尝试过
SELECT INTO,但我已经阅读了文档。据我了解SELECT INTO将返回无效。但是我需要batch_main中的amount_id来将相应的数据插入batch_main_detail。 -
您是否有一个字段可以标记临时表和主表中记录的关系?如果你有,我想你可以用 2 个查询来完成你的任务,给我看看你的表结构。
-
@Tarzan 我已将链接附加到问题中的表结构。请检查一下。在该结构中
batchid是连接主表和临时表中的行的关系
标签: php sql postgresql