-
Notifications
You must be signed in to change notification settings - Fork 134
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
Make select Faster #154
base: master
Are you sure you want to change the base?
Make select Faster #154
Conversation
lore/io/connection.py
Outdated
@@ -272,7 +276,21 @@ def select(self, sql=None, extract=None, filename=None, **kwargs): | |||
|
|||
@query_cached | |||
def _select(self, sql, bindings): | |||
return self.__execute(sql, bindings).fetchall() | |||
if self._use_psycopg2: | |||
try: |
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.
Wonder if there is value in falling back to SQL alchemy as a final try before giving up completely.
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.
Not a blocker though. This looks good!
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.
If self._use_psycopg2 is False, we immediately return the result for the alternative execution.
Merged the two with statements into one line.
if the error isn't about "no results to fetch", the original exception will be automatically re-raised without needing to explicitly call raise e.
def _connection_execute(self, sql, bindings): | ||
if self._use_psycopg2: | ||
with self._connection.engine.raw_connection().connection as conn: | ||
with conn.cursor() as cursor: | ||
cursor.execute(sql, bindings) | ||
try: | ||
return ResultWrapper(cursor.fetchall()) | ||
except psycopg2.ProgrammingError as e: | ||
if 'no results to fetch' in str(e): | ||
return None | ||
raise e | ||
else: | ||
return self._connection.execute(sql, bindings) |
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.
def _connection_execute(self, sql, bindings): | |
if self._use_psycopg2: | |
with self._connection.engine.raw_connection().connection as conn: | |
with conn.cursor() as cursor: | |
cursor.execute(sql, bindings) | |
try: | |
return ResultWrapper(cursor.fetchall()) | |
except psycopg2.ProgrammingError as e: | |
if 'no results to fetch' in str(e): | |
return None | |
raise e | |
else: | |
return self._connection.execute(sql, bindings) | |
def _connection_execute(self, sql, bindings): | |
if not self._use_psycopg2: | |
return self._connection.execute(sql, bindings) | |
try: | |
with self._connection.engine.raw_connection().connection as conn, conn.cursor() as cursor: | |
cursor.execute(sql, bindings) | |
return ResultWrapper(cursor.fetchall()) | |
except psycopg2.ProgrammingError as e: | |
if 'no results to fetch' not in str(e): | |
raise | |
What
Use Psycopg2 directly for PG selects in order to improve performance.
Why
sqlalchemy ads ~1ms of additional latency for basic selects and more for queries which return large result sets.
Without sqlalchemy
With sqlalchemy
sql_test.py