0% found this document useful (0 votes)
131 views19 pages

Ruby On Rails: Database Indexing Techniques

This document discusses database indexing techniques in Ruby on Rails. It explains what database indexing is, why it is important for performance, and how to add indexes in Rails. It covers creating singular and composite indexes, indexing associations and foreign keys, and using the rails_indexes plugin to identify missing indexes. Bottlenecks of adding too many indexes are also addressed.

Uploaded by

Sandip Ransing
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
131 views19 pages

Ruby On Rails: Database Indexing Techniques

This document discusses database indexing techniques in Ruby on Rails. It explains what database indexing is, why it is important for performance, and how to add indexes in Rails. It covers creating singular and composite indexes, indexing associations and foreign keys, and using the rails_indexes plugin to identify missing indexes. Bottlenecks of adding too many indexes are also addressed.

Uploaded by

Sandip Ransing
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 19

Ruby On Rails: Database

Indexing Techniques

san2821@gmail.com
Who am I ?

Rails Developer at JOsh software,


Tech. Blogger at funonrails,
Opensource Evangelist

Github @sandipransing Twitter @sandipransing

Scribd @sandip.ransing Skype @sandip.ransing


Contents
 DB indexing ?

Why ?

How ?
 How to display indexes in databse table ?
 Is there any overhead when there are lot indexes

What things needs to be taken care of ?
 What are the different tyeps ? (singular/composite)
 How to implement in rails ?
DB Indexing
 a database index is a data structure that improves
the speed of operations on a database table
Why to index DB table ?
 If you don't index database tables, its obivous that
app performance is going to be suck ;)
 It does not mean that you are going to add index
on every column of database table otherwise it's
going to be bottleneck in performace :)
How to add indexes
 CREATE INDEX Syntax

CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
 [index_type]
 ON tbl_name (index_col_name,...)
 [index_type]


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 | 0 | PRIMARY | 1 | id |A | 114905 | NULL | NULL | | BTREE | |


| 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 |
|

 | customer_cars | 1 | index_customer_cars_on_id | 1 | 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

You might also like