简体   繁体   中英

Laravel DB get() is slow

So i try to get around 5k records, u made this code:

            $mainData = DB::table('table')
            ->where('id', $id)
            ->where('status', '>', 0)
            ->where('amount', '>', 0)
            ->orderBy('timestamp', 'DESC')
            ->where('status', '>=', DB::raw('UNIX_TIMESTAMP((LAST_DAY(DATE_SUB(NOW(), INTERVAL ' . intval($month) . ' MONTH))+INTERVAL 1 DAY)-INTERVAL 1 MONTH)'))
            ->where('status', '<', DB::raw('UNIX_TIMESTAMP(LAST_DAY(DATE_SUB(NOW(), INTERVAL ' . intval($month) . ' MONTH))+INTERVAL 1 DAY)'))
            ->get();

when i made ->toSql() i got this query:

    select * from `table` where `id` = 3 and `status` > 0 and `amount` > 0 and `status` >= UNIX_TIMESTAMP((LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH))+INTERVAL 1 DAY)-INTERVAL 1 MONTH) and `status` < UNIX_TIMESTAMP(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH))+INTERVAL 1 DAY) order by `timestamp` desc

when i executed this query in phpmyadmin: Showing rows 0 - 24 (5980 total, Query took 0.0513 seconds.) but when i run this query in laravel it took around 6 seconds(for only 6k records) i think problem is can be fact that laravel create new object per record is any way to speedup this?

You could try and optimise your code by re-writing more of it into raw queries or rewriting the entire query into raw SQL. Like you mentioned, Laravel is having to create thousands of objects and this really slows the process down which is what you're experiencing.

Take a look at Raw Expressions on the Laravel docs to read more about this.

Basic example from docs:

$users = DB::table('users')
                     ->select(DB::raw('count(*) as user_count, status'))
                     ->where('status', '<>', 1)
                     ->groupBy('status')
                     ->get();

Maybe also trim down the amount of data that is being returned by supplying an array of fields to ->get() . You can take a look at the API for doing that on the Laravel Eloquent Builder API Documentation .

It is fast in phpMyAdmin because it returns paged results and not creating any php objects, as you have pointed.

Instead of using get() use paginate() for paged results or to get all records with a single field use pluck(), eg pluck('name', 'id'), which will return an associative array keyed by id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM