D
D
Dmi3ii2018-08-21 16:52:51
Laravel
Dmi3ii, 2018-08-21 16:52:51

How to optimize queries in Laravel or a simple query in 1.5 seconds is it normal?

Good afternoon, bright heads.
In adminer, a simple query to a table with 19 rows takes 0.003s, in Laravel 1.54s - is that normal? I understand that you have to pay for convenience, but 1000 times?! Something I do not understand.
Win 8.1 WAMP

SELECT * FROM `events` WHERE `key_id` = '89'
--4 строки (0.003 s)

namespace App\Http\Controllers;

use App\Event;
use App\Http\Resources\Event as EventResource;
use App\Key;
use Illuminate\Http\Request;

class EventsController extends Controller
{
    public function index($key_id)
    {
        $events = Event::where('key_id', $key_id)->orderBy('date', 'desc')->get();
        return EventResource::collection($events);
    }
<...>
}

// 1.5 - 2 sec

Insomnia GET laravel/api
* Preparing request to http://api-laravel.local/api/events/89
* Using libcurl/7.51.0-DEV OpenSSL/1.0.2j zlib/1.2.8 libssh2/1.6.1_DEV
* Enable automatic URL encoding
* Enable SSL validation
* Enable cookie sending with jar of 2 cookies
* Connection 6 seems to be dead!
* Closing connection 6
* Hostname api-laravel.local was found in DNS cache
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to api-laravel.local (127.0.0.1) port 80 (#7)
> GET /api/events/89 HTTP/1.1
> Host: api-laravel.local
> User-Agent: insomnia/5.16.6
> Accept: */*
< HTTP/1.1 200 OK
< Date: Tue, 21 Aug 2018 13:44:17 GMT
< Server: Apache/2.4.27 (Win64) PHP/7.1.9
< X-Powered-By: PHP/7.1.9
< X-Xdebug-Profile-Filename: d:\wamp\bin\php\php7.1.9\zend_ext\cachegrind.out.60236
< Cache-Control: no-cache, private
< X-RateLimit-Limit: 60
< X-RateLimit-Remaining: 57
< Content-Length: 1206
< Content-Type: application/json

* Received 1206 B chunk
* Curl_http_done: called premature == 0
* Connection #7 to host api-laravel.local left intact

Answer the question

In order to leave comments, you need to log in

7 answer(s)
A
Alexander Aksentiev, 2018-08-21
@Sanasol

d:\wamp\bin\php\php7.1.9\zend_ext\cachegrind.out.60236
Well, open the report and see what exactly time eats. Obviously not a request.
Although on Windows there may be a connection in the database, depending on how the host 127.0.0.1 or localhost is registered
, but there it is usually stupid, not 1.5 seconds.
or so
https://github.com/barryvdh/laravel-debugbar

K
Konstantin B., 2018-08-21
@Kostik_1993

Welcome to the adult world. Set yourself at least Homestead and then throw your accusations at Laravel. It's all about Windows, it's all about PHP for Windows, and plus, your file processing speed can be monstrous. You can have your WAMP set up so dumb that it and Hello World will display for a second. If you
want to write good applications, write them on Linux

D
devlocal, 2018-08-22
@devlocal

Need to use linux

K
Kekoc, 2018-08-23
@Kekoc

Do you host projects on windows too?

D
DevMan, 2015-02-12
@Rattlesneyk

Because $t- this is an object, and instead $t['category']you need to use $t->category
It's elementary, and the error is extremely clear what the problem is.

E
Evgeny Kalibrov, 2015-02-12
@rework

If you do this:
then your code should work correctly, since the second argument of the json_decode function is responsible for the fact that the passed json will be converted into an associative array, and not into an object.

K
killerdr, 2019-02-19
@killerdr

$items = json_decode(json_encode($items),true);
and the problem will be solved.
I'll leave it here for future seekers of a solution to such a misunderstanding.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question