ruby on rails - Sorting records quickly from a model with 500k records -


i attempting sort records in model has 500k rows in it. when attempted procedure had 200 records , used following code , pulled out records 1-5 list popular:

@mostpopular = product.find(:all, :order => 'click_count desc')

however, have far larger dataset, grinds computer halt , looking try complete search in more efficient manner.

i have tried adjusting code @mostpopular = product.order('click_count desc').limit(10) still take long time complete...

is there more efficient way pull out top 10 popular records large dataset?

thanks time

the answer not in rails, it's in database.

write query log, can see query being done:

logger.debug product.find(:all, :order => 'click_count desc').limit(10).to_sql 

once have sql in hand, head on database's console , ask show query plan , statistics query. don't database you're using, in postgresql, you'd use explain command. i'll see row scan (aka sequence scan) being done.

you may find click_count missing index, , adding fixes trouble.


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -