-
Notifications
You must be signed in to change notification settings - Fork 6
Enhanced scan report upload speed #1227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7a6c287
Improve speed of writing to DB
brian-kim31 383fc31
Added fast_executemany and commit_every params to improve the speed o…
brian-kim31 5528449
increase commit to 10k
brian-kim31 5e8e69c
Added Commit every row to avoid transaction overhead
brian-kim31 8494f21
Added Commit every row to avoid transaction overhead
brian-kim31 cfbac32
Commit every 10k
brian-kim31 3683def
Implement execute_values to make SR upload fast
brian-kim31 30e4074
Increase Page size to 1000
brian-kim31 71bf9d4
Merge branch 'main' into optimization
brian-kim31 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| from libs.settings import AIRFLOW_DAGRUN_TIMEOUT, AIRFLOW_DEBUG_MODE | ||
| from libs.utils import update_job_status | ||
| from openpyxl.worksheet.worksheet import Worksheet | ||
| from psycopg2.extras import execute_values | ||
|
|
||
| # PostgreSQL connection hook | ||
| pg_hook = PostgresHook( | ||
|
|
@@ -105,26 +106,35 @@ def update_temp_data_dictionary_table( | |
| } | ||
| ) | ||
|
|
||
| # Insert records into the temporary table | ||
| # Insert records into the temporary table using execute_values for fast bulk inserts | ||
| if dictionary_records: | ||
| pg_hook.insert_rows( | ||
| table=f"temp_data_dictionary_{scan_report_id}", | ||
| rows=[ | ||
| ( | ||
| d["table_name"], | ||
| d["field_name"], | ||
| d["value"], | ||
| d["value_description"], | ||
| ) | ||
| for d in dictionary_records | ||
| ], | ||
| target_fields=[ | ||
| "table_name", | ||
| "field_name", | ||
| "value", | ||
| "value_description", | ||
| ], | ||
| ) | ||
| conn = pg_hook.get_conn() | ||
| cursor = conn.cursor() | ||
| try: | ||
| execute_values( | ||
| cursor, | ||
| f""" | ||
| INSERT INTO temp_data_dictionary_{scan_report_id} | ||
| (table_name, field_name, value, value_description) | ||
| VALUES %s | ||
| """, | ||
| [ | ||
| ( | ||
| d["table_name"], | ||
| d["field_name"], | ||
| d["value"], | ||
| d["value_description"], | ||
| ) | ||
| for d in dictionary_records | ||
| ], | ||
| ) | ||
| conn.commit() | ||
| except Exception: | ||
| conn.rollback() | ||
| raise | ||
| finally: | ||
| cursor.close() | ||
| conn.close() | ||
|
|
||
| logging.info( | ||
| f"Created temporary data dictionary table with {len(dictionary_records)} records" | ||
|
|
@@ -195,23 +205,34 @@ def create_temp_field_values_table( | |
| } | ||
| ) | ||
|
|
||
| # Using execute_values() for fast bulk inserts (see notes above) | ||
| if field_values_data: | ||
| pg_hook.insert_rows( | ||
| table=f"temp_field_values_{table_id}", | ||
| rows=[ | ||
| ( | ||
| d["field_name"], | ||
| d["value"], | ||
| d["frequency"], | ||
| ) | ||
| for d in field_values_data | ||
| ], | ||
| target_fields=[ | ||
| "field_name", | ||
| "value", | ||
| "frequency", | ||
| ], | ||
| ) | ||
| conn = pg_hook.get_conn() | ||
| cursor = conn.cursor() | ||
| try: | ||
| execute_values( | ||
|
Contributor
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. same here as well.
AndyRae marked this conversation as resolved.
Outdated
|
||
| cursor, | ||
| f""" | ||
| INSERT INTO temp_field_values_{table_id} | ||
| (field_name, value, frequency) | ||
| VALUES %s | ||
| """, | ||
| [ | ||
| ( | ||
| d["field_name"], | ||
| d["value"], | ||
| d["frequency"], | ||
| ) | ||
| for d in field_values_data | ||
| ], | ||
| ) | ||
| conn.commit() | ||
| except Exception: | ||
| conn.rollback() | ||
| raise | ||
| finally: | ||
| cursor.close() | ||
| conn.close() | ||
|
|
||
| except Exception as e: | ||
| logging.error(f"Error creating data dictionary table: {str(e)}") | ||
|
|
||
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.
It will be good to have some sanity check and batching on this input, at the moment I think this could be any size and we probably want a batch of a certain size.
Previously pg_hook.insert_rows did this automatically so we would want something similar to ensure it is robust.