Skip to content
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

Fix double EXPLAIN when calling explain on queryset #654

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion silk/sql.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
import traceback

from django.core.exceptions import EmptyResultSet
Expand Down Expand Up @@ -53,7 +54,11 @@ def _explain_query(connection, q, params):

# currently we cannot use explain() method
# for queries other than `select`
prefixed_query = f"{prefix} {q}"
if re.match(f"^{prefix} .*", q):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about using just str.startswith() instead?

Suggested change
if re.match(f"^{prefix} .*", q):
if q.startswith(prefix):

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that will work!

# to avoid "EXPLAIN EXPLAIN", do not add prefix
prefixed_query = q
else:
prefixed_query = f"{prefix} {q}"
with connection.cursor() as cur:
cur.execute(prefixed_query, params)
result = _unpack_explanation(cur.fetchall())
Expand Down