mysql 具有91K记录的异步API

wfypjpf4  于 2023-05-28  发布在  Mysql
关注(0)|答案(1)|浏览(93)

我有一个局部模型,里面有大约91K的记录。我的位置模型只有3个字段:id,name & city_id我将它与Cities Table连接起来,以区分Locality Table中几乎相似的名称

$data = Locality::query()->join('cities', 'cities.id', '=', 'localities.city_id')->select('cities.id as id', DB::Raw("CONCAT(localities.name, ', ', cities.name) AS localityCity"))
->orderBy('localities.name')
->when($request->search, fn (Builder $query) => $query->where('localities.name', 'like', "%{$request->search}%"))
->when(
    $request->exists('selected'),
    fn (Builder $query) => $query->whereIn('localities.id', $request->input('selected', [])),
    fn (Builder $query) => $query->limit(10)
)
->get();

当我用它来添加新的本地性时,这段代码工作正常,但是当API已经选择了数据时,它会显示Too Many Request Error,当我禁用throttle并重试时,选择数据需要花费太多时间

pzfprimi

pzfprimi1#

您应该优化查询。是否尝试创建表索引?您还可以尝试使用chunk方法检索记录。看看你的Laravel版本的chunk方法。https://laravel.com/docs/10.x/queries#chunking-results

相关问题