文章转发自专业的Laravel开发者社区,原始连接:learnku.com/laravel/t/1…php
对象关系映射(ORM)使得处理数据惊人地简单。因为以面向对象的方式定义数据之间关系使得查询关联模型数据变得容易,开发者不太须要关注数据底层调用。html
ORM 的标准数据优化是渴望式加载相关数据。咱们将创建一些示例关系,而后逐步了解查询随着渴望式加载和非渴望式加载变化。我喜欢直接使用代码来试验一些东西,并经过一些示例来讲明渴望式加载是如何工做的,这将进一步帮助你理解如何优化查询。laravel
在基本级别,ORM 是 “懒惰” 加载相关的模型数据。可是,ORM 应该如何知道你的意图?在查询模型后,您可能永远不会真正使用相关模型的数据。不优化查询被称为 “N + 1” 问题。当您使用对象来表示查询时,您可能在不知情的状况下进行查询。web
想象一下,您收到了100个来自数据库的对象,而且每条记录都有1个关联的模型(即belongsTo)。使用ORM默认会产生101条查询; 对原始100条记录 进行一次查询,若是访问了模型对象上的相关数据,则对每条记录进行附加查询。在伪代码中,假设您要列出全部已发布帖子的发布做者。从一组帖子(每一个帖子有一位做者),您能够获得一个做者姓名列表,以下所示:sql
$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 概念很是类似。ruby
# Rails
posts = Post.includes(:author).limit(100)
# Laravel
$posts = Post::with('author')->limit(100)->get();
复制代码
经过从更广阔的视角探索,我发现我得到了更好的理解。Active Record 文档涵盖了一些能够进一步帮助该想法产生共鸣的示例。bash
Laravel 的 ORM,叫做 Eloquent, 能够很轻松的预加载模型,甚至预加载嵌套关联模型。让咱们以Post模型为例,学习如何在 Laravel 项目中使用预先加载。 咱们将使用这个项目构建,而后更深刻地浏览一些预加载示例以进行总结。app
让咱们构建一些 数据库迁移, 模型, 和 数据库种子 来体验预加载。若是你想跟着操做,我假设你有权访问数据库而且能够完成了基本的 Laravel 安装。
使用 Laravel 安装器, 新建项目:
laravel new blog-example
复制代码
根据你的数据库和选择编辑 .env
文件。
接下来,咱们将建立三个模型,以便您能够尝试预加载嵌套关系。这个例子很简单,因此咱们能够专一于预加载,并且我省略了你可能会使用的东西,如索引和外键约束。
php artisan make:model -m Post
php artisan make:model -m Author
php artisan make:model -m Profile
复制代码
该 -m
标志建立一个迁移,以与将用于建立表模式的模型一块儿使用。
数据模型将具备如下关联:
Post -> belongsTo -> Author
Author -> hasMany -> Post
Author -> hasOne -> Profile
复制代码
让咱们为每一个数据表建立一个简表结构。我只添加了 up()
方法,由于 Laravel 将会为新的数据表自动添加 down()
方法。这些迁移文件放在了 database/migrations/
目录中:
<?php
use 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');
}
}
复制代码
<?php
use 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');
}
}
复制代码
<?php
use 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
:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function author()
{
return $this->belongsTo(Author::class);
}
}
复制代码
接下来, app\Author.php
模型有两个关联关系:
<?php
namespace 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 migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrating: 2017_08_04_042509_create_posts_table
Migrated: 2017_08_04_042509_create_posts_table
Migrating: 2017_08_04_042516_create_authors_table
Migrated: 2017_08_04_042516_create_authors_table
Migrating: 2017_08_04_044554_create_profiles_table
Migrated: 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:refresh
php 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();
复制代码
嵌套预加载来作相同的工做。在咱们的例子中,每一个做者的 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()
方法后。
但愿你能了解到更多关于预加载模型的相关知识,而且了解它是如何在更加深刻底层的工做方式。 预加载文档 是很是全面的,我但愿额外的一些代码实现能够帮助您更好的优化关联查询。