F
Filamentβ€’2y ago
eoncode

Running Bulk Action with 'Select All' times out or runs out of memory

Hello, I have a table with lots of rows and running a bulk action while 'Select All' is checked wither returns a timeout error (from cloudflare) or an internal server error where the PHP process runs out of memory. In the action, I'm simple chunking the selected records and dispatching a job for each chunk. I realise that the cause of the issue is the fact that filament loads all the selected records into memory before passing them down to the action's closure. If this isn't an issue the maintainers want to jump on quickly, 'm thinking of modifying this behaviour such that only the keys/PK of the selected table items are passed down, basically something more memory efficient. However, so far, I've had no luck finding where filament processes the action callback.
Solution:
I just realized you can already access the list of selected IDs: ```php ->action(function ($livewire) { // $livewire->selectedTableRecords })...
Jump to solution
7 Replies
toeknee
toekneeβ€’2y ago
How many rows? How much memory? You should look how to optimise it
eoncode
eoncodeOPβ€’2y ago
For our UserResource, we have a bulk action that admin can use to send emails to selected users. There are almost 42k users on the system and we have the timeout error when all users are selected and the bulk action is submitted
You should look how to optimise it
Exactly. I'm looking into filament code for where it executes bulk action to see where I might be able to tweak, if you could point me there. Essentially, filament is loading all the 42k rows into memory before executing the action which is not good for obvious reasons. Right now I've set memory limit in php.ini at 1.5gb
Patrick Boivin
Patrick Boivinβ€’2y ago
Hi @eoncode Can't say I'm too familiar with the internals but I've been digging quite a bit in the context of my current project (and just for fun πŸ€“) I believe what you're referring to is this: https://github.com/filamentphp/filament/blob/2.x/packages/tables/src/Concerns/HasBulkActions.php#L186 The call to getSelectedTableRecords() loads all records for the current selection. Something you could explore to optimize it would be to override the Table method (to prevent loading all records in memory) and use a custom BulkAction class where you can inject a list of IDs instead of the default $records https://github.com/filamentphp/filament/blob/2.x/packages/tables/src/Actions/BulkAction.php#L58
Solution
Patrick Boivin
Patrick Boivinβ€’2y ago
I just realized you can already access the list of selected IDs:
->action(function ($livewire) {
// $livewire->selectedTableRecords
})
->action(function ($livewire) {
// $livewire->selectedTableRecords
})
eoncode
eoncodeOPβ€’2y ago
@pboivin awesome. thanks πŸ‘ this looks like what I need
toeknee
toekneeβ€’2y ago
Or you ca use:
->action(function (Collection $records) {
// $records
->action(function (Collection $records) {
// $records
Patrick Boivin
Patrick Boivinβ€’2y ago
I think emmann is trying to access the IDs without loading the collection of records
Want results from more Discord servers?
Add your server