Laravel 慢查询

Squ*_*gs. 14 mysql performance laravel

public function delete( ReportDetailRequest $request )
    {
        $id = (int)$request->id;
        $customerRecord = CustomerInfo::find($id);
        $customerRecord->delete();
    }
Run Code Online (Sandbox Code Playgroud)

我目前在 Laravel 应用程序中有上述内容,其中将 DELETE 请求发送到该控制器。目前,正如您所看到的,它非常简单,但查询似乎非常慢。它在 2.23 秒内返回邮递员。我应该怎么做才能加快速度?据我所知,数据库层 (mysql) 确实有一个关于 ID 的索引,并且应用程序没有在调试中运行。这是典型的吗?

编辑:很好地认为请求验证可能正在做某事(它正在验证该用户是否有权删除)。

class ReportDetailRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $id = (int)$this->route('id');
        $customerInfo = CustomerInfo::find($id)->first();
        $company = $customerInfo->company_id;
        return (auth()->user()->company->id  == $company );
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

显示创建表:

CREATE TABLE "customer_info" (
  "id" int(11) NOT NULL AUTO_INCREMENT,
  "user_id" int(11) DEFAULT NULL,
  "report_guid" varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  "customer_email" varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "created_at" timestamp NULL DEFAULT NULL,
  "updated_at" timestamp NULL DEFAULT NULL,
  "report_read" tinyint(1) NOT NULL,
  "customer_name" varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "customer_support_issue" longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  "company_id" int(11) NOT NULL,
  "archived" tinyint(1) NOT NULL,
  "archived_at" timestamp NULL DEFAULT NULL,
  "report_active" tinyint(4) DEFAULT NULL,
  "customer_screenshot" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "video_url" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY ("id"),
  KEY "indexReportLookup" ("report_guid"),
  KEY "guid" ("report_guid"),
  KEY "customer_info_id_index" ("id")
)
Run Code Online (Sandbox Code Playgroud)

基线:

 public function delete( Request $request )
    {
        // $id = (int)$request->id;
        // $customerRecord = CustomerInfo::find($id);
        // $foo_sql = $customerRecord->delete()->toSql();
        // echo($foo_sql);
        return 'test';
        //$customerRecord->delete();
    }
Run Code Online (Sandbox Code Playgroud)

测试响应。

好的,一张全新的桌子,带有全新的请求。里面只有一个 ID,看起来像这样:

在此处输入图片说明

控制器看起来像:

public function deleteTest( Request $request )
    {
        $id = (int)$request->id;
        $customerRecord = NewTable::where('id', '=', $id)->first();
        $customerRecord->delete();
        return response(null, 200);
    }
Run Code Online (Sandbox Code Playgroud)

邮递员版本是:版本 7.27.1 (7.27.1)

1630 毫秒。跆拳道。对新表的简单请求需要 1.6 秒。

说明删除:

1   DELETE  new_tables      range   PRIMARY PRIMARY 8   const   1   100 Using where
Run Code Online (Sandbox Code Playgroud)

解释选择

1   SIMPLE  new_tables      const   PRIMARY PRIMARY 8   const   1   100 Using index
Run Code Online (Sandbox Code Playgroud)

MYSQL 8.0.18 版 innodb_version 8.0.18


所以现在来增加乐趣。

一个无框架的 PHP 文件。简单的 GET 请求。100 毫秒。

<?php
echo('tester');
?>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

编辑。只是重申一下。

Laravel GET 方法(带身份验证)返回测试,返回 1.6 秒。

一个无框架的“sample.php”文件在 100 毫秒内返回。

Laravel GET 方法(无身份验证)返回测试,430 毫秒后返回。

Laravel GET 方法(无需身份验证但具有数据库访问权限)在 1483 毫秒内返回。

一旦应用程序开始使用数据库,看起来确实有一些东西阻止了请求。

Route::middleware('auth:api')->get('/test1','Api\CustomerInfoController@deleteTest')->name('report.deleteTest1.api');
Route::middleware('auth:api')->get('/test2','Api\NewTableController@index')->name('report.deleteTest2.api');

Route::get('/test3','Api\CustomerInfoController@deleteTest')->name('report.deleteTest3.api');
Route::get('/test4','Api\NewTableController@index')->name('report.deleteTest4.api');
 Route::get('/test5','Api\NewTableController@dbTest')->name('report.deleteTest5.api');
Run Code Online (Sandbox Code Playgroud)

新表控制器:

<?php

namespace App\Http\Controllers\Api;

class NewTableController extends Controller
{

    
    public function index()
    {
        return "test2";
    }



}
Run Code Online (Sandbox Code Playgroud)

CustomerInfoController(删除了一些东西,但方法在概念上与 NewTableController 非常相似,尽管正在进行一些依赖注入)。

<?php

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\ReportDetailRequest;
use App\Services\CustomerInfoService;
use Auth;
use App\LookupParent;
use App\LookupChild;
use App\CustomerInfo;
use App\Http\Resources\CustomerInfoResourceCollection;
use App\Http\Resources\CustomerInfoResource;
use App\Http\Resources\CustomerInfoResourceDetail;
use Carbon\Carbon;
use App\NewTable;

class CustomerInfoController extends Controller
{
    protected $customerInfoService;

    public function __construct(
        CustomerInfoService $customerInfoService
        )
    {
        $this->customerInfoService = $customerInfoService;
    }


    public function deleteTest()
    {
        return 'deleteTest';
    }


   public function dbTest()
   {   
     tap(NewTable::find(1))->delete();
   }


}
Run Code Online (Sandbox Code Playgroud)

结果:

/test1 (with authentication 1380ms)
/test2 (with authentication 1320ms)
/test3 (without authentication 112ms)
/test4 (without authentication 124ms)
/test5 (db without authentication 1483ms)
Run Code Online (Sandbox Code Playgroud)

换句话说,身份验证与数据库对话,就像一个简单的删除查询一样,无需身份验证。这些至少需要一秒钟才能完成。这导致了上面提到的大约两秒钟的请求,其中包含两个元素(身份验证和数据库访问)。

编辑。对于那些从谷歌阅读的人。问题与 Digital Ocean 提供的托管数据库有关。在同一台机器上在 MySQL 上设置本地化数据库,问题自行解决。认为这是来自世界各地的 Web 服务器和数据库之间的数据中心的延迟,或者是 DigitalOcean 的数据库管理员配置错误。自己解决了,问题不在于 Laravel。

mak*_*i10 0

Mysql 8 在启动时引入了二进制日志记录。要在 Mysql 8 中禁用二进制日志记录,您需要使用 启动 MySQL 服务器--disable-log-bin。据我所知,禁用上面的这个功能,你的速度至少会提高 10%。

如果您需要更多解释,请访问此线程https://dba.stackexchange.com/a/216624