Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
a03dd095
Unverified
Commit
a03dd095
authored
1 year ago
by
Ngoc Khuat
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Detect N+1 hydration and throw an exception (#40272)
parent
b277fcc7
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/metabase/models/interface.clj
+22
-0
22 additions, 0 deletions
src/metabase/models/interface.clj
with
22 additions
and
0 deletions
src/metabase/models/interface.clj
+
22
−
0
View file @
a03dd095
...
...
@@ -8,6 +8,7 @@
[
clojure.walk
:as
walk
]
[
malli.core
:as
mc
]
[
malli.error
:as
me
]
[
metabase.config
:as
config
]
[
metabase.legacy-mbql.normalize
:as
mbql.normalize
]
[
metabase.legacy-mbql.schema
:as
mbql.s
]
[
metabase.lib.core
:as
lib
]
...
...
@@ -704,6 +705,27 @@
[
_original-model
dest-key
_hydrated-key
]
[(
u/->snake_case_en
(
keyword
(
str
(
name
dest-key
)
"_id"
)))])
(
methodical/defmethod
t2.hydrate/hydrate-with-strategy
:around
::t2.hydrate/multimethod-simple
"Throws an error if do simple hydrations that make DB call on a sequence."
[
model
strategy
k
instances
]
(
if
(
or
config/is-prod?
(
<
(
count
instances
)
2
)
;; we skip checking these keys because most of the times its call count
;; are from deferencing metabase.api.common/*current-user-permissions-set*
(
#
{
:can_write
:can_read
}
k
))
(
next-method
model
strategy
k
instances
)
(
t2/with-call-count
[
call-count
]
(
let
[
res
(
next-method
model
strategy
k
instances
)
;; if it's a lazy-seq then we need to realize it so call-count is counted
res
(
if
(
instance?
clojure.lang.LazySeq
res
)
(
doall
res
)
res
)]
;; only throws an exception if the simple hydration makes a DB call
(
when
(
pos-int?
(
call-count
))
(
throw
(
ex-info
(
format
"N+1 hydration detected!!! Model %s, key %s]"
(
pr-str
model
)
k
)
{
:model
model
:strategy
strategy
:k
k
:items-count
(
count
instances
)
:db-calls
(
call-count
)})))
res
))))
(
mu/defn
instances-with-hydrated-data
"Helper function to write batched hydrations.
Assoc to each `instances` a key `hydration-key` with data from calling `instance-key->hydrated-data-fn` by `instance-key`.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment