I tried using the same eloquent query for a specific field to be both ASC and DESC. It takes 120ms with desc and only 40ms without desc. What could be the solution? I have tried indexing schedule_time with desc through SQL. Does not seem to help. The query is as follows:
@Sinnbeck Is there a way to optimize this query? Actually I have around 50k data in production and a threshold for API endpoint to be loaded within a few milliseconds. And I am not able to achieve that. Currently I am with the query below:
@parash75 did you try adding indexes on the columns you filter by? I suggest seeding around the same amount of data locally so that you can test it out. Personally I tweak the table locally in my database editor and run the query there while testing. Once it's performance is as expected, I write the changes as a migration
@Sinnbeck I have tried indexing. Category and Post have many-to-many relationship. I receive category_id in my endpoint to which I need to respond with 25 posts with given conditions. I have a pivot table for them. I am not sure how to optimize it.
@parash75 those are different queries. Laravel does seperate queries for loading relationships. I assume those few milli seconds are for just a single query?
@Sinnbeck It is 500ms to be precise. If the client does not receive response within 500ms, it skips the endpoint. Similarly, if it needs to skip the endpoint for a few more time, the client automatically blacklists the endpoint.
@parash75 ah ok. So 500ms in total. I would think that should be achievable. But go over each query laravel makes and optimize it as much as possible. I personally use debugbar to get an overview, and then take one query at a time and optimize outside of laravel
Do you have a database index on both order_no and schedule_time?
If I'm not mistaken the index of mysql has an influence and you have two orderBy records. One sorting ASC and one DESC. There is quite some documentation on it at:
However, adding indexes also have a negative impact on insert queries as it takes a bit of time to update indexes after each insert. But it might be worth a try to create an index of the combination order_no + schedule_time in the order you want and see if that helps.
@parash75 Try creating an index INDEX order_no_schedule_time_desc (order_no ASC, schedule_time DESC), it might help. But take into account that each index also has to be stored somewhere and updating it takes a bit of time.
@click Wow I really appreciate it since this has shown visual changes. But how do we index desc through migration in laravel? I use continuous deployment