我在 PostgreSQL 中有一张护照表。它有大约 200 条记录。
在 Laravel 路由中,我创建了一条测试路由并尝试执行此查询:
Route::get('test', function () {
\DB::enableQueryLog();
\DB::statement("
insert into passports (type, number_and_series, expiration_date, issued_by, person_id, updated_at, created_at) values ('russian', '123123', null, '123123', 128, '2021-04-12 14:23:43', '2021-04-12 14:23:43') returning 'id';");
return \DB::getQueryLog();
});
我得到的结果:
[{"query":"insert into passports (type, number_and_series, expiration_date, issued_by, person_id, updated_at, created_at) values ('russian', '123123', null, '123123', 128, '2021-04-12 14:23:43', '2021-04-12 14:23:43') returning 'id';","bindings":[],"time":1864.22}]
注意,时间是 1864 毫秒,差不多 2 秒。
当我运行时explain analyze,我得到了这个:
执行时间:0.858 ms
为什么会发生?起初,我怀疑关系有问题,因为我使用了 Eloquent ORM,但即使在使用原始查询运行 DB::statement 之后,它也需要很长时间。