Tenancy and BadgeCount - seems to cause an N+1 problem of sorts
First of all... I love Filament!
I have a v3 installation running with Tenancy:
user
client
client_user
Very simple implementation.
For each Resource, I would like to display the "badge count" in the sidebar navigation menu.
Take a look at:
public static function getNavigationBadge(): ?string
{
return static::getModel()::count();
}
public static function getNavigationBadgeColor(): ?string
{
return static::getModel()::count() > 10 ? 'info' : 'gray';
}
Straight from the docs.
The problem is that I get 4 queries for "each" Tenant, of which I have 8 for a total of 32 duplicated queries.
Has anyone ran into this kind of problem before?
It seems that Filament::getTenant() works as it should, but is looping over every Tenant even though I'm only viewing data for one Tenant.
Any advice is greatly appreciated!
14 Replies
You running count for 2 times so it obviously going to get duplicated queries
It’s from official documentation
So? But do you understand what the code you copied do?
I definitely understand this code. It's obvious that count is called multiple times. But I don’t think that even in this case it will lead to the same result as the author. But again I could be wrong.
What I understand from the author is, there are 2 issues raised
1) duplicated queries
2) count from not current tenant are executed
For the tenant part I’m have no comment as I don’t use tenancy in my app
However the duplicate queries from count is due to the code the author copied so just to point it out
So if he worked out how to cache the count it should reduce the queries and then the remaining issue is on the tenancy
Yeap. I think you're right
When I remove tenancy from the app, then the query runs twice as the query is in the code twice, which I completely get. It seems that for every tenant, the query runs twice. So, if I have 10 tenants, then the query runs 2x per tenant for a total of 20 queries.
can you create an issue on github with a repo?
That's a good idea. I can work on that and post a link when I have it ready.
why not..
then you can remove getNavigationBadge and getNavigationBadgeColor and calculate the badge only once
it would be easier to define a
static::getModelCount
that stored to a static variable and use it in both methods rather than hack around like thisit would need to be a computed property, otherwise calling
static::getModelCount()
twice (one for the label and one for the color) would have the same resultnot tested but this should works, no?
yeah, but that's a project-spacific code, i doubt it will be included into the core