8
.
05
.
2024
9
.
09
.
2022
Ruby on Rails
Backend

What you lost when you used structure.sql instead of schema.rb in your Rails project

Mariusz Kozieł
Chief Executive Officer

Both schema formats have their own pros and cons. I didn’t want to write when and why we should use structure.sql or schema.rb but I might do it in the next article. You can read more in the official documentation. In this article I would like to show you a short production story.

We have jumped into a new project which had database performance problems. It was easy to notice that client's team had struggled with it also. For example, they made database replication to redirect the most time-consuming queries to second instance. Numerous requests and millions of records in tables were causing a real problem.

The client’s team had various ideas on how to solve the problem. We discussed about:

  • scaling DB horizontally
  • materialized SQL views
  • extract the most crucial part as a microservice

But we decided to start from the beginning and we started digging. New Relic (APM - Application Performance Monitoring tool) was our best friend. We have detected the 5 most time-consuming queries and investigated them one by one.

I would like to show one of them:

performance_before_changes

Query which is run so often (46 times per minute) can't be that slow. Let’s investigate why? SQL explain and other checks proved that everything happened because of one line


validates :column_id, uniqueness: { scope: :other_column_id }

and lack of unique index on those columns in the database.

By adding index:


def change
    add_index :table, [:column_id, :other_column_id], unique: true
end

We have achieved a nice result. Average query time fell down from 731 to 1.5 ms which solves one of the biggest DB problem.

performance_after_changes

Success story but what the hell is the advantage of using schema.rb instead of structure.sql as our schema format?

If schema.rb had been used, mentioned issue would not have existed. Thanks to Rubocop-rails which is used in the project. We are enthusiasts of this tool :)

We might have learned about this issue earlier or even previous team could catch it immediately after problematic validation was added. Rubocop Rails/UniqueValidationWithoutIndex check highlight it:


app/models/example.rb:10:3: C: Rails/UniqueValidationWithoutIndex: Uniqueness validation should have a unique index on the database column.
  validates :column_id, uniqueness: { scope: :other_column_id }
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Rubocop checks indexes in the schema.rb file so there is no verification if there is no schema.rb file. Probably there are other tools that can detect it as fast as Rubocop. Let me know if you know any helpful tools or tips that allow detecting database optimization problems

Mariusz Kozieł
Chief Executive Officer

Check my Twitter

Check my Linkedin

Did you like it? 

Sign up To VIsuality newsletter

READ ALSO

Hot topic: Progressive Web Apps instead of native mobile apps

11
.
06
.
2025
Michał Krochecki
Ruby on Rails
Business
Backend
Frontend

Docker hosted on Jelastic

11
.
06
.
2025
Marcin Prokop
Ruby on Rails
Backend
Tutorial

All the pieces matter - Visuality DNA

11
.
06
.
2025
Michał Piórkowski
Visuality
HR

Tech conferences 2018/2019 you definitely should attend

11
.
06
.
2025
Michał Krochecki
Conferences

Visuality Poznań is here!

11
.
06
.
2025
Michał Piórkowski
Visuality
Business
HR

Why we chose Ruby on Rails and React.js for our main technologies? (FAQ).

11
.
06
.
2025
Michał Krochecki
Ruby on Rails
Backend
Frontend
Visuality

Branding: How to style your Jira?

11
.
06
.
2025
Lukasz Jackiewicz
Tutorial
Design
Project Management

How to start your UX/UI designer career

11
.
06
.
2025
Bartłomiej Bednarski
Design
Tutorial
HR

WebUSB - Bridge between USB devices and web browsers

11
.
06
.
2025
Burak Aybar
Ruby on Rails
Frontend
Backend
Tutorial

Visuality comes to town - this time it's Poznań

11
.
06
.
2025
Michał Piórkowski
Visuality
HR

How to choose a software house.

11
.
06
.
2025
Michał Piórkowski
Ruby on Rails
Business
Visuality

CSS Modules in Rails

11
.
06
.
2025
Adam Król
Ruby on Rails
Tutorial
Backend
Frontend

JSON API versus the NIH syndrome

11
.
06
.
2025
Nadia Miętkiewicz
Backend
Frontend
Tutorial

From Idea to Concept

11
.
06
.
2025
Michał Krochecki
Ruby on Rails
Business
Startups

Styling React Components

11
.
06
.
2025
Umit Naimian
Ruby on Rails
Frontend
Tutorial

How good design can help your business grow

11
.
06
.
2025
Lukasz Jackiewicz
Design
Business
Marketing

TODO not. Do, or do not.

11
.
06
.
2025
Stanisław Zawadzki
Ruby on Rails
Software

CS Lessons #003: Density map in three ways

11
.
06
.
2025
Michał Młoźniak
Ruby
Backend
Tutorial
Software

Clean code for the win

11
.
06
.
2025
Michał Piórkowski
Ruby on Rails
Backend
Frontend
Business

Crowd-operated Christmas Lights

11
.
06
.
2025
Nadia Miętkiewicz
Ruby on Rails
Backend

How to startup and be mature about it

11
.
06
.
2025
Rafał Maliszewski
Ruby on Rails
Startups
Business

A journey of a thousand miles begins with workshops

11
.
06
.
2025
Michał Piórkowski
Software
HR

CS Lessons #002: Data structures

11
.
06
.
2025
Michał Młoźniak
Ruby
Software