您的位置 首页 php

Laravel 的 N+1 问题解决方法

对象关系映射(ORM)使得处理数据惊人地简单。由于以面向对象的方式定义数据之间关系使得查询关联模型数据变得容易,开发者不太需要关注数据底层调用。

ORM 的标准数据优化是渴望式加载相关数据。我们将建立一些示例关系,然后逐步了解查询随着渴望式加载和非渴望式加载变化。我喜欢直接使用代码来试验一些东西,并通过一些示例来说明渴望式加载是如何工作的,这将进一步帮助你理解如何优化查询。

介绍

在基本级别,ORM 是 “懒惰” 加载相关的模型数据。但是,ORM 应该如何知道你的意图?在查询模型后,您可能永远不会真正使用相关模型的数据。不优化查询被称为 “N + 1” 问题。当您使用对象来表示查询时,您可能在不知情的情况下进行查询。

想象一下,您收到了 100 个来自数据库的对象,并且每条记录都有 1 个关联的模型(即 belongsTo)。使用 ORM 默认会产生 101 条查询;对原始 100 条记录 进行一次查询,如果访问了模型对象上的相关数据,则对每条记录进行附加查询。在伪代码中,假设您要列出所有已发布帖子的发布作者。从一组帖子(每个帖子有一位作者),您可以得到一个作者姓名列表,如下所示:

$posts = Post::published()->get(); // 一次查询$authors = array_map(function($post) {    // 生成对作者模型的查询    return $post->author->name;}, $posts);

我们并没有告诉模型我们需要所有作者,因此每次从各个 Post 模型实例中获取作者姓名时都会发生单独的查询 。

预加载

正如我所提到的,ORM 是 "懒惰" 加载关联。如果您打算使用关联的模型数据,则可以使用预加载将 101 次查询缩减为 2 次查询。您只需要告诉模型你渴望它加载什么。

以下是使用预加载的 Rails Active Record guide 中的示例.。正如您所看到的,这个概念与 Laravel's eager loading 概念非常相似。

# Railsposts = Post.includes(:author).limit(100)# Laravel$posts = Post::with('author')->limit(100)->get();

通过从更广阔的视角探索,我发现我获得了更好的理解。Active Record 文档涵盖了一些可以进一步帮助该想法产生共鸣的示例。

Laravel 的 Eloquent ORM

Laravel 的 ORM,叫作 Eloquent, 可以很轻松的预加载模型,甚至预加载嵌套关联模型。让我们以 Post 模型为例,学习如何在 Laravel 项目中使用预先加载。

我们将使用这个项目构建,然后更深入地浏览一些预加载示例以进行总结。

构建

让我们构建一些 数据库迁移, 模型, 和 数据库种子 来体验预加载。如果你想跟着操作,我假设你有权访问数据库并且可以完成了基本的 Laravel 安装。

使用 Laravel 安装器,新建项目:

laravel new blog-example

根据你的数据库和选择编辑 .env 文件。

接下来,我们将创建三个模型,以便您可以尝试预加载嵌套关系。这个例子很简单,所以我们可以专注于预加载,而且我省略了你可能会使用的东西,如索引和外键约束。

php artisan make:model -m Postphp artisan make:model -m Authorphp artisan make:model -m Profile

该 -m 标志创建一个迁移,以与将用于创建表模式的模型一起使用。

数据模型将具有以下关联:

Post -> belongsTo -> Author

Author -> hasMany -> Post

Author -> hasOne -> Profile

迁移

让我们为每个数据表创建一个简表结构。我只添加了 up() 方法,因为 Laravel 将会为新的数据表自动添加 down() 方法。这些迁移文件放在了 database/migrations/ 目录中:

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreatePostsTable extends Migration{    /**     * 执行迁移     *     * @return void     */    public function up()    {        Schema::create('posts', function (Blueprint $table) {            $table->increments('id');            $table->unsignedInteger('author_id');            $table->string('title');            $table->text('body');            $table->timestamps();        });    }    /**     * 回滚迁移     *     * @return void     */    public function down()    {        Schema::dropIfExists('posts');    }}
<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateAuthorsTable extends Migration{    /**     * 执行迁移     *     * @return void     */    public function up()    {        Schema::create('authors', function (Blueprint $table) {            $table->increments('id');            $table->string('name');            $table->text('bio');            $table->timestamps();        });    }    /**     * 回滚迁移     *     * @return void     */    public function down()    {        Schema::dropIfExists('authors');    }}
<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateProfilesTable extends Migration{    /**     * 执行迁移     *     * @return void     */    public function up()    {        Schema::create('profiles', function (Blueprint $table) {            $table->increments('id');            $table->unsignedInteger('author_id');            $table->date('birthday');            $table->string('city');            $table->string('state');            $table->string('website');            $table->timestamps();        });    }    /**     * 回滚迁移     *     * @return void     */    public function down()    {        Schema::dropIfExists('profiles');    }}

