This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Replace uses of simple_insert_many with simple_insert_many_values. #11742
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
368fb8b
Replace uses of simple_insert_many with simple_insert_many_values.
clokep c7fcdaa
Remove unused simple_insert_many.
clokep 0744430
Replace uses of simple_insert_many_txn with simple_insert_many_values…
clokep 36919b1
Remove unused simple_insert_many_txn.
clokep eee1057
Rename simple_insert_many_values{_txn} to simple_insert_many{_txn}.
clokep e03a6a4
Newsfragment
clokep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Minor efficiency improvements when inserting many values into the database. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -934,56 +934,6 @@ def simple_insert_txn( | |
txn.execute(sql, vals) | ||
|
||
async def simple_insert_many( | ||
self, table: str, values: List[Dict[str, Any]], desc: str | ||
) -> None: | ||
"""Executes an INSERT query on the named table. | ||
|
||
The input is given as a list of dicts, with one dict per row. | ||
Generally simple_insert_many_values should be preferred for new code. | ||
|
||
Args: | ||
table: string giving the table name | ||
values: dict of new column names and values for them | ||
desc: description of the transaction, for logging and metrics | ||
""" | ||
await self.runInteraction(desc, self.simple_insert_many_txn, table, values) | ||
|
||
@staticmethod | ||
def simple_insert_many_txn( | ||
txn: LoggingTransaction, table: str, values: List[Dict[str, Any]] | ||
) -> None: | ||
"""Executes an INSERT query on the named table. | ||
|
||
The input is given as a list of dicts, with one dict per row. | ||
Generally simple_insert_many_values_txn should be preferred for new code. | ||
|
||
Args: | ||
txn: The transaction to use. | ||
table: string giving the table name | ||
values: dict of new column names and values for them | ||
""" | ||
if not values: | ||
return | ||
|
||
# This is a *slight* abomination to get a list of tuples of key names | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some value of "slight"! |
||
# and a list of tuples of value names. | ||
# | ||
# i.e. [{"a": 1, "b": 2}, {"c": 3, "d": 4}] | ||
# => [("a", "b",), ("c", "d",)] and [(1, 2,), (3, 4,)] | ||
# | ||
# The sort is to ensure that we don't rely on dictionary iteration | ||
# order. | ||
keys, vals = zip( | ||
*(zip(*(sorted(i.items(), key=lambda kv: kv[0]))) for i in values if i) | ||
) | ||
|
||
for k in keys: | ||
if k != keys[0]: | ||
raise RuntimeError("All items must have the same keys") | ||
|
||
return DatabasePool.simple_insert_many_values_txn(txn, table, keys[0], vals) | ||
|
||
async def simple_insert_many_values( | ||
self, | ||
table: str, | ||
keys: Collection[str], | ||
|
@@ -1002,11 +952,11 @@ async def simple_insert_many_values( | |
desc: description of the transaction, for logging and metrics | ||
""" | ||
await self.runInteraction( | ||
desc, self.simple_insert_many_values_txn, table, keys, values | ||
desc, self.simple_insert_many_txn, table, keys, values | ||
) | ||
|
||
@staticmethod | ||
def simple_insert_many_values_txn( | ||
def simple_insert_many_txn( | ||
txn: LoggingTransaction, | ||
table: str, | ||
keys: Collection[str], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -536,9 +536,9 @@ def _add_account_data_for_user( | |
self.db_pool.simple_insert_many_txn( | ||
txn, | ||
table="ignored_users", | ||
keys=("ignorer_user_id", "ignored_user_id"), | ||
values=[ | ||
{"ignorer_user_id": user_id, "ignored_user_id": u} | ||
for u in currently_ignored_users - previously_ignored_users | ||
(user_id, u) for u in currently_ignored_users - previously_ignored_users | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a handful (maybe 1/3rd?) of callers which could benefit from passing in some constant values for some keys, but I figured that could be left to another round of optimization, if we wanted to complicate the API. (I'm unsure if there's real benefit to it...) |
||
], | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I guess the efficiency savings here are:
PyObject*
pointers repeatedly pointing to the same set of keys (column names)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was mostly thinking of the allocation of all the dictionaries, but yes to all of that!
I also copied this changelog from the previous PR.