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

queries: n+1 queries #722

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alexisvisco
Copy link

Description

This pull request aims to demonstrate an issue in GORM's query behavior as observed in the provided test function TestGORM. The function illustrates potential inefficiencies and unexpected results that may have been introduced due to a previous fix in GORM's preloading and join operations.

Background

Previously, a fix was implemented in GORM to address an issue where, when joining on Account.Pet and then preloading Account.Companies, the account was loaded twice, even though it was already present in the main query. This fix aimed to improve the consistency and efficiency of the query process.

Current Issue

However, the provided test function suggests that the fix may have unintentionally introduced another issue. The test highlights the following:

  • Unexpected Queries: The function is expected to execute two queries (one for fetching user data and another for related data). Instead, there may be an additional, unnecessary query, suggesting an n+1 query problem.

TLDR:

DB.Joins("Company").Preload("Company.Addresses").Find(&result).Error

is resulting in

2024/04/23 15:08:19 /Users/alexisviscogliosi/dev/go-playground/db.go:59
[1.205ms] [rows:2] SELECT * FROM "addresses" WHERE "addresses"."company_id" = 1

2024/04/23 15:08:19 /Users/alexisviscogliosi/dev/go-playground/db.go:59
[0.634ms] [rows:2] SELECT * FROM "addresses" WHERE "addresses"."company_id" = 2

2024/04/23 15:08:19 /Users/alexisviscogliosi/dev/go-playground/db.go:59
[3.667ms] [rows:2] SELECT "users"."id","users"."created_at","users"."updated_at","users"."deleted_at","users"."name","Company"."id" AS "Company__id","Company"."name" AS "Company__name","Company"."user_id" AS "Company__user_id" FROM "users" LEFT JOIN "companies" "Company" ON "users"."id" = "Company"."user_id

Instead of

SELECT `users`.`id`,`users`.`created_at`,`users`.`updated_at`,`users`.`deleted_at`,`users`.`name`,`Company`.`id` AS `Company__id`,`Company`.`name` AS `Company__name`,`Company`.`user_id` AS `Company__user_id` FROM `users
		LEFT JOIN `companies` `Company` ON `users`.`id` = `Company`.`user_id`
		WHERE `users`.`deleted_at` IS NULL
SELECT * FROM `companies` WHERE `companies`.`id` IN (1, 2)
  • Preloading Behavior: The use of preloading (Preload("Company.Addresses")) may be causing unexpected query behavior or inefficiencies. The preloading process might not be functioning as expected, potentially due to the previous fix.

Demonstration Purpose

This pull request serves as a demonstration of the potential impact of changes in GORM's behavior, particularly in relation to preloading and joining operations. It provides an opportunity to understand how these changes might affect database efficiency and application performance.

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.

None yet

1 participant