异步 API 具有 91,000 条记录

huangapple go评论78阅读模式
英文:

Async API having 91K Records

问题

我有一个包含约91,000条记录的地点模型。
我的地点模型只有3个字段:id、name和city_id。
我正在与城市表联接以区分与地点表中几乎相似的名称

$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",当我禁用节流并再次尝试时,选择数据需要太长时间。

英文:

I have a Locality Model having about 91K Records in it.
My Locality Model has only 3 Fields: id, name & city_id
I am Joining it with Cities Table to differentiate the almost similar names from the 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();

This Code works fine when I am using it to add new locality
but when the api has already selected data then it is showing Too Many Request Error and when I disable the throttle and try again it takes too much time to select the data

答案1

得分: 1

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

英文:

You should optimize your query. Did you try creating table index? You can also try to retrieve the records with the chunk method. Take a look at the chunk method for your Laravel version.
https://laravel.com/docs/10.x/queries#chunking-results

huangapple
  • 本文由 发表于 2023年5月26日 10:25:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定