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

Cache MiqExpression.get_col_type in MiqReport::Formatting #17195

Conversation

NickLaMuro
Copy link
Member

@NickLaMuro NickLaMuro commented Mar 23, 2018

I noticed while running a report that there were a lot of 0 row queries being executed against the DB when running a report. Turns out that when a report is building the html data, the MiqReport::Formatting code is checking for every column in every row what the format should be, even though it shouldn't change on a row by row basis.

Specifically, this becomes a problem when it uses MiqExpression.get_col_type to determine the format as it will fetch from the DB what the column type is to help determine the format. This query does not get cached, so it is duplicated for every column in each row of the report dataset.

The query in my tests (from the report described in #17141 ) that was being repeated in the logs was as follows:

SELECT  "custom_attributes".*
FROM "custom_attributes"
WHERE "custom_attributes"."name" = 'build-date:SECTION:docker_labels'
  AND "custom_attributes"."resource_type" = 'ContainerImage'
ORDER BY "custom_attributes"."id" ASC
LIMIT 1

Benchmarks

Using the same dataset and report from #17141 , the difference before and after this change is as follows:

ms queries query (ms) rows
before 910076 219950 444531.4 80492
after 250861 121752 43773.2 80491

Of note: The changes from #17141 were in place in both runs of the above.

Links

@NickLaMuro NickLaMuro force-pushed the cache_miq_report_format_column_type_lookup branch from f20a3aa to 3862d36 Compare March 28, 2018 20:44
This call will make the same call to the DB over and over again on
certain attributes, and is incredibly "N+1y" on large reports.

This adds a simple cache for the data return from the DB calls to make
sure they are only executed once per column.  Moved into a separate
method to break up the bulk of the current method.

Note:  An extra `to_sym` was removed when in the new method since it
would always get executed on the next line if the value existed.
Probably could remove the `unless` as well since at that point, as we
should be clear of any `nil` errors... 🤷
@NickLaMuro
Copy link
Member Author

@miq-bot assign @gtanzillo

Gregg, figured I would assign this to you since the git history has your fingerprints all over it ;)

Feel free to re-assign if you have someone better in mind to look at this. Thanks.

Copy link
Member

@kbrock kbrock left a comment

Choose a reason for hiding this comment

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

in a report, the same columns will be used over and over again. so the premise is to just cache the types for the duration of the report

@miq-bot
Copy link
Member

miq-bot commented Mar 28, 2018

Checked commit NickLaMuro@3862d36 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
1 file checked, 0 offenses detected
Everything looks fine. 👍

Copy link
Member

@gtanzillo gtanzillo left a comment

Choose a reason for hiding this comment

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

Looks goos. Nice find @NickLaMuro!

@gtanzillo gtanzillo added this to the Sprint 83 Ending Apr 9, 2018 milestone Apr 2, 2018
@gtanzillo gtanzillo merged commit 1b18fb1 into ManageIQ:master Apr 2, 2018
@NickLaMuro
Copy link
Member Author

@gtanzillo Thanks!

Based on the BZ that this is for, I think we want to backport this to the G release. That seem reasonable?

@gtanzillo
Copy link
Member

@NickLaMuro

Based on the BZ that this is for, I think we want to backport this to the G release. That seem reasonable?

👍 I added the necessary labels

@NickLaMuro
Copy link
Member Author

Thanks!

simaishi pushed a commit that referenced this pull request Apr 10, 2018
…umn_type_lookup

Cache MiqExpression.get_col_type in MiqReport::Formatting
(cherry picked from commit 1b18fb1)

https://bugzilla.redhat.com/show_bug.cgi?id=1565677
@simaishi
Copy link
Contributor

Gaprindashvili backport details:

$ git log -1
commit 051eb05e939bf56460003e86e4c0378b80a54772
Author: Gregg Tanzillo <[email protected]>
Date:   Mon Apr 2 14:57:13 2018 -0400

    Merge pull request #17195 from NickLaMuro/cache_miq_report_format_column_type_lookup
    
    Cache MiqExpression.get_col_type in MiqReport::Formatting
    (cherry picked from commit 1b18fb151cd6cadd51f9efc67ed817e69610e7cd)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1565677

simaishi pushed a commit that referenced this pull request Apr 10, 2018
…umn_type_lookup

Cache MiqExpression.get_col_type in MiqReport::Formatting
(cherry picked from commit 1b18fb1)

https://bugzilla.redhat.com/show_bug.cgi?id=1565678
@simaishi
Copy link
Contributor

Fine backport details:

$ git log -1
commit 6d24a5d1e87dfa27efc7c653c64d5b62031b2b6a
Author: Gregg Tanzillo <[email protected]>
Date:   Mon Apr 2 14:57:13 2018 -0400

    Merge pull request #17195 from NickLaMuro/cache_miq_report_format_column_type_lookup
    
    Cache MiqExpression.get_col_type in MiqReport::Formatting
    (cherry picked from commit 1b18fb151cd6cadd51f9efc67ed817e69610e7cd)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1565678

d-m-u pushed a commit to d-m-u/manageiq that referenced this pull request Jun 6, 2018
…rmat_column_type_lookup

Cache MiqExpression.get_col_type in MiqReport::Formatting
(cherry picked from commit 1b18fb1)

https://bugzilla.redhat.com/show_bug.cgi?id=1565678
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants