Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use quoted table name #230

Merged
merged 1 commit into from
Sep 22, 2023
Merged

Use quoted table name #230

merged 1 commit into from
Sep 22, 2023

Conversation

tagliala
Copy link
Member

Retrieving quoted table name directly from the class instead of re-calculating it from the connection is an order of magnitude faster

class User < ActiveRecord::Base
  def self.qtn
    quoted_table_name
  end

  def self.cqtn
    connection.quote_table_name(table_name)
  end
end

Ruby 2.2 / Rails 5.0:

            User.qtn:  3003860.8 i/s
           User.cqtn:   259818.5 i/s - 11.56x  slower

Ruby 3.2 / Rails 7.0:

Comparison:
            User.qtn: 13504395.2 i/s
           User.cqtn:   517189.1 i/s - 26.11x  slower

Retrieving quoted table name directly from the class instead of
re-calculating it from the connection is an order of magnitude faster.

This change only enforces best practices because the value is memoized
and so it does not affect performance

```rb
class User < ActiveRecord::Base
  def self.qtn
    quoted_table_name
  end

  def self.cqtn
    connection.quote_table_name(table_name)
  end
end
```

Ruby 2.2 / Rails 5.0:
```
            User.qtn:  3003860.8 i/s
           User.cqtn:   259818.5 i/s - 11.56x  slower
```

Ruby 3.2 / Rails 7.0:

```
Comparison:
            User.qtn: 13504395.2 i/s
           User.cqtn:   517189.1 i/s - 26.11x  slower
```
@tagliala tagliala force-pushed the chore/use-quoted-table-name branch from 4c6ca4f to 3daf3db Compare September 22, 2023 09:06
@tagliala tagliala merged commit 7362608 into master Sep 22, 2023
@tagliala tagliala deleted the chore/use-quoted-table-name branch September 22, 2023 09:10
@tagliala
Copy link
Member Author

Looks like the timeline is one of the most expensive methods

Compared to the commit before optimizations started:

Before:

#timeline                        3.122635   0.367694   3.490329 (  8.078685)

After:

#timeline                        3.054792   0.358099   3.412891 (  7.965729)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant