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

dylanryan's avatar

Query gets extremely slow

    private function getArticles()
    {
        $query = Article::orderBy($this->sort, $this->sortOrder);
        if ($this->searchTerm != '') {
            $query->where('blog_url', 'LIKE', '%' . $this->searchTerm . '%');
        }
        // $query->whereHas('categories', function($query) {
        //     $query->whereIn('category_id', [8]);
        // });
        $query->where('language', 'LIKE', '%' . $this->filterLanguage . '%');
        $query->where('tld', 'LIKE', '%' . $this->filterTld . '%');
        if ($this->priceFrom > 0 or $this->priceTo > 0) {
            $query->whereBetween('price_sale', [$this->priceFrom, $this->priceTo]);
        }
        if ($this->trafficFrom > 0 or $this->trafficTo > 0) {
            if($this->trafficTo == 200000) {
                $this->trafficTo = 99999999;
            }
            $query->whereBetween('traffic', [$this->trafficFrom, $this->trafficTo]);
        }

        return $query->paginate(15);
    }

Hey guys, i have a question, so this query runs perfectly fine and pretty fast, but if i run the query with the code which is currently commented out, it is extremely slow, is there something i can do about this?

0 likes
10 replies
mstrauss's avatar

Hi @dylanryan

Maybe try this:

 $query->whereHas('categories', function($query) {
             $query->whereIn('id', [8]);
         });

As the sub query is referencing the category not the Article. Of course , if you only are retrieving categories within one category you are probably best to use a simple where clause like below:

$query->whereHas('categories', function($query) { $query->where('id', 8); });

1 like
dylanryan's avatar

this was just an example, most of the time there will be more than one id, thats why i used whereIn

but even with a simple where clause it is extremely slow.

Tray2's avatar

You are using LIKE and that is always slow since it will require full table scans. Use = where possible.

dylanryan's avatar

Thats actually not the problem, everything is loading in 500ms with the like statements.

but if i uncomment this

    $query->whereHas('categories', function($query) {
            $query->whereIn('category_id', [8]);
        });

the whole page is loading in 5s, so 10times slower

dylanryan's avatar

the table looks like that:

  • id
  • article_id
  • category_id
  • created_at
  • updated at

i guess by index you mean the id?

dylanryan's avatar

the categories table has 10.000 entries, i think thats why it is so slow.. but is there anything i can do to make it faster?

Tray2's avatar
Tray2
Best Answer
Level 73

Add an index to the table on category_id

1 like
Tray2's avatar

Lets say you have a list of id like this

1
2
3
4
5
6
7
8
9
10

and you are looking for id 10, It will start at the top and check all the ids till it reaches the matching one. An index looks something like this

1-5                      6-10
  1-3    4-5           6-8  9-10

So it will first check if the 10 is within the first 1-5 group it's not so it will choose the 6-10 group and then it will check if it's within the 6-8 group since it's not it will choose the 9-10 group and find the record.

So instead of doing ten comparsions it does three or four. This wont matter in a small table but if you have thousands or hundreds of thousands of rows it makes a huge difference.

1 like

Please or to participate in this conversation.