Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Xanger's avatar

Query slow loading

I have a query that selects similar articles based on the hashtag of the article.

$similar_art = App\Models\Article::where('is_published', '0')
    ->where('id', '!=', $article->id)
    ->where('tag', 'LIKE', '%'.$value.'%')
    ->inRandomOrder()
    ->limit(5)
    ->get();

However, this query slows down the page loading so much, how can I improve it?

0 likes
5 replies
Snapey's avatar

seems simple enough. How many records are there?

Xanger's avatar

@Snapey you say in Article? in total about 10k, using the filters a thousand. debugbar also colors it red with 20.36ms...

cwhite's avatar

debugbar also colors it red with 20.36ms...

  • Lol that's nothing, wait until you have a query that runs into the PHP max execution time of 30s
  • Debugbar just colors and draws the query proportional to all of the other queries
  • The LIKE %value% statement is going to slow the query down since it probably can't use an index. You can try using value%, or try creating a full-text index on that column (but then you're trading speed for more space).
sureshramani's avatar

Select only the columns which you need to show on frontend. For Example: If you need to display name, image and short_description then select this 3 columns from the table. Instead of selecting all columns from the table.

Please or to participate in this conversation.