Bug 1330306

Summary: two queries are performed when searching for erratum that does not exist
Product: Red Hat Satellite Reporter: Adam Price <adprice>
Component: PerformanceAssignee: Adam Price <adprice>
Status: CLOSED ERRATA QA Contact:
Severity: high Docs Contact:
Priority: unspecified    
Version: 6.2.0CC: bbuckingham, lpramuk, mmccune, oprazak
Target Milestone: UnspecifiedKeywords: Triaged
Target Release: Unused   
Hardware: Unspecified   
OS: Unspecified   
URL: http://projects.theforeman.org/issues/14268
Whiteboard:
Fixed In Version: rubygem-katello-3.0.0.34-1 Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-07-27 11:31:37 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Adam Price 2016-04-25 20:38:35 UTC
If you search for something that does not exist (for example, searching for erratum 'asdfasdf' without field specified), two search queries are performed. The first query is to get a count for pagination, but even if the count is zero, the second query is still performed. In some cases, these queries can take 3+ seconds, so running it once instead of twice can make the page much snappier.

To repro, sync some RH repos and then do the following:

<pre>
time curl -sk 'https://admin:changeme@localhost/katello/api/v2/errata?organization_id=3&search=sadfasdf' > /dev/null
</pre>

If you enable slow query logging in postgres (log_min_duration_statement = 500 will do it), you'll see two queries:

<pre>
LOG:  duration: 3125.694 ms  execute <unnamed>: SELECT COUNT(DISTINCT "katello_errata"."id") FROM "katello_errata" LEFT OUTER JOIN "katello_erratum_cves" ON "katello_erratum_cves"."erratum_id" = "katello_errata"."id" LEFT OUTER JOIN "katello_erratum_bugzillas" ON "katello_erratum_bugzillas"."erratum_id" = "katello_errata"."id" LEFT OUTER JOIN "katello_erratum_packages" ON "katello_erratum_packages"."erratum_id" = "katello_errata"."id" WHERE (("katello_errata"."errata_id" ILIKE '%sadfasdf%' OR "katello_errata"."severity" ILIKE '%sadfasdf%' OR "katello_errata"."errata_type" ILIKE '%sadfasdf%' OR "katello_erratum_cves"."cve_id" ILIKE '%sadfasdf%' OR "katello_erratum_bugzillas"."bug_id" ILIKE '%sadfasdf%' OR "katello_erratum_packages"."nvrea" ILIKE '%sadfasdf%' OR "katello_erratum_packages"."name" ILIKE '%sadfasdf%')) AND "katello_errata"."id" IN (SELECT DISTINCT "katello_errata"."id" FROM "katello_errata" INNER JOIN "katello_repository_errata" ON "katello_repository_errata"."erratum_id" = "katello_errata"."id" WHERE "katello_repository_errata"."repository_id" IN (SELECT "katello_repositories"."id" FROM "katello_repositories"  WHERE (katello_repositories.id ....

LOG:  duration: 3129.778 ms  execute <unnamed>: SELECT  DISTINCT "katello_errata"."id", katello_errata.updated AS alias_0, katello_errata.id AS alias_1 FROM "katello_errata" LEFT OUTER JOIN "katello_erratum_cves" ON "katello_erratum_cves"."erratum_id" = "katello_errata"."id" LEFT OUTER JOIN "katello_erratum_bugzillas" ON "katello_erratum_bugzillas"."erratum_id" = "katello_errata"."id" LEFT OUTER JOIN "katello_erratum_packages" ON "katello_erratum_packages"."erratum_id" = "katello_errata"."id" WHERE (("katello_errata"."errata_id" ILIKE '%sadfasdf%' OR "katello_errata"."severity" ILIKE '%sadfasdf%' OR "katello_errata"."errata_type" ILIKE '%sadfasdf%' OR "katello_erratum_cves"."cve_id" ILIKE '%sadfasdf%' OR "katello_erratum_bugzillas"."bug_id" ILIKE '%sadfasdf%' OR "katello_erratum_packages"."nvrea" ILIKE '%sadfasdf%' OR "katello_erratum_packages"."name" ILIKE '%sadfasdf%')) AND "katello_errata"."id" IN (SELECT DISTINCT "katello_errata"."id" FROM "katello_errata" INNER JOIN "katello_repository_errata" ON "katello_repository_errata"."erratum_id" = "katello_errata"."id" WHERE "katello_repository_errata"."repository_id" IN (SELECT "katello_repositories"."id" FROM "katello_repositories"  WHERE (katello_repositories.id in  ...
</pre>

Comment 1 Adam Price 2016-04-25 20:38:36 UTC
Created from redmine issue http://projects.theforeman.org/issues/14268

Comment 2 Adam Price 2016-04-25 20:38:38 UTC
Upstream bug assigned to adprice

Comment 4 Bryan Kearney 2016-05-16 20:21:37 UTC
Moving to POST since upstream bug http://projects.theforeman.org/issues/14268 has been closed

Comment 5 Ondřej Pražák 2016-05-27 13:43:23 UTC
When searching for non-existing errata, only the SELECT COUNT... query is performed. Verified with snap 13.1

Comment 6 Bryan Kearney 2016-07-27 11:31:37 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2016:1501