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

Improve performance of statistics queries #494

Merged
merged 1 commit into from
Jul 15, 2024

Conversation

aolley
Copy link
Contributor

@aolley aolley commented Jul 15, 2024

Without this, the many sub-queries in stats calculations that JOIN the question_references table dont hit any of the indexes on that table. For sites with large question_references tables - this can be horrible for performance as it ends up doing a seqscan on that table.

By adding the usingcontextid field, we instead get indexscan's.

@timhunt
Copy link
Member

timhunt commented Jul 15, 2024

Thank you for doing this. I should have noticed this was necessary before. Very important to hit this index. However, since this is all about query performance ...

These queries all process data from a single student quiz. Therefore, there will only every be a single contextid involved, and therefore there is no need to join the context table! Just add AND qr.usingcontextid = :contextid4 in the join on question_references, and add the extra query parameters in get_attempt_stat_joins_params. (Not my code that the params and the SQL are initialised so far apart, but now is probably not the time to fix that.)

Are you able to make this change? (If not, let me know.) Thanks.

@aolley
Copy link
Contributor Author

aolley commented Jul 15, 2024 via email

@aolley
Copy link
Contributor Author

aolley commented Jul 15, 2024

@timhunt I've updated the change to instead pass the contextid to where its needed and include it in the queries. I've done some quick testing so far and it looks like it does what it needs to do.

@timhunt
Copy link
Member

timhunt commented Jul 15, 2024

Thanks. That is better.

It is a trade-off between changing all thost function calls, and just repeating $contextid = context_module::instance($cmid)->id in the various functions. Contexts are cached, so repeating that is not a performance issue. Is it very annoying if I ask you to change it again?

@aolley
Copy link
Contributor Author

aolley commented Jul 15, 2024 via email

Without this, the many sub-queries in stats calculations that JOIN the
question_references table dont hit any of the indexes on that table. For
sites with large question_references tables - this can be horrible for
performance as it ends up doing a seqscan on that table.

By adding the usingcontextid field, we instead get indexscan's.
@timhunt timhunt merged commit a4841ba into studentquiz:main Jul 15, 2024
2 of 4 checks passed
@timhunt
Copy link
Member

timhunt commented Jul 15, 2024

Thanks. That looks good now. Merged.

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