Skip to content

Optimize SQL reformatting #1574

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

Merged
merged 1 commit into from
Jan 12, 2022

Conversation

adamchainz
Copy link
Contributor

Add a few bits of caching:

  1. Add sub-function to parse_sql with lru_cache, so that repeat calls with the same query are fast.
    This saves a lot of processing in N+1 situations.
  2. Cache constructed filter stacks in get_filter_stack().
    This avoids recreating all the various sqlparse objects for each query.
  3. Pre-compile the simplification regex.
    The re module already uses an internal LRU cache of regexes, but this avoids recompiling if the regex ever drops out of that cache.

Building on top of #1571, this takes run time for the same tested view from ~1100ms to ~950ms, another ~15% saving.

@adamchainz
Copy link
Contributor Author

After #1571 is merged, I'd like to rebase this and update the release note.

Add a few bits of caching:

1. Add sub-function to `parse_sql` with `lru_cache`, so that repeat calls with the same query are fast.
   This saves a lot of processing in N+1 situations.
2. Cache constructed filter stacks in `get_filter_stack()`.
   This avoids recreating all the various sqlparse objects for each query.
3. Pre-compile the simplification regex.
   The `re` module already uses an internal LRU cache of regexes, but this avoids recompiling if the regex ever drops out of that cache.

Building on top of django-commons#1571, this takes run time for the same tested view from ~1100ms to ~950ms, another ~15% saving.
@adamchainz adamchainz force-pushed the optimize_sql_panel_2 branch from 0c19469 to 64d9295 Compare January 12, 2022 12:50
@adamchainz
Copy link
Contributor Author

Rebased and updated

@matthiask
Copy link
Member

Thanks! That's awesome.

@matthiask matthiask merged commit ee606db into django-commons:main Jan 12, 2022
@adamchainz adamchainz deleted the optimize_sql_panel_2 branch January 12, 2022 13:29
gone pushed a commit to gone/django-debug-toolbar that referenced this pull request Jan 16, 2022
Add a few bits of caching:

1. Add sub-function to `parse_sql` with `lru_cache`, so that repeat calls with the same query are fast.
   This saves a lot of processing in N+1 situations.
2. Cache constructed filter stacks in `get_filter_stack()`.
   This avoids recreating all the various sqlparse objects for each query.
3. Pre-compile the simplification regex.
   The `re` module already uses an internal LRU cache of regexes, but this avoids recompiling if the regex ever drops out of that cache.

Building on top of django-commons#1571, this takes run time for the same tested view from ~1100ms to ~950ms, another ~15% saving.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants