0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

laravel random 遅い

Posted at

約 5万件のデータから、ランダムでデータを取得したい

結論

速く検索するには
1 一旦主キーのみで検索し、データを取得
2 1⃣を元に検索する

0.05秒 - 0.07秒

hoge.php
//① 主キーだけをランダムで取得
$rand_id = Answer::query()
    ->select('id')
    ->inRandomOrder()
    ->take(1)
    ->first();//最初の

//② 取得したランダムキーで検索
$answer = Answer::query()
    ->where('id',$rand_id->id)
    ->take(1)
    ->get()->toArray();

遅い

とにかくすべてをランダムに取得

0.16秒 - 0.2秒

hoge.php

$answer = Answer::query()
    ->inRandomOrder()
    ->take(1)
    ->get()->toArray();


以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?