fast_random 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fast_random.gemspec
4
+ gemspec
5
+
6
+ gem "activesupport", "~> 3.0.0"
data/README.markdown ADDED
@@ -0,0 +1,52 @@
1
+ Database (e.g. MySql) :order=>'rand()' is slow.
2
+
3
+ ## Solution
4
+
5
+ use <http://jan.kneschke.de/projects/mysql/order-by-rand/> to replace ORDER BY RAND()
6
+
7
+ ## Performance
8
+
9
+ Now let's see what happends to our performance. We have 3 different queries for solving our problems.
10
+
11
+ Q1. ORDER BY RAND()
12
+ Q2. RAND() * MAX(ID)
13
+ Q3. RAND() * MAX(ID) + ORDER BY ID
14
+
15
+ Q1 is expected to cost N * log2(N), Q2 and Q3 are nearly constant.
16
+
17
+ The get real values we filled the table with N rows ( one thousand to one million) and executed each query 1000 times.
18
+
19
+ 100 1.000 10.000 100.000 1.000.000
20
+ Q1 0:00.718s 0:02.092s 0:18.684s 2:59.081s 58:20.000s
21
+ Q2 0:00.519s 0:00.607s 0:00.614s 0:00.628s 0:00.637s
22
+ Q3 0:00.570s 0:00.607s 0:00.614s 0:00.628s 0:00.637s
23
+ As you can see the plain ORDER BY RAND() is already behind the optimized query at only 100 rows in the table.
24
+
25
+
26
+ ## Install
27
+
28
+ Gemfile
29
+
30
+ ``` ruby
31
+ gem 'fast_random'
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ include FastRandom in model
37
+
38
+ ```
39
+ class Post < ActiveRecord::Base
40
+ include FastRandom
41
+ end
42
+ ```
43
+
44
+ ### Query
45
+
46
+ * try: `Post.random.limit(10)`
47
+
48
+ ## License
49
+
50
+ Copyright (c) 2011 xdite
51
+
52
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "fast_random/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "fast_random"
7
+ s.version = FastRandom::VERSION
8
+ s.authors = ["xdite"]
9
+ s.email = ["xuite.joke@gmail.com"]
10
+ s.homepage = "https://github.com/xdite/fast_random"
11
+ s.summary = %q{ultra fast order by rand() solution, see http://jan.kneschke.de/projects/mysql/order-by-rand/}
12
+ s.description = %q{ultra fast order by rand() solution, see http://jan.kneschke.de/projects/mysql/order-by-rand/}
13
+
14
+ s.rubyforge_project = "fast_random"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
25
+
@@ -0,0 +1,15 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'active_support/concern'
3
+ module FastRandom
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def random
8
+ joins("join (SELECT CEIL(RAND() * (SELECT MAX(id) FROM #{table_name})) AS gid) AS r2").where("#{table_name}.id > r2.gid")
9
+ end
10
+ end
11
+
12
+ module InstanceMethods
13
+ end
14
+
15
+ end
@@ -0,0 +1,3 @@
1
+ module FastRandom
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fast_random
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - xdite
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-29 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: ultra fast order by rand() solution, see http://jan.kneschke.de/projects/mysql/order-by-rand/
23
+ email:
24
+ - xuite.joke@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.markdown
35
+ - Rakefile
36
+ - fast_random.gemspec
37
+ - lib/fast_random.rb
38
+ - lib/fast_random/version.rb
39
+ has_rdoc: true
40
+ homepage: https://github.com/xdite/fast_random
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project: fast_random
69
+ rubygems_version: 1.5.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: ultra fast order by rand() solution, see http://jan.kneschke.de/projects/mysql/order-by-rand/
73
+ test_files: []
74
+