Prepare to optimize

Remember that optimizing Ecto begins with optimizing our database before we make any changes to our Elixir code to speed things up. If our tables are missing critical indexes, or we have to perform excessive numbers of joins due to a suboptimal table setup, there’s not much that Ecto can do to help. Database optimization is a much bigger topic than we can cover here, but the documentation for your database is a good place to start learning how to get your data into top shape.

We’ll also want to make sure that we know exactly where our application is slow. We might think we have an idea, but we won’t know for sure unless we’ve looked at some metrics. Metrics are a good way to determine the status of our application. Ecto’s built-in adapters use the Telemetry library to deliver metrics. We recommend that you look at the “Telemetry Events” section of the Ecto.Repo documentation to learn how to use it. Comparing metrics before and after we make changes is the best way to determine that we’re moving in the right direction.

Optimize queries

One way to optimize our application is to change how it queries data. As a first step, we can consider two actors—bandwidth and latency.

Get hands-on with 1200+ tech skills courses.