Re: [sqlite] random rows

2018-06-02 Thread Yuriy M. Kaminskiy
On 06/01/18 04:41 , Abroży Nieprzełoży wrote: > 2018-06-01 2:12 GMT+02:00, Torsten Curdt : >> I need to get some random rows from a large(ish) table. >> >> The following seems to be the most straight forward - but not the fastest. >> >> SELECT * FROM table ORDER BY random() limit 200 >> >> Is

Re: [sqlite] random rows

2018-06-01 Thread Stephen Chrzanowski
Here's my two cents. Don't spend it all in one place... CREATE TABLE [RandomTable]( [PriID] INTEGER PRIMARY KEY AUTOINCREMENT, [DataCol] CHAR); INSERT INTO [RandomTable]([PriID], [DataCol]) VALUES(1, 'a'); INSERT INTO [RandomTable]([PriID], [DataCol]) VALUES(2, 'b'); INSERT INTO

Re: [sqlite] random rows

2018-06-01 Thread Don V Nielsen
?? SELECT * FROM table WHERE id IN (SELECT id FROM table ORDER BY RANDOM() LIMIT x) Maybe. It is more memory efficient then trying to sort the entire lot of data. On Thu, May 31, 2018 at 7:13 PM Torsten Curdt wrote: > I need to get some random rows from a large(ish) table. > > The following

Re: [sqlite] random rows

2018-06-01 Thread Shevek
You may find it faster to do: select c from t where rowid in (list-of-constants) and generate the list of constants using something like a blackrock permutation generator. That takes linear time, whereas all the order-by variants are n.log(n). You need some sort of row-id generator function,

Re: [sqlite] random rows

2018-05-31 Thread Jay Kreibich
I’m not entirely sure your solution will have an even distribution. It depends a lot on how many times random() is called (once per row vs once per sort operation), and how the sort algorithm works. I might do this instead: SELECT * FROM (SELECT random(), t.* FROM t) ORDER BY 1 LIMIT 200;

Re: [sqlite] random rows

2018-05-31 Thread Abroży Nieprzełoży
hmm... i think: begin transaction; query 'select count(*) from tab' to get total number of records, name it N randomly select 200 integers from the range 0 to N-1 for each number query 'select * from tab limit 1 offset ?' with '?' bound to selected number end transaction; 2018-06-01 2:12

[sqlite] random rows

2018-05-31 Thread Torsten Curdt
I need to get some random rows from a large(ish) table. The following seems to be the most straight forward - but not the fastest. SELECT * FROM table ORDER BY random() limit 200 Is there a faster/better approach? cheers, Torsten ___ sqlite-users