0

I want to use persistent to fetch a number of records from the database, selected randomly.

My first idea was to have an auto-incremented id field on the model, and fetch records by id. But I quickly concluded that this is probably not a good idea. If for instance some records are deleted, there will be gaps. (in my scenario the data will be more or less static, but it's still vulnerable)

For now the plan is to use mongodb, but this may change, I'm just researching now, the project hasn't started yet.

Some databases have native functions for selecting random records, does persistent support this? Or is there another way to accomplish the same?

maillard
  • 680
  • 1
  • 14
  • 27
  • Servant does not have *anything* to do with how you retrieve data from your database, it only handles RESTful HTTP transport. A better question would be asking this and providing information about the type database you are using and the way you are accessing that database in Haskell, but servant is not relevant to that. – Alexis King Nov 12 '16 at 20:35
  • Oh, right, i'm mixing up things here. Will update the question – maillard Nov 12 '16 at 20:36
  • 1
    As far as I know, at least for SQL, there is no particular efficient way of selecting a random entry from a table, so I don’t expect the bindings to provide one. – Joachim Breitner Nov 12 '16 at 20:56
  • For selecting one (or more) rows at random from a table: http://stackoverflow.com/q/19412/1126841. Spoiler: you want some variation on `select ... order by random() limit `. – chepner Nov 12 '16 at 21:25
  • Use persistent's rawSql function and use chepner's solution from the above to achieve what you want. – Sibi Nov 12 '16 at 21:39
  • https://docs.mongodb.com/v3.2/reference/operator/aggregation/sample/ – Daniele Tassone Nov 12 '16 at 21:42
  • Better than the "order by random" is to use TABLESAMPLE with SQL if it is supported (https://www.postgresql.org/docs/current/static/sql-select.html#SQL-FROM) – Richard Huxton Nov 12 '16 at 22:57

0 Answers0