Ruby On Rails: Database Indexing Techniques
Ruby On Rails: Database Indexing Techniques
Indexing Techniques
san2821@gmail.com
Who am I ?
index_col_name:
col_name [(length)] [ASC | DESC]
index_type:
USING {BTREE | HASH}
CREATE INDEX enables you to add indexes to existing tables.
A column list of the form (col1,col2,...) creates a multiple-column index.
Prefixes can be specified for CHAR, VARCHAR, BINARY, and VARBINARY columns.
CREATE INDEX part_of_name ON customer (name(10));
MySQL showing indexes for
table
show index from <table-name>
show index from customer_cars;
+---------------+------------+-----------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type |
Comment |
+---------------+------------+-----------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| customer_cars | 1 | index_customer_cars_on_customer_id_and_car_id | 1 | customer_id | A | 114905 | NULL | NULL | | BTREE |
|
| customer_cars | 1 | index_customer_cars_on_customer_id_and_car_id | 2 | car_id |A | 114905 | NULL | NULL | | BTREE |
|
+---------------+------------+-----------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
4 rows in set (0.03 sec)
Bottlenecks
Performance doesn't comes straight
Every record getting created or inserted into the
DB table adds extra burden of executing index for
that particular record.
Also indexes get strored on file-system
Too many indexes results into poor performance
as choosing indexes adds complexity
Careful choice of indexes certainly improves
performance.
What things needs to
concentrated ?
Observe for slower queries based on time
Observe for each query based on -
Frequency
Foreign keys
Orders
Conditions
Types
Singular made up of one column
Composite made up of multiple columns
Rails DB indexing
In rails, while creating indexes we need to
observe for associations and foreign_keys
Indexing simple associations
class SuccessCall < ActiveRecord::Base
belongs_to :call
end
class Call < ActiveRecord::Base
has_many :success_calls
end
add_index :success_calls, :call_id
Rails DB indexing
Indexing polymorphic associations (composite)
class Comment < ActiveRecord::Base
# A comment can be from 'Review', 'Post', 'VideoGallery', 'PhotoGallery', 'Product'
# contains columns resource_id, resource_type
belongs_to :resource, :polymorphic => true
end
class Post < ActiveRecord::Base
has_many :comments, :as => resource
end
add_index :comments, [:resource_id, :resource_type]
Rails DB indexing
Indexing foreign_keys (singular)
class Following < ActiveRecord::Base
belongs_to :follower, :class_name => 'User', :foreign_key => :follower_id
belongs_to :followed, :class_name => 'User', :foreign_key => :followed_id
end
class User < ActiveRecord::Base
has_many :following_as_follower, :foreign_key => :follower_id, :class_name =>
'Following'
has_many :following_as_followed, :foreign_key => :followed_id, :class_name =>
'Following'
End
(continue...)
Rails DB indexing
Indexing foreign_keys (singular)
add_index :following, :follower_id
add_index :following, :followed_id
Rails DB indexing
Indexing finders
find_by_<attribute>
add_index :<table_name>, attribute
find_by_<attribute1>_and_<attribute2>
add_index :<table_name>, [attribute1, attribute2]
conditions => ['<attr1> = ? and <attr2> = ? or
<attr3> = ?', ?, ?,?]
add_index :<table_name>, [attr, attr2, attr3]
Migrating Indexes
Create Index
add_index(table_name, column_names, options)
Adds a new index with the name of the column.
Other options include :name and :unique (e.g. {
:name => "users_name_index", :unique => true }).
Remove Index
remove_index(table_name, index_name)
Removes the index specified by index_name.
NOTE: sometimes index names becomes too large so that it's not
supported by database in such cases make use of name option
Got Lasy ?
There is plugin called rails_indexes
Description
A rake task to track down missing database indexes that does not
assume that all foreign keys end with the convention of _id
Installation
script/plugin install git://github.com/eladmeidar/rails_indexes.git
Usage
rake db:index_migration
rake db:find_query_indexes
Questions ??
???
References
http://api.rubyonrails.org/
http://dev.mysql.com/doc/refman/5.0/en/create-index.html
http://github.com/eladmeidar/rails_indexes