I'm working on PostgreSQL with PHP.
Is it possible to select a particular number of random values in a column FROM table WHERE condition
instead of
select column FROM table WHERE condition
then convert them into an array and use array_rand()?
(I do not want to use this way because I will have millions of rows and selecting all values first then array_rand() is probably going to take a lot of time.)
Let's say I have a table like this:
name | items
-----------+------------
Ben | {dd,ab,aa}
-----------+------------
David | {dd,aa}
-----------+------------
Bryan | {aa,ab,cd}
-----------+------------
Glen | {cd}
-----------+------------
Edward | {aa,cd}
-----------+------------
Aaron | {dd,aa}
-----------+------------
..... (many many more)
Updates:
And I need to select the 10 random values in a column (or basically the 10 random rows) that match a condition (in this case would be @> ARRAY[aa]) without a sequential table scan or something that is time-consuming.
order by random() is going to take a lot of time as it has to deal with every row so I will go with a better solution instead.