0

I am doing a test solution module at Laravel. Users solve the test. And properly solved tests will not be returned to users.

Questions are always mixed. Obtained by random method.

My code:

$test_status= DB::table('test_status')->where('status', '=', 'check')-select('id')->get();

$test= DB::table('test')->whereNotIn('id', $test_status)->inRandomOrder()->first();

In this case, I will not show the user back the tests that he has previously solved.

The problem is that if the user has found the answer to 10,000 tests, will it be too late for me to get a query from the database? If there is a better way, please share

OMR
  • 11,736
  • 5
  • 20
  • 35
  • Does this answer your question? [Is SQL IN bad for performance?](https://stackoverflow.com/questions/1013797/is-sql-in-bad-for-performance) – Ion Mar 07 '21 at 15:50
  • if your first query is slow, then make an index `status` column – STA Mar 07 '21 at 15:59
  • This does not fit my question –  Mar 07 '21 at 15:59
  • Droid, Can you tell me exactly how? –  Mar 07 '21 at 16:01
  • I am just curious, `whereNotIn()` accept an index array, but in your case `$test_status` is a arry + object – STA Mar 07 '21 at 16:02
  • I'd say that that question is your exact case. If you have a few records, using any `WHERE IN / WHERE NOT in` is not a problem. 10000 is just too much for that, even if you have an index on `status`. Look at `UNION / EXCEPT / INTERSECT`. – Ion Mar 07 '21 at 16:07
  • You might run into the problem that the query itself is too long or exceeds the amount of bound parameters allowed rather than performance issues. – IGP Mar 07 '21 at 17:00

0 Answers0