模型

你需要定义模型关联并通过预先加载来进行更多的实验。当你运行 php artisan make:model 命令的时候,它将为你创建模型文件。

第一个模型为 app/Post.php :

<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Post extends Model{    public function author()    {        return $this->belongsTo(Author::class);    }}

接下来, app\Author.php 模型有两个关联关系:

<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Author extends Model{    public function profile()    {        return $this->hasOne(Profile::class);    }    public function posts()    {        return $this->hasMany(Post::class);    }}

通过模型和迁移,你可以运行迁移并继续尝试使用一些种子模型数据进行预加载。

php artisan migrateMigration table created successfully.Migrating: 2014_10_12_000000_create_users_tableMigrated:  2014_10_12_000000_create_users_tableMigrating: 2014_10_12_100000_create_password_resets_tableMigrated:  2014_10_12_100000_create_password_resets_tableMigrating: 2017_08_04_042509_create_posts_tableMigrated:  2017_08_04_042509_create_posts_tableMigrating: 2017_08_04_042516_create_authors_tableMigrated:  2017_08_04_042516_create_authors_tableMigrating: 2017_08_04_044554_create_profiles_tableMigrated:  2017_08_04_044554_create_profiles_table

如果你查看下数据库,你就会看到所有已经创建好的数据表!

工厂模型

为了让我们可以运行查询语句,我们需要创建一些假数据来提供查询,让我们添加一些工厂模型,使用这些模型来为数据库提供测试数据。

打开 database/factories/ModelFactory.php 文件并且将如下三个工厂模型添加到现有的 User 工厂模型文件中:

/** @var \Illuminate\Database\Eloquent\Factory $factory */$factory->define(App\Post::class, function (Faker\Generator $faker) {    return [        'title' => $faker->sentence,        'author_id' => function () {            return factory(App\Author::class)->create()->id;        },        'body' => $faker->paragraphs(rand(3,10), true),    ];});/** @var \Illuminate\Database\Eloquent\Factory $factory */$factory->define(App\Author::class, function (Faker\Generator $faker) {    return [        'name' => $faker->name,        'bio' => $faker->paragraph,    ];});$factory->define(App\Profile::class, function (Faker\Generator $faker) {    return [        'birthday' => $faker->dateTimeBetween('-100 years', '-18 years'),        'author_id' => function () {            return factory(App\Author::class)->create()->id;        },        'city' => $faker->city,        'state' => $faker->state,        'website' => $faker->domainName,    ];});

这些工厂模型可以很容易的填充一些我们可以用来查询的数据;我们也可以使用它们来创建并生成关联模型所需的数据。

打开 database/seeds/DatabaseSeeder.php 文件将以下内容添加到 DatabaseSeeder::run() 方法中:

public function run(){    $authors = factory(App\Author::class, 5)->create();    $authors->each(function ($author) {        $author            ->profile()            ->save(factory(App\Profile::class)->make());        $author            ->posts()            ->saveMany(                factory(App\Post::class, rand(20,30))->make()            );    });}

你创建了五个 author 并遍历循环每一个 author ,创建和保存了每个 author 相关联的 profile 和 posts (每个 author 的 posts 的数量在 20 和 30 个之间)。

我们已经完成了迁移、模型、工厂模型和数据库填充的创建工作,将它们组合起来可以以重复的方式重新运行迁移和数据库填充:

php artisan migrate:refreshphp artisan db:seed

你现在应该有一些已经填充的数据,可以在下一章节使用它们。注意在 Laravel 5.5 版本中包含一个 migrate:fresh 命令,它会删除表,而不是回滚迁移并重新应用它们。

尝试使用预加载

现在我们的前期工作终于已经完成了。 我个人认为最好的可视化方式就是将查询结果记录到 storage/logs/laravel.log 文件当中查看。

要把查询结果记录到日志中,有两种方式。第一种,可以开启 MySQL 的日志文件,第二种,则是使用 Eloquent 的数据库调用来实现。通过 Eloquent 来实现记录查询语句的话,可以将下面的代码添加到 app/Providers/AppServiceProvider.php boot () 方法当中:

namespace App\Providers;use DB;use Log;use Illuminate\Support\ServiceProvider;class AppServiceProvider extends ServiceProvider{    /**     * Bootstrap any application services.     *     * @return void     */    public function boot()    {        DB::listen(function($query) {            Log::info(                $query->sql,                $query->bindings,                $query->time            );        });    }    // ...}

我喜欢把这个监听器封装在配置检查的时候,以便可以控制记录查询日志的开关。你也可以从 Laravel Debugbar 获取到更多相关的信息。

首先,尝试一下在不使用预加载模型的时候,会发生什么情况。清除你的 storage/log/laravel.log 文件当中的内容然后运行 "tinker" 命令:

php artisan tinker>>> $posts = App\Post::all();>>> $posts->map(function ($post) {...     return $post->author;... });>>> ...

这个时候检查你的 laravel.log 文件,你会发现一堆查询作者的查询语句:

[2017-08-04 06:21:58] local.INFO: select * from `posts`[2017-08-04 06:22:06] local.INFO: select * from `authors` where `authors`.`id` = ? limit 1 [1][2017-08-04 06:22:06] local.INFO: select * from `authors` where `authors`.`id` = ? limit 1 [1][2017-08-04 06:22:06] local.INFO: select * from `authors` where `authors`.`id` = ? limit 1 [1]....

然后,再次清空 laravel.log 文件,, 这次使用 with() 方法来用预加载查询作者信息:

php artisan tinker>>> $posts = App\Post::with('author')->get();>>> $posts->map(function ($post) {...     return $post->author;... });...

这次你应该看到了,只有两条查询语句。一条是对所有帖子进行查询,以及对帖子所关联的作者进行查询:

[2017-08-04 07:18:02] local.INFO: select * from `posts`[2017-08-04 07:18:02] local.INFO: select * from `authors` where `authors`.`id` in (?, ?, ?, ?, ?) [1,2,3,4,5]

如果你有多个关联的模型,你可以使用一个数组进行预加载的实现:

$posts = App\Post::with(['author', 'comments'])->get();

在 Eloquent 中嵌套预加载

嵌套预加载来做相同的工作。在我们的例子中,每个作者的 model 都有一个关联的个人简介。因此,我们将针对每个个人简介来进行查询。

清空 laravel.log 文件,来做一次尝试:

php artisan tinker>>> $posts = App\Post::with('author')->get();>>> $posts->map(function ($post) {...     return $post->author->profile;... });...

你现在可以看到七个查询语句,前两个是预加载的结果。然后,我们每次获取一个新的个人简介时,就需要来查询所有作者的个人简介。

通过预加载,我们可以避免嵌套在模型关联中的额外的查询。最后一次清空 laravel.log 文件并运行一下命令:

>>> $posts = App\Post::with('author.profile')->get();>>> $posts->map(function ($post) {...     return $post->author->profile;... });

现在,总共有三个查询语句:

[2017-08-04 07:27:27] local.INFO: select * from `posts`[2017-08-04 07:27:27] local.INFO: select * from `authors` where `authors`.`id` in (?, ?, ?, ?, ?) [1,2,3,4,5][2017-08-04 07:27:27] local.INFO: select * from `profiles` where `profiles`.`author_id` in (?, ?, ?, ?, ?) [1,2,3,4,5]

懒人预加载

你可能只需要收集关联模型的一些基础的条件。在这种情况下,可以懒惰地调用关联数据的一些其他查询:

php artisan tinker>>> $posts = App\Post::all();...>>> $posts->load('author.profile');>>> $posts->first()->author->profile;...

你应该只能看到三条查询,并且是在调用 $posts->load() 方法后。

总结

希望你能了解到更多关于预加载模型的相关知识,并且了解它是如何在更加深入底层的工作方式。 预加载文档 是非常全面的,我希望额外的一些代码实现可以帮助您更好的优化关联查询。

推荐教程:《Laravel教程》《PHP教程》

文章来源:智云一二三科技

文章标题:Laravel 的 N+1 问题解决方法

文章地址:https://www.zhihuclub.com/27593.shtml

关于作者: 智云科技

热门文章

网站地图