4
laravel-soar -An extension package for optimizing sql statements easily and easily in Laravel applications.

score.png

Features

  • Support sentence optimization based on heuristic algorithm
  • Support multi-column index optimization for complex queries (UPDATE, INSERT, DELETE, SELECT)
  • Support EXPLAIN rich interpretation of information
  • Support SQL fingerprint, compression and beautification
  • Support Eloquent query builder method to generate SQL optimization report

Source link

Related items

Environmental requirements

  • laravel >= 5.5

installation

$ composer require guanguans/laravel-soar --dev -vvv

Publishing service

$ php artisan vendor:publish --provider="Guanguans\\LaravelSoar\\SoarServiceProvider"

use

Generate sql scoring report example

use App\Models\Member;
    
Member::query()
    ->select([
        'id',
        'nickname',
    ])
    ->where('id', 100)
    // ->toSoarScore()
    // ->dumpSoarScore()
    ->ddSoarScore()
;

high-score.png

// 查询构建器使用示例
DB::table('yb_member')
    ->select('*')
    ->join('yb_member_account as yb_member_account', 'yb_member_account.member_id', '=', 'yb_member.id')
    ->whereRaw('1 <> 1')
    ->where('yb_member.nickname', 'like', 'admin')
    ->where('yb_member.username', 'like', '%admin%')
    ->whereRaw("substring(yb_member.username, 1, 5) = 'admin'")
    ->whereIn('yb_member.id', [110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120])
    ->orWhereNotNull('yb_member.realname')
    ->groupByRaw("yb_member.status, '100'")
    ->having('yb_member.id', '>', '100')
    ->inRandomOrder()
    // ->toSoarScore()   // 生成 sql 评分报告
    // ->dumpSoarScore() // 打印 sql 评分报告
    ->ddSoarScore()      // 打印 sql 评分报告,并且退出应用程序。
;

low-score.png

Example of generating an explain information interpretation report

// 查询构建器使用示例
DB::table('yb_member')
    ->select('*')
    ->join('yb_member_account as yb_member_account', 'yb_member_account.member_id', '=', 'yb_member.id')
    ->whereRaw('1 <> 1')
    ->where('yb_member.nickname', 'like', 'admin')
    ->where('yb_member.username', 'like', '%admin%')
    ->whereRaw("substring(yb_member.username, 1, 5) = 'admin'")
    ->whereIn('yb_member.id', [110, 120])
    ->orWhereNotNull('yb_member.realname')
    ->groupByRaw("yb_member.status, '100'")
    ->having('yb_member.id', '>', '100')
    ->inRandomOrder()
    // ->toSoarHtmlExplain()   // 生成 explain 信息解读报告
    // ->dumpSoarHtmlExplain() // 打印 explain 信息解读报告
    ->ddSoarHtmlExplain()      // 打印 explain 信息解读报告,并且退出应用程序。
;

explain.png

Beautify sql statement

// 查询构建器使用示例
DB::table('yb_member')
    ->select('*')
    ->join('yb_member_account as yb_member_account', 'yb_member_account.member_id', '=', 'yb_member.id')
    ->whereRaw('1 <> 1')
    ->where('yb_member.nickname', 'like', 'admin')
    ->where('yb_member.username', 'like', '%admin%')
    ->whereRaw("substring(yb_member.username, 1, 5) = 'admin'")
    ->whereIn('yb_member.id', [110, 120])
    ->orWhereNotNull('yb_member.realname')
    ->groupByRaw("yb_member.status, '100'")
    ->having('yb_member.id', '>', '100')
    ->inRandomOrder()
    // ->toSoarPretty()   // 生成美化后的 sql
    // ->dumpSoarPretty() // 打印美化后的 sql
    ->dumpSoarPretty()    // 打印美化后的 sql,并且退出应用程序。
;

pretty.png

Other usage examples

\Soar::score($sql);        // 生成 sql 评分报告
\Soar::mdExplain($sql);    // 生成 markdown 格式的 explain 信息解读报告
\Soar::htmlExplain($sql);  // 生成 html 格式的 Explain 信息解读报告
\Soar::syntaxCheck($sql);  // sql 语法检查
\Soar::fingerPrint($sql);  // 生成 sql 指纹
\Soar::pretty($sql);       // 美化 sql
\Soar::md2html($sql);      // 将 markdown 格式内容转化为 html 格式内容
\Soar::help($sql);         // 输出 soar 帮助命令内容
\Soar::exec($command);     // 执行任意 soar 命令

guanguans
2.4k 声望89 粉丝

No practice, no gain in one's wit.