5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

laravel 5.7 mysqlチューニング インデックスを貼る

Posted at

サイト作成後に、テストデータをてんこ盛り入れてすぐにチューニングしてもいいんじゃないかと思う。あとでやろうとすると面倒なので。

DBクエリ改善は地道な道。楽な道がない。

根気よくやっていきましょう。

use DB;を忘れずに。

HogeController.php

use DB;

class HogeController extends Controller
{
    public static function test(request $request)
    {
        DB::enableQueryLog();//SQLログ記録開始
        $res = Contactsub::query()
            ->where('voice_flag',0)
            ->where('to_id',26112)
            ->orderBy('created_at', 'DESC')
            ->get();//全て取得
            
      dd(DB::getQueryLog());//SQLログを表示
        die;
    }


結果


[query] => select * from `contactsubs` where `voice_flag` = ? and `to_id` = ? order by `created_at` desc
[bindings] => Array
                (
                    [0] => 0
                    [1] => 26112
                )

[time] => 32.65
            

32.65 かかっていますね。
上記の query を EXPLAINをしてみます。


//        EXPLAINをつけた query , bindings の データ
$data = DB::select("EXPLAIN select * from `contactsubs` where `voice_flag` = ? and `to_id` = ? order by `created_at` desc",[0]);
dd($data);


結果


    [0] => stdClass Object
        (
            [id] => 1
            [select_type] => SIMPLE
            [table] => contactsubs
            [partitions] => 
            [type] => ALL
            [possible_keys] => 
            [key] => 
            [key_len] => 
            [ref] => 
            [rows] => 27240
            [filtered] => 1.0000001192093
            [Extra] => Using where; Using filesort
        )


・[key] が 空
・Extra に filesort がある

ということで、インデックスを貼ります。

今回は
・voice_flag
・to_id
・created

の 3つを使うので、複合インデックスを貼ります。


//インデックスを追加
DB::statement('ALTER TABLE contactsubs ADD INDEX voice_to_created(voice_flag,to_id,created_at)');


//インデックスを削除
//DB::statement('ALTER TABLE contactsubs DROP INDEX voice_to_created');

インデックス追加後に再度 EXPLAINを 実行。


    [0] => stdClass Object
        (
            [id] => 1
            [select_type] => SIMPLE
            [table] => contactsubs
            [partitions] => 
            [type] => ref
            [possible_keys] => voice_to_created
            [key] => voice_to_created
            [key_len] => 12
            [ref] => const,const
            [rows] => 16
            [filtered] => 100
            [Extra] => Using where
        )



お。key に voice_to_created が入りましたね。
では速度もチェック。


    [0] => Array
        (
            [query] => select * from `contactsubs` where `voice_flag` = ? and `to_id` = ? order by `created_at` desc
            [bindings] => Array
                (
                    [0] => 0
                    [1] => 26112
                )

            [time] => 17.84
        )


17.84 まで落ちましたね。

インデックスを貼るコツは、AとBをwhere して C で並び替える。という場合は、全てに複合インデックスを貼るってのがコツです。

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?