From d8ec265ca493cddaa044f825299b2588623d0e92 Mon Sep 17 00:00:00 2001 From: lukemelia Date: Thu, 11 Jan 2007 05:29:29 +0000 Subject: [PATCH] Added some database optimizations, mostly in the form of indices on commonly queried columns git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@396 a4c988fc-2ded-0310-b66e-134b36920a42 --- tracks/app/controllers/application.rb | 8 +------ tracks/app/controllers/context_controller.rb | 2 +- tracks/app/models/user.rb | 2 +- tracks/db/migrate/022_add_indices.rb | 23 ++++++++++++++++++++ tracks/db/schema.rb | 14 +++++++++++- tracks/lib/login_system.rb | 10 ++++++++- 6 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 tracks/db/migrate/022_add_indices.rb diff --git a/tracks/app/controllers/application.rb b/tracks/app/controllers/application.rb index 946dc831..df4731f3 100644 --- a/tracks/app/controllers/application.rb +++ b/tracks/app/controllers/application.rb @@ -16,7 +16,6 @@ class ApplicationController < ActionController::Base layout 'standard' before_filter :set_session_expiration - before_filter :get_current_user prepend_before_filter :login_required after_filter :set_charset @@ -64,12 +63,7 @@ class ApplicationController < ActionController::Base end private - - def get_current_user - @user = User.find(session['user_id']) if session['user_id'] - @prefs = @user.prefs unless @user.nil? - end - + def parse_date_per_user_prefs( s ) return nil if s.blank? @user.prefs.tz.unadjust(Date.strptime(s, @user.prefs.date_format)).utc.to_date diff --git a/tracks/app/controllers/context_controller.rb b/tracks/app/controllers/context_controller.rb index 648b843d..f890923d 100644 --- a/tracks/app/controllers/context_controller.rb +++ b/tracks/app/controllers/context_controller.rb @@ -155,7 +155,7 @@ class ContextController < ApplicationController # TODO: Temporarily doing this search manually until I can work out a way # to do the same thing using not_done_todos acts_as_todo_container method # Hides actions in hidden projects from context. - @not_done_todos = @context.todos.find_in_state(:all, :active, :order => "todos.due IS NULL, todos.due ASC, todos.created_at ASC") + @not_done_todos = @context.todos.find(:all, :conditions => ['todos.state = ?', 'active'], :order => "todos.due IS NULL, todos.due ASC, todos.created_at ASC", :include => :project) @count = @not_done_todos.size end diff --git a/tracks/app/models/user.rb b/tracks/app/models/user.rb index 3ec3277e..98717aa4 100644 --- a/tracks/app/models/user.rb +++ b/tracks/app/models/user.rb @@ -32,7 +32,7 @@ class User < ActiveRecord::Base end end has_many :notes, :order => "created_at DESC", :dependent => :delete_all - has_one :preference + has_one :preference, :dependent => :destroy attr_protected :is_admin diff --git a/tracks/db/migrate/022_add_indices.rb b/tracks/db/migrate/022_add_indices.rb new file mode 100644 index 00000000..45319b5d --- /dev/null +++ b/tracks/db/migrate/022_add_indices.rb @@ -0,0 +1,23 @@ +class AddIndices < ActiveRecord::Migration + def self.up + add_index :todos, [:user_id, :state] + add_index :todos, [:user_id, :project_id] + add_index :todos, [:project_id] + add_index :todos, [:context_id] + add_index :todos, [:user_id, :context_id] + add_index :preferences, :user_id + add_index :projects, :user_id + add_index :contexts, :user_id + end + + def self.down + remove_index :contexts, :user_id + remove_index :projects, :user_id + remove_index :preferences, :user_id + remove_index :todos, [:user_id, :context_id] + remove_index :todos, [:project_id] + remove_index :todos, [:context_id] + remove_index :todos, [:user_id, :project_id] + remove_index :todos, [:user_id, :state] + end +end diff --git a/tracks/db/schema.rb b/tracks/db/schema.rb index c44ce4f9..55b70a1e 100644 --- a/tracks/db/schema.rb +++ b/tracks/db/schema.rb @@ -2,7 +2,7 @@ # migrations feature of ActiveRecord to incrementally modify your database, and # then regenerate this schema definition. -ActiveRecord::Schema.define(:version => 21) do +ActiveRecord::Schema.define(:version => 22) do create_table "contexts", :force => true do |t| t.column "name", :string, :default => "", :null => false @@ -11,6 +11,8 @@ ActiveRecord::Schema.define(:version => 21) do t.column "user_id", :integer, :default => 0, :null => false end + add_index "contexts", ["user_id"], :name => "index_contexts_on_user_id" + create_table "notes", :force => true do |t| t.column "user_id", :integer, :default => 0, :null => false t.column "project_id", :integer, :default => 0, :null => false @@ -54,6 +56,8 @@ ActiveRecord::Schema.define(:version => 21) do t.column "time_zone", :string, :default => "London", :null => false end + add_index "preferences", ["user_id"], :name => "index_preferences_on_user_id" + create_table "projects", :force => true do |t| t.column "name", :string, :default => "", :null => false t.column "position", :integer, :default => 0, :null => false @@ -62,6 +66,8 @@ ActiveRecord::Schema.define(:version => 21) do t.column "state", :string, :limit => 20, :default => "active", :null => false end + add_index "projects", ["user_id"], :name => "index_projects_on_user_id" + create_table "sessions", :force => true do |t| t.column "session_id", :string t.column "data", :text @@ -83,6 +89,12 @@ ActiveRecord::Schema.define(:version => 21) do t.column "state", :string, :limit => 20, :default => "immediate", :null => false end + add_index "todos", ["user_id", "state"], :name => "index_todos_on_user_id_and_state" + add_index "todos", ["user_id", "project_id"], :name => "index_todos_on_user_id_and_project_id" + add_index "todos", ["project_id"], :name => "index_todos_on_project_id" + add_index "todos", ["context_id"], :name => "index_todos_on_context_id" + add_index "todos", ["user_id", "context_id"], :name => "index_todos_on_user_id_and_context_id" + create_table "users", :force => true do |t| t.column "login", :string, :limit => 80 t.column "password", :string, :limit => 40 diff --git a/tracks/lib/login_system.rb b/tracks/lib/login_system.rb index 1761f868..d1fb3196 100644 --- a/tracks/lib/login_system.rb +++ b/tracks/lib/login_system.rb @@ -46,7 +46,7 @@ module LoginSystem return true end - if session['user_id'] and authorize?(User.find(session['user_id'])) + if session['user_id'] and authorize?(get_current_user) return true end @@ -64,6 +64,14 @@ module LoginSystem access_denied return false end + + def get_current_user + if @user.nil? && session['user_id'] + @user = User.find session['user_id'], :include => :preference + end + @prefs = @user.prefs unless @user.nil? + @user + end # overwrite if you want to have special behavior in case the user is not authorized # to access the current operation.