Skip to content

Commit be4b81b

Browse files
authored
Merge pull request vnpy#6 from tiger3927/main
save_bar_data chunked 加快批量速度
2 parents fea3f6e + 348a9e2 commit be4b81b

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

vnpy_postgresql/postgresql_database.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from peewee import (
55
AutoField,
6+
chunked,
67
CharField,
78
DateTimeField,
89
FloatField, IntegerField,
@@ -175,11 +176,18 @@ def save_bar_data(self, bars: List[BarData], stream: bool = False) -> bool:
175176
d.pop("vt_symbol")
176177
data.append(d)
177178

178-
# 使用upsert操作将数据更新到数据库中
179+
# 使用upsert操作将数据更新到数据库中 chunked批量操作加快速度
179180
with self.db.atomic():
180-
for d in data:
181-
DbBarData.insert(d).on_conflict(
182-
update=d,
181+
for c in chunked(data, 100):
182+
DbBarData.insert_many(c).on_conflict(
183+
update={DbBarData.volume: DbBarData.volume,
184+
DbBarData.turnover: DbBarData.turnover,
185+
DbBarData.open_interest: DbBarData.open_interest,
186+
DbBarData.open_price: DbBarData.open_price,
187+
DbBarData.high_price: DbBarData.high_price,
188+
DbBarData.low_price: DbBarData.low_price,
189+
DbBarData.close_price: DbBarData.close_price
190+
},
183191
conflict_target=(
184192
DbBarData.symbol,
185193
DbBarData.exchange,

0 commit comments

Comments
 (0)