1

In a single transaction I am inserting may rows into a table, before inserting the row I perform a query to see if there is already a row with the key I am about to insert.

What I see is that the query to check the key exists gets very slow within my transaction, but from another transaction it is fast, and in the next transaction it is fast.

I cant break this work down into smaller transactions as the request I am processing needs to be in a single transaction.

Is there anything I can do to make the select query in this transaction fast?

4

2 回答 2

0

如果插入数百万条记录首先要做增量提交,因为您可能会遇到临时空间碎片或导致速度减慢的限制。这也可以在开始结束块中完成,这允许您通过添加索引

create index b indexName on table_name(col1, col2, col3);

与先前的答案状态相比,合并更快。
或者添加所有忽略重复然后删除重复这可以通过
例如

begin
    insert into table_name select * from table_name; [ if pulling from another table]or[use values and column maps]
    delete from table_name A where rowid >(select min(rowid) from table_name B where A.key_value=B.key_value);
end

如果在一个过程中,这也需要查询和删除都可以在开始结束块中并立即执行('you ddl statement here';');

于 2013-03-29T19:38:01.513 回答
0

所以,请添加约束/主键。这将允许您删除所有选择。可以考虑使用MERGE作为@Egor_Skriptunoff 推荐的方法。

或为您选择的列添加索引。

于 2013-03-21T11:00:11.020 回答