queries taking time inside `mon_synchronize`

Hi All,

I have a Rails app (rails version 5.2.4.1 / ruby 2.6.3) that makes queries to a postgres database using ActiveRecord.

I want to reduce query latency.

I’m using NewRelic to understand what the slow queries are, and I see pretty much each one of them is spending time inside the mon_synchronize method in monitor.rb

https://github.com/ruby/ruby/blob/12c7321d758c736d42dbbd36c74628f7a243851e/lib/monitor.rb#L230

I’m attaching a few screenshots from NewRelic.

My question is - what is the reason ActiveRecord is taking this lock? Is there anything I can do to get a better response from the queries.

The database itself is pretty small - thousands of records.

thanks,

thushara

Hi Thushara,

Just to double-check: Is your database indexed correctly? Have you tried plugging in something like lol_dba gem to spot missing indexes?

Yes - the indexes are there. Most times, postgres doesn’t use them as it is faster to just read the rows from the table (vs going back to the table again). I’ve seen this to be the case unless the tables are large. Also, it is generally pretty fast - but there is this occasional slow down.

Hello there, i know this is an old question you posted but i wonder whether you got to the bottom of this. I am finding myself on a very similar situation to the one you originally described and I would be very grateful for any pointers on how to solve this.

with best wishes, nat

Well, it seemed like what would happen if all cores were busy with other queries.

We moved our data to redis, which is a magnitude faster.

1 Like