fast_random 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +9 -8
- data/lib/fast_random.rb +3 -6
- data/lib/fast_random/version.rb +1 -1
- metadata +19 -41
data/README.markdown
CHANGED
@@ -8,19 +8,20 @@ use <http://jan.kneschke.de/projects/mysql/order-by-rand/> to replace ORDER BY R
|
|
8
8
|
|
9
9
|
Now let's see what happends to our performance. We have 3 different queries for solving our problems.
|
10
10
|
|
11
|
-
Q1. ORDER BY RAND()
|
12
|
-
Q2. RAND() * MAX(ID)
|
13
|
-
Q3. RAND() * MAX(ID) + ORDER BY ID
|
11
|
+
* Q1. ORDER BY RAND()
|
12
|
+
* Q2. RAND() * MAX(ID)
|
13
|
+
* Q3. RAND() * MAX(ID) + ORDER BY ID
|
14
14
|
|
15
15
|
Q1 is expected to cost N * log2(N), Q2 and Q3 are nearly constant.
|
16
16
|
|
17
17
|
The get real values we filled the table with N rows ( one thousand to one million) and executed each query 1000 times.
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
|
20
|
+
100 1.000 10.000 100.000 1.000.000
|
21
|
+
Q1 0:00.718s 0:02.092s 0:18.684s 2:59.081s 58:20.000s
|
22
|
+
Q2 0:00.519s 0:00.607s 0:00.614s 0:00.628s 0:00.637s
|
23
|
+
Q3 0:00.570s 0:00.607s 0:00.614s 0:00.628s 0:00.637s
|
24
|
+
As you can see the plain ORDER BY RAND() is already behind the optimized query at only 100 rows in the table.
|
24
25
|
|
25
26
|
|
26
27
|
## Install
|
data/lib/fast_random.rb
CHANGED
@@ -2,14 +2,11 @@
|
|
2
2
|
require 'active_support/concern'
|
3
3
|
module FastRandom
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
5
|
+
|
6
6
|
module ClassMethods
|
7
7
|
def random
|
8
8
|
joins("join (SELECT CEIL(RAND() * (SELECT MAX(id) FROM #{table_name})) AS gid) AS r2").where("#{table_name}.id > r2.gid")
|
9
9
|
end
|
10
10
|
end
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
11
|
+
|
12
|
+
end
|
data/lib/fast_random/version.rb
CHANGED
metadata
CHANGED
@@ -1,34 +1,23 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: fast_random
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- xdite
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2011-12-29 00:00:00 +08:00
|
19
|
-
default_executable:
|
12
|
+
date: 2013-02-07 00:00:00.000000000 Z
|
20
13
|
dependencies: []
|
21
|
-
|
22
14
|
description: ultra fast order by rand() solution, see http://jan.kneschke.de/projects/mysql/order-by-rand/
|
23
|
-
email:
|
15
|
+
email:
|
24
16
|
- xuite.joke@gmail.com
|
25
17
|
executables: []
|
26
|
-
|
27
18
|
extensions: []
|
28
|
-
|
29
19
|
extra_rdoc_files: []
|
30
|
-
|
31
|
-
files:
|
20
|
+
files:
|
32
21
|
- .gitignore
|
33
22
|
- Gemfile
|
34
23
|
- README.markdown
|
@@ -36,39 +25,28 @@ files:
|
|
36
25
|
- fast_random.gemspec
|
37
26
|
- lib/fast_random.rb
|
38
27
|
- lib/fast_random/version.rb
|
39
|
-
has_rdoc: true
|
40
28
|
homepage: https://github.com/xdite/fast_random
|
41
29
|
licenses: []
|
42
|
-
|
43
30
|
post_install_message:
|
44
31
|
rdoc_options: []
|
45
|
-
|
46
|
-
require_paths:
|
32
|
+
require_paths:
|
47
33
|
- lib
|
48
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
35
|
none: false
|
50
|
-
requirements:
|
51
|
-
- -
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
|
54
|
-
|
55
|
-
- 0
|
56
|
-
version: "0"
|
57
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
41
|
none: false
|
59
|
-
requirements:
|
60
|
-
- -
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
segments:
|
64
|
-
- 0
|
65
|
-
version: "0"
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
66
46
|
requirements: []
|
67
|
-
|
68
47
|
rubyforge_project: fast_random
|
69
|
-
rubygems_version: 1.
|
48
|
+
rubygems_version: 1.8.25
|
70
49
|
signing_key:
|
71
50
|
specification_version: 3
|
72
51
|
summary: ultra fast order by rand() solution, see http://jan.kneschke.de/projects/mysql/order-by-rand/
|
73
52
|
test_files: []
|
74
|
-
|