ruby-on-railsrubypostgresqlsequelsequel-gem

Is instance.attribute_name and instance[:attribute_name] exactly the same for sequel model attribute (db column)?


I use Rails 5.1.4, Sequel 5.9.0, Postgre, rack-mini-profiler (the latest), sequel-rails 1.0.1.

I'm trying to figure out why my application is slow. Here's what I found.

I made a simple query to get posts using

@posts = Post.all

and got ~150.

And I have a partial to render it. This is simplified:

<% @posts.each do |post| %>
  <%= post.title %>
  <%= post.body %>
<% end %>

When I looked in rack-mini-profiler's log, I saw

SELECT NULL; 

requests which slows rendering, but if I change the . method to :[]:

<% @posts.each do |post| %>
  <%= post[:title] %>
  <%= post[:body] %>
<% end %>

the NULL queries disappear and rendering becomes much faster. (Both title and body are fields in my DB, nothing else.)

Why? Is this the thing I need to know about Sequel? I should not use instance.method in the future in that cases, or am I missing something?

When I tried the request from the console, I got:

> Post.first
D, [2018-06-09T22:46:41.277269 #96772] DEBUG -- : (0.001008s) SELECT NULL
D, [2018-06-09T22:46:41.277952 #96772] DEBUG -- : (0.000362s) SELECT * FROM "posts" LIMIT 1

What is the first phantom request? How can I debug and remove it?


Solution

  • Finally, I found the problem:

    it was a :connection_validator plugin.

    Bypass this plugin unless you really-really need it!