A
A
Andrzej Wielski2016-02-22 22:01:13
Laravel
Andrzej Wielski, 2016-02-22 22:01:13

Why are SQL queries so slow?

Hello.
The point is this. There was a code for the Laravel framework, the result of which was a whole bunch of inner joins. Here is the code itself:

$this->product = $this->product->where(function ($query) use ($option, $value) {
            $i = 0;
            foreach($value as $single_value){
              if($i == 0){
                $query->whereHas('options', function($query) use($option, $single_value){
                  $query->where('name', $option)->where('value', $single_value);
                });
              } else {
                $query->orWhereHas('options', function($query) use($option, $single_value){
                  $query->where('name', $option)->where('value', $single_value);
                });
              }
              $i++;
            }
          });

7481e668a236484d9e3e617d5c3adc69.png
Made small changes:
$this->product = $this->product->whereHas('options', function ($query) use ($option, $value) {
            $i = 0;
            foreach($value as $single_value){
              if($i == 0){
                $query->where(function($query) use($option, $single_value){
                  $query->where('name', $option)->where('value', $single_value);
                });
              } else {
                $query->orWhere(function($query) use($option, $single_value){
                  $query->where('name', $option)->where('value', $single_value);
                });
              }
              $i++;
            }
          });

7067a1c940524efb9cd7767792857bfc.png
As a result - less complex code without unnecessary joins. But the speed has slowed down.
Moreover, all other requests (Laravel links and so on) began to work for more than a second (!).
0da194c1f8f24124b8094daabccb662b.png
What is the reason for this behavior?
How to be?
Return Joins? I do not think that this is the best solution, at least the size of the SQL code confuses me.
I wanted faster work - it turned out the opposite.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question