为什么批量插入更快?是因为插入单行的连接和设置开销对于一组行是相同的吗?还有哪些因素会使批量插入更快?
批量更新如何工作?假设表没有唯一性约束,则insert语句对批处理中的其他insert语句实际上没有任何影响.但是,在批量更新期间,更新可能会更改表的状态,因此可能会影响批处理中其他更新查询的结果.
我知道批量插入查询的语法是在一个大查询中包含所有插入值.批量更新查询如何?例如,如果我有单一的表单更新查询:
update <table> set <column>=<expression> where <condition1>
update <table> set <column>=<expression> where <condition2>
update <table> set <column>=<expression> where <condition3>
update <table> set <column>=<expression> where <condition4>
Run Code Online (Sandbox Code Playgroud)
它们在批处理中使用时会发生什么.单个查询会是什么样的?
批量插入和更新是SQL标准的一部分吗?
在现有的 PostgreSQL 表中,我想要UPDATE几个现有列的值来自字典查找(请参阅下面的字典)。有点像这篇不错的博客文章中描述的那样。但是,我不知道如何使用 Python 字典来做到这一点。可怕的伪代码来了:
d = {10:'chair', 11:'table', 12:'lamp',
20:'english ivy', 21:'peace lily', 22:'spider plant'}
curs.execute("""
UPDATE my_table t
SET furniture = %(t.furniture)s,
SET plant = %(t.plant)s""",
d)
Run Code Online (Sandbox Code Playgroud)
在最初的表看起来有点像这样:
gid | furniture | plant
-----------------------
0 | 10 | 21
1 | 11 | 20
...
Run Code Online (Sandbox Code Playgroud)
之后的操作就应该是这样的:
gid | furniture | plant
-----------------------------
0 | chair | peace lily
1 | table | english ivy
...
Run Code Online (Sandbox Code Playgroud)
这是可能的还是我必须遍历表格?