简体   繁体   中英

Query still getting logged as slow-query in mysql server in Rails app even after enabling low level caching

I have recently added low level caching to a query in Rails 3.2.14 app. In my local I do not see the cached query running twice but its getting logged in production.

I have even checked answers given on this thread where it talks about converting the query result to an array using to_a method.

@top_quiz_creators = Rails.cache.fetch("/quiz/creators", expires_in: 1.hours) do
   Quiz.find_by_sql("some complex query").to_a
end

In mysql-slow.log:

# Time: 170118  8:28:32
# User@Host: db[db] @ ip-xyz.ec2.internal [xyz]
# Query_time: 5.403992  Lock_time: 0.000074 Rows_sent: 5  Rows_examined: 10272991
SET timestamp=1484728112;

How can I stop this query from hitting DB again and again?

To speed up your query have to keep two things in your mind

  1. First implement the foreign keys and indexes in the database level
  2. And use ActiveRecord wisely

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