Duplicate queries despite closures and static variables
I changed to closures, which reduced duplicate queries.
Then I added some local caching for the current request by using static variables, further reducing duplicate queries.
Still, the query inside
options()
is called 3 times by hidden()
.
Is there any way to get rid of these duplicate queries?
18 Replies
One way to further reduce duplicate queries would be to move the query inside the
options()
closure to a higher level where it can be cached and reused across requests.
For example, you could create a separate function outside of the component definition that retrieves the options for a given arrangement_type_id
and caches the result. Then, you can call this function inside the options()
closure to get the cached options instead of querying the database.
Here's an example implementation:
In this implementation, the getArrangementOptions()
function uses a static cache variable to store the retrieved options for each arrangement_type_id
. If the options for a given arrangement_type_id
haven't been retrieved yet, the function queries the database and caches the result.
Inside the options()
closure, we simply call getArrangementOptions()
with the arrangement_type_id
parameter to get the cached options. The hidden()
closure doesn't need to change since it already uses the getOptions()
method to check if there are any options available.
By caching the options at a higher level and reusing them across requests, you can further reduce duplicate queries and improve performance.That's a possibility indeed. Maybe I'll even implement a generic model cache, or even a package. Thanks!
Any possibilities from within the Filament framework?
we dont cache within filament in case you want to implement dynamic options
Ok, I'll see if I can implement some cache-per-request layer myself.
What about relationships? As soon as I introduce a
I get 5 duplicate queries. I wouldn't know how to get rid of those.
I came up with this simple solution to prevent duplicate queries in most cases:
The first time during the request it will store the result in a global variable. Every subsequent call will use the stored result. The
__FILE__ . __LINE__
part is just to easily copy paste and have a unique key.
It removes the duplicate queries that remained while using a static variable inside the closure.
The only duplicates left now are the ones from ->relation()
. Any ideas to remove those are welcome.maybe the spatie once() helper
I did't know that one. It seems to me it's doing something similar to what I've done. At least the result is the same.
I don't see how I would implement
once()
in case of a Repeater::make('records')->relationship()
?5 duplicate queries when in the lifecycle?
4x this:
without the
GLOBALS
check.
While rendering the EditRequest page.
Sorry, you asked about the repeater...
4x this on the ->relation()
while rendering the EditRequest page:
are you on the latest filament version?
I am now. Same story.
im not sure then, it sounds like a bug but there might be a good reason for it.
Should I add an issue for it on Github?
if you want, its not a priority to fix though as it doesnt actually stop you from doing anything
you can submit a PR to fix if you want, that would be better
Ok. I'll use my
GLOBAL
hack to prevent the majority of duplicate queries. If I still get in trouble somewhere down the road I'll look into it. Thanks!if i were you, id prioritise code quality over microoptimizations and not bother to be honest 😁
I guess you're right. It's my first Filament project of hopefully probably many. So I want to set up everything really tight in order to prevent troubles way down the road.
yeah but i wouldnt worry about a probably tiny query running a handful of times
just my opinion with priorities
I agree a 1+n is way worse