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

Performance Improvements for has_many through queries #205

Merged
merged 5 commits into from
Dec 27, 2024

Conversation

ssteffen
Copy link
Collaborator

This PR introduces two optional improvements to conditions that allow for more efficient queries. These are specifically for conditions referencing a has_many :through association.

The two features are:
.with_through_id_relationship - this is simply a bool flag to tell Refine that we expect this to be an id attribute through an association. That way we can shortcut our queries to simply fetch the source primary_key and the target primary_key with the joins table rather than having to run two separate inner_joins to bridge the 3 tables

.with_forced_index(index_name) - Adds a FORCE INDEX() wrapper with the FROM query in the subquery generated for the condition.

So it will change a query like

SELECT `contacts`.* FROM `contacts` WHERE `contacts`.`workspace_id` = 3 AND `contacts`.`billable` = TRUE AND (`contacts`.`id` IN (SELECT `contacts`.`id` FROM `contacts` INNER JOIN `contacts_applied_tags` ON `contacts_applied_tags`.`contact_id` = `contacts`.`id` INNER JOIN `contacts_tags` ON `contacts_tags`.`id` = `contacts_applied_tags`.`tag_id` WHERE (`contacts_tags`.`id` IN (2, 3))))

To

SELECT `contacts`.* FROM `contacts` WHERE `contacts`.`workspace_id` = 3 AND `contacts`.`billable` = TRUE AND (`contacts`.`id` IN (SELECT `contacts_applied_tags`.`contact_id` FROM `contacts_applied_tags` FORCE INDEX(`index_contacts_applied_tags_on_contact_id`) WHERE (`contacts_applied_tags`.`tag_id` IN (2, 3))))

@ssteffen ssteffen self-assigned this Dec 16, 2024
@ssteffen ssteffen merged commit 3191367 into main Dec 27, 2024
1 check passed
@ssteffen ssteffen deleted the has_many_through_perf_improvements branch December 27, 2024 15:29
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