Do you have Laravel Debugbar enabled? If not, add it to your project. It is helpful to see which queries are executed and how long they take. This makes pin pointing the problem much easier.
https://github.com/barryvdh/laravel-debugbar
There could be a few things "wrong"
- One thing that could be slowing things down is doing the extra query to count the total records. Do you really need a full paginator or could you also use a simple prev/next ? If so you could use the SimplePaginate instead of the LengtAwarePaginator https://medium.com/@asked_io/optimizing-laravel-5-mysql-for-9m-rows-step-1-56285bad2d65
- I see you order on
service_datedo you have an index on that field? - Do you have indexes on the database fields you use to filter?
- How does the filter on staff name and client name look like? If it is a
LIKE %search%comparison it could slow things down because it needs to run through all of your records and is not able to use your database index. - I'm not sure what exactly is happening with the
$clientsvariable and the mapping but it looks a lot like the famous N+1 problem. Are you not able to resolve this with smarter eager loading?
In short, first figure out which queries are executed and which ones are slow or which ones you can optimize by using eager loading.
And to answer your other questions:
Is there a way to measure this query for debugging without replicating production data? (Due to privacy regulation, I really don't want production data on my machine)
You could use database seeders with the help of model factories and faker. See https://laravel.com/docs/5.7/seeding
Could removing the note_text field have an effect on the time of the query to complete?
Probably not, it looks like you only retrieve 25 records with this value and do not filter or sort on this field so that shouldn't be the biggest problem here.