1DBIx::Class::Helper::ReUssuelrtSCeotn:t:rRiabnudtoemdD(B3PI)exr:l:CDloacsusm:e:nHtealtpieorn::ResultSet::Random(3)
2
3
4
6 DBIx::Class::Helper::ResultSet::Random - Get random rows from a
7 ResultSet
8
10 # note that this is normally a component for a ResultSet
11 package MySchema::ResultSet::Bar;
12
13 use strict;
14 use warnings;
15
16 use parent 'DBIx::Class::ResultSet';
17
18 __PACKAGE__->load_components('Helper::ResultSet::Random');
19
20 # in code using resultset:
21 my $random_row = $schema->resultset('Bar')->rand->single;
22
24 This component allows convenient selection of random rows. See "NOTE"
25 in DBIx::Class::Helper::ResultSet for a nice way to apply it to your
26 entire schema.
27
28 Currently this works by doing something akin to
29
30 SELECT TOP($x) from $table ORDER BY RANDOM()
31
32 Lots of people think this is slow. My own benchmarks show that doing
33 the above, for 10 rows in a table with just over 8 million rows, is
34 nearly instant. Although that was with SQL Server, and different
35 databases will handle that differently.
36
37 So please, if you have performance issues and want this to work with
38 your database, get in touch and I will do what I can to get it to work
39 quickly enough to suite your needs.
40
42 rand
43 This method takes a single argument, being the size of the random
44 ResultSet to return. It defaults to 1. This Component will throw
45 exceptions if the argument is not an integer or not greater than zero.
46
47 _rand_order_by
48 This module currently does an "ORDER BY" on some db specific function.
49 If for some reason it guesses incorrectly for your database the easiest
50 way to fix that in the short-term (ie without patching upstream) is to
51 override this method. So for example, if your db uses "RAND()" instead
52 of "RANDOM()" and it's not in the predefined list of dbs you could just
53 do the following in your ResultSet class:
54
55 sub _rand_order_by { 'RAND()' }
56
58 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
59
61 This software is copyright (c) 2020 by Arthur Axel "fREW" Schmidt.
62
63 This is free software; you can redistribute it and/or modify it under
64 the same terms as the Perl 5 programming language system itself.
65
66
67
68perl v5.36.0 2022D-B0I7x-:2:2Class::Helper::ResultSet::Random(3)