简体   繁体   中英

How to speed up active record in rails3.2? My active record is taking more than 3 minutes for querying

This is my active record query for loading accounts with respect to branch,area and mandal.Could anyone help me for optimizing it?

 HpEntry.includes(:area, :mandal, :branch)
        .where(:BranchId => 58, :AreaId => 117, :MandalId => 741)
        .last

In 3.2, you'll probably see a bit of a performance gain by explicitly specifying the order you want your results to be ordered, and reverse it so that you can call .first rather than .last .

For example:

HpEntry.includes(:area, :mandal, :branch)
       .where(:BranchId =>58, :AreaId => 117, :MandalId => 741)
       .order('created_at DESC') # <= newest first
       .first

You may also get better performance by adding indexes for the columns you are searching upon ( BranchId , AreaId and MandalId ). In PostgreSQL you'll get the best benefit by adding three indexes, one on each of the three columns. Other DBMS systems may get better performance by having a composite index.

As it is, whatever database you're using it'll do you good to learn and understand its EXPLAIN output, which will give you insight into what your database is doing, and which parts of your query are the slowest. That'll also give you an insight as to whether any changes you make are really helping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM