Skip to content

perf: optimise api/v3/license#2442

Draft
rh-jfuller wants to merge 1 commit into
guacsec:mainfrom
rh-jfuller:remove_superfluous_orderby
Draft

perf: optimise api/v3/license#2442
rh-jfuller wants to merge 1 commit into
guacsec:mainfrom
rh-jfuller:remove_superfluous_orderby

Conversation

@rh-jfuller

@rh-jfuller rh-jfuller commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

The GET /v3/license endpoint (license/service/mod.rs:297-417) has a few problems:

  1. The COUNT subquery includes a redundant ORDER BY
    At line 376, union_query.clone() is used as a subquery for COUNT. But union_query already has ORDER BY text ASC applied (line 369). The COUNT query becomes:
    SELECT COUNT(*) FROM (
    SELECT ... UNION ... ORDER BY text ASC -- ORDER BY is useless inside COUNT
    ) AS subquery
    PostgreSQL must sort the entire UNION result before counting, for no reason.

  2. a few other oddities

  3. replace sbom_source_document_id_idx with covering index sbom_source_doc_id_covering_idx

This fix removes the ORDER BY and tries to ensure we avoid stacking up these queries

| 11903 | postgres | postgres | 109.81.20.200 | 0:01:36.412994 | <null>          | <null>     | SELECT COUNT(*) AS "num_items" FROM (SELECT DISTINCT "expanded_license"."expanded_text" AS "text" FROM "expanded_license" UNION (SELECT DISTINCT "license"."text" AS "text" FROM "license" WHERE NOT EXISTS(SELECT $1 FROM "sbom_license_expanded" WHERE "sbom_license_expanded"."license_id" = "license"."id"))) AS "subquery" |
| 11921 | postgres | postgres | 109.81.20.200 | 0:01:20.804414 | <null>          | <null>     | SELECT COUNT(*) AS "num_items" FROM (SELECT DISTINCT "expanded_license"."expanded_text" AS "text" FROM "expanded_license" UNION (SELECT DISTINCT "license"."text" AS "text" FROM "license" WHERE NOT EXISTS(SELECT $1 FROM "sbom_license_expanded" WHERE "sbom_license_expanded"."license_id" = "license"."id"))) AS "subquery" |
| 11927 | postgres | postgres | 109.81.20.200 | 0:01:03.240111 | <null>          | <null>     | SELECT COUNT(*) AS "num_items" FROM (SELECT DISTINCT "expanded_license"."expanded_text" AS "text" FROM "expanded_license" UNION (SELECT DISTINCT "license"."text" AS "text" FROM "license" WHERE NOT EXISTS(SELECT $1 FROM "sbom_license_expanded" WHERE "sbom_license_expanded"."license_id" = "license"."id"))) AS "subquery" |
| 11907 | postgres | postgres | 109.81.20.200 | 0:00:03.722634 | <null>          | <null>     | SELECT DISTINCT "expanded_license"."expanded_text" AS "text" FROM "expanded_license" UNION (SELECT DISTINCT "license"."text" AS "text" FROM "license" WHERE NOT EXISTS(SELECT $1 FROM "sbom_license_expanded" WHERE "sbom_license_expanded"."license_id" = "license"."id")) ORDER BY text ASC, text ASC LIMIT $2 OFFSET $3      |

Summary by Sourcery

Enhancements:

  • Refine the COUNT query backing api/v3/license to run against an unsorted subquery, reducing overhead from superfluous ORDER BY in count operations.

@sourcery-ai

sourcery-ai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors license query construction so that the COUNT query is built from an unsorted subquery, avoiding unnecessary ORDER BY in the count operation and improving performance.

Flow diagram for updated license count query construction

flowchart TD
    A[Build spdx_query and non_sbom_query] --> B[Union queries into spdx_query]
    B --> C[Clone spdx_query into count_subquery]
    C --> D[Build count_query using count_subquery - no ORDER BY]
    B --> E[Add LICENSE_TEXT expression and ORDER BY to spdx_query]
    E --> F[Convert spdx_query to union_query for data results]
Loading

File-Level Changes

Change Details Files
Build the COUNT query from a cloned, unsorted version of the SPDX+non-SBOM union query instead of the final sorted union query.
  • Clone the combined SPDX query before applying ORDER BY to create an unsorted subquery for counting.
  • Construct a COUNT(*) select that uses the cloned subquery as its FROM source.
  • Remove the previous COUNT query that used the fully built union_query (including ORDER BY) as its subquery.
modules/fundamental/src/license/service/mod.rs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@rh-jfuller rh-jfuller changed the title perf: remove superfluous order by in query backing api/v3/license. minor: remove superfluous order by in query backing api/v3/license. Jul 2, 2026
@rh-jfuller rh-jfuller force-pushed the remove_superfluous_orderby branch 2 times, most recently from d1d16c2 to 7da9b0b Compare July 2, 2026 15:11
@rh-jfuller rh-jfuller changed the title minor: remove superfluous order by in query backing api/v3/license. perf: optimise api/v3/license Jul 2, 2026
@rh-jfuller rh-jfuller requested a review from ctron July 2, 2026 16:03
@rh-jfuller rh-jfuller self-assigned this Jul 2, 2026
@rh-jfuller rh-jfuller force-pushed the remove_superfluous_orderby branch from 7da9b0b to 83fa2d0 Compare July 2, 2026 17:05
@rh-jfuller rh-jfuller marked this pull request as draft July 3, 2026 03:48
@rh-jfuller

Copy link
Copy Markdown
Contributor Author

this PR is more for discussion - there are performance issues here but unclear what expectation of api/v3/license

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant