我很难理解为什么 Cloud SQL 比我的本地 MySQL 服务器稍微慢一些。
我可以接受从配置到配置可能会发生一些变化,但是......
在 localhost 中,只需 240 毫秒即可完成一个查询,该查询仅返回表中的所有记录及其关系(只有 90 条记录),而在 Cloud SQL 中则需要近30 秒。它就像 28kb 的数据。
我在 Laravel 5.5 中使用 PHP。根据所有 GCP 文档,我拥有所需的所有配置。
我不认为这是一个配额问题。
使用第二代 Cloud SQL 并从具有自动缩放功能的 App Engine flex 环境连接。
我确实将 Cloud SQL 实例的服务器规格提高到了 1.7 GB RAM。我不认为这是与服务器性能相关的问题...
谢谢。
编辑
这令人困惑。
我正在调试正在执行的原始 SQL 字符串,当我使用 DD 进行调试时,我正在阻止 JSON 响应,它只需要 1 秒。由于某种我无法理解的原因,好多了。
这是结局更快的一个
DB::enableQueryLog();
Contact::with([
'rooms',
'bathrooms',
'parkingLots',
'generalInterests',
'locationInterests',
'strongInterests',
'phonePrefix',
'source',
'contactType'
])->get();
dd(DB::getQueryLog());
return new JsonResponse([
'query' => 'all',
'results' => Contact::with([
'rooms',
'bathrooms',
'parkingLots',
'generalInterests',
'locationInterests',
'strongInterests',
'phonePrefix',
'source',
'contactType'
])->get()
], 200);
这是一个只需要同样长时间的
DB::enableQueryLog();
Contact::with([
'rooms',
'bathrooms',
'parkingLots',
'generalInterests',
'locationInterests',
'strongInterests',
'phonePrefix',
'source',
'contactType'
])->get();
Log::debug(print_r(DB::getQueryLog(), true));
return new JsonResponse([
'query' => 'all',
'results' => Contact::with([
'rooms',
'bathrooms',
'parkingLots',
'generalInterests',
'locationInterests',
'strongInterests',
'phonePrefix',
'source',
'contactType'
])->get()
], 200);
如您所见,唯一的区别是是否达到了回报。
编辑 2
发现问题
事实证明,当返回 JSON 响应时,Eloquent 模型会处理模型的附加程序,这些附加程序会向另一个 API REST 发出一些请求,并且它会为结果集中的每个模型执行此操作。
在 localhost 中,相同的操作只是在发出请求之前返回 null。所以,这是我的问题。
与 MySQL、GCP、Laravel 或 PHP 无关。只是我自己的愚蠢。
非常感谢您花时间阅读这个荒谬的问题。我最诚挚的歉意。