Avoid SQL query pessimization when auxiliary database(s) used - #4833
Merged
Conversation
rouault
reviewed
Sep 1, 2026
rouault
approved these changes
Sep 1, 2026
rouault
left a comment
Member
There was a problem hiding this comment.
LGTM. Can you rework the commit history to blend together the fixup commit and its original one?
Looking at the EXPLAIN QUERY PLAN for this query, it was materializing intermediate results if an auxiliary db was in use. Dropping the ORDER BY avoided the need to materialize and significantly improves the performance of the query.
wrenoud
force-pushed
the
avoid-materialize
branch
from
September 1, 2026 12:45
1f6d2de to
b3ddc49
Compare
Contributor
Author
Done! Thanks @rouault |
jjimenezshaw
reviewed
Sep 1, 2026
jjimenezshaw
approved these changes
Sep 1, 2026
Member
|
Thanks @wrenoud ! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Investigating the performance of cs2cs when an auxiliary database is used, the query run by
AuthorityFactory::Private::createPropertiesSearchUsageswas found to be particularly slow. Reviewing the results ofEXPLAIN QUERY PLANon the query it was found that intermediate results from theVIEWs used toUNIONthe primary and auxiliary databases were beingMATERIALIZE'ed (see details below). This was in affect duplicating the tables in memory for the query.Details
I first attempted to use temporary tables for
extentandscope, which incurred an overhead of table creation, but significantly improved the query time. But @rouault pointed out that possibly theORDER BYwas the cause for theMATERIALIZE, and it was. That is what is implemented here. The sort is done after the query. You can see the newEXPLAIN QUERY PLANin the details below.Details
I also added a minor performance improvement in
DatabaseContext::Private::attachExtraDatabases, the columns were being aggregated into a string multiple times per table for each database. I switch to doing it once and reusing the joined columns string.Results
This is for Windows 11, Intel Core Ultra 7, running the following command:
Below you can see for various values of environment variable
PROJ_AUX_DBthe per-query average over 4 runs, with 11 calls per run.empty.dbis initialized with no entries, andnsrs_proj.dbis from https://github.com/jjimenezshaw/NSRS-2022-PROJPROJ_AUX_DBmaster(4 runs, n=44)
(4 runs, n=44)