anttton123's avatar

Optimization of my Product::where eloquent

Hello!

I'm seeking assistance regarding a large database containing approximately 2 million products. Each product has unique_id, name, product_type, and other attributes. I have attempted to implement a search function for these products.

Here's the code I'm currently using:

 Product::where('name', 'like', '%' . $search . '%')->orderBy($sort, $sortDirection)->simplePaginate(20);

However, I've noticed that when using the orderBy clause, the search time increases by approximately 80%, which significantly affects the performance. I've come across suggestions recommending the indexing of certain columns to optimize and improve search speed. Could you please explain how indexing works and which columns should be indexed?

I've observed that when searching for a specific term that I know only one product has, the search time increases dramatically. On the other hand, if I search for a well-known term that many products contain, the search speed is considerably faster, and the response time is reduced.

I would greatly appreciate any suggestions or feedback on how to implement a more efficient search function. Thank you in advance!

0 likes
4 replies
tykus's avatar

how indexing works

The database builds a search tree to optimise search performance https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html

which columns should be indexed

Whichever columns, or combinations of columns you intend to be searchable, and how you intend to search them. Searching for records where column has exact value) is relatively easily solved with a simple index; whereas LIKE comparisons are not. A Full Text Index and Search could be considerably faster in such circumstances

anttton123's avatar

@tykus Thank you for your quick response! Yes the thing is that I want a better optimized way to search for terms in names. For example in "Summer" search for 'me' and that matches with "Summer". What would you think about chunks? Search chunk by chunk. Haven't tried this yet!

Kind regards!

anttton123's avatar

@Snapey Hey! Thank you for your feedback, will checkout the links you provided, a search engine probably is what i'm looking for!

Please or to participate in this conversation.