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

SSP controller: avoid expensive subqueries #1173

Closed
irees opened this issue Aug 22, 2017 · 0 comments
Closed

SSP controller: avoid expensive subqueries #1173

irees opened this issue Aug 22, 2017 · 0 comments
Assignees

Comments

@irees
Copy link
Member

irees commented Aug 22, 2017

Using operator_onestop_id calls Operator.find_by_onestop_ids!

    operators = Operator.find_by_onestop_ids!(AllowFiltering.param_as_array(params, :operator_onestop_id))
    @ssps = @ssps.where(operator: operators)

which produces a query using a subquery to find operators:

SELECT  "current_schedule_stop_pairs".* FROM "current_schedule_stop_pairs" WHERE "current_schedule_stop_pairs"."operator_id" IN (SELECT id FROM "current_operators" WHERE "current_operators"."onestop_id" = 'o-123-81')  ORDER BY "current_schedule_stop_pairs"."id" ASC LIMIT 51 OFFSET 0

This is very expensive and can timeout.

Using pluck on Operator.find_by_onestop_ids! (at the cost of a second query) produces a simpler, more efficient, more index-searchable form for the main query:

SELECT  "current_schedule_stop_pairs".* FROM "current_schedule_stop_pairs" WHERE "current_schedule_stop_pairs"."operator_id" = 1  ORDER BY "current_schedule_stop_pairs"."id" ASC LIMIT 51 OFFSET 0

Use this form in all SSP query params, and consider expanding to all controllers.

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

No branches or pull requests

1 participant