in Education by
I'm working a lot with a VERY large mysql2 database at the moment, and although I have indexed what I thought were the proper fields, the return time for some queries is quite slow. I have two models that are causing issues, Comment and Commenter. Now, there is a has_many relationship between Commenter and Comment, but my query relies on finding the each Comment's Commenter's username. So I'll run something like this: c = Blog.first.comments ##this bit runs fine, I indexed the "blog_id" field on Comments c.collect {|c| c.commenter.username} To help with the speed issues, I created an index on the commenter_id field for the Comment model. But it is still running very slow.. Does anybody know of what I could do differently, that would help increase the speed of the query? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
An index on commenter_id helps when you want to find the comments for a given commenter_id ("find me all the comments joe made"). But when you do c.commenter you're searching for users, presumably the one whose id is equal to the comment's commenter_id. There should already be an index on the id column. The surefire way is to take the actual sql statements generated (in development these are in development.log), and use explain on them, for example explain select * from comments where id = 12345 Given that it's very unlikely that you managed to create a table without an index on it's id column, the most likely culprit is eager loading - if a post had 500 comments then the above code would fetch the associated users one by one, and those 500 roundtrips to the database add up c.includes(:commenter).collect {...} or c.eager_load(:commenter).collect {...} will fix that (the above snippets assume you're using rails 3).

Related questions

0 votes
    ActiveRecord 3.2.14 I want to use ActiveRecord in a non-Rails Ruby project. I want to have ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    explain system tuning with regard to the ai project cycle..plz give the correct ans Select the correct answer from above options...
asked Dec 7, 2021 in Education by JackTerrance
0 votes
    explain system tuning with regard to the ai project cycle..plz give the correct ans Select the correct answer from above options...
asked Dec 5, 2021 in Education by JackTerrance
0 votes
    explain system tuning with regard to the ai project cycle..plz give the correct ans Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    In cassandra consistency is achieved through consistency tuning mechanisms (1)True (2)False...
asked May 7, 2021 in Technology by JackTerrance
0 votes
    In cassandra consistency is achieved through consistency tuning mechanisms (1)True (2)False...
asked Apr 16, 2021 in Technology by JackTerrance
0 votes
    A ________ is a logical grouping of database objects, usually to facilitate security, performance, or the ... Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    How inserting data through stored procedure do reduces network traffic and increase database performance? (a) ... Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    ___________ is popular for applications such as storage of log files in a database system since it offers the ... in division Storage and File Structures of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    I am trying to build a Rails 3.1 app in which users can preview individual pages of documents or ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    To configure the admin settings of my app I made a Admin controller and a AdminConfig model. The Admin ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    I am trying to figure out how to use concerns in my routes file. I have models called User, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    To configure the admin settings of my app I made a Admin controller and a AdminConfig model. The Admin ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    If I have a "Bill" entity, I might create that bill instance via REST API by assigning the food ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have a rails application which is api only i am using gem rails-api. Now I am trying to create ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
...