Searching in big database results in timeout
Hello, when searching in my table I keep running into timeouts.
The exact error is "Maximum execution time of 60 seconds exceeded" at the return statement of the Database\Connection.php
The database has a field uuid which contains an uuid. When I write a manual select statement it takes about 0.8s to get the results. I also tried in another Laravel project and the queries seem to execute quickly.
This is the code for the table function
I also tried giving it a custom query analogue to what I did in the other laravel project (it really just is a where statement), producing this
Have I missed something where I could've changed something?
(I am in my early php stages)
What I just found out through some more testing is that the search works fine when I use a uuid that in the first couple of hundred (or thousand for that matter) rows, but using one that is "further away" seems to hang the search entirely.
Thanks in advance for any pointers
Solution:Jump to solution
Ah I found the "issue"
Selecting from the database is very quick, but not if you add
order by xxx asc
...11 Replies
Hmm this can be a lot of things
1) Install Laravel debugbar to see which query is actually the slowest. It might be that you're running into a n+1 issue
2) Disable pagination on the table to see if it helps
3) Relevant to the 1st point, check pre-load any relationships you might be using
You don't need debugbar if you're familiar with other ways of profiling your app's queries
Out of curiosity, how many rows are we talking about here?
I face similar issue when I try to attach record in Relationship manager while using Select Serach. It times out. And when I use pre loading then it goes in infinite wait in loading the button.
The last query I see in debugging is just an aggregate query that does count(*) on the table with the filter I set, that query runs in 0.something ms
I will try it w/o pagination later
Currently I do not have any relationships configured but I will check
The initial amount of rows is quite large but once you set the uuid filter it is down to like 170k rows
The important question: Is the query fast without Filament?
Yes, in my only laravel project the query with a
$query->where('uuid', $uuid)
runs in under a second
and if I do a direct SELECT * FROM xxx WHERE uuid="..."
it also runs in under a secondSolution
Ah I found the "issue"
Selecting from the database is very quick, but not if you add
order by xxx asc
No I probably only need to figure out how to remove that from the query
Changing the default sort did the trick
You are missing an index then.
The table does have an index 🤔
Yeah but probably not the right one if you have a default sorting. One index isn’t enough if you want fast sorting for different orders, filters etc.
I changed it to sort by a timedate column since sorting by the id doesn't make sense in this case.