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
Post a Comment