cd-slash

cd-slash

Ecto not using indexes in SQLite causing slow query

I have a relatively large table (~350k rows and ~50 columns) in an existing GADM database that I’m connecting to with Ecto.

I’ve created indexes on the table with the migration below:

defmodule GADM.Migrations.AddIndexes do
  use Ecto.Migration

  def up do
    create(index(:gadm_410, [:gid_0]))
    create(index(:gadm_410, [:gid_1]))
    create(index(:gadm_410, [:gid_2]))
    create(index(:gadm_410, [:gid_3]))
    create(index(:gadm_410, [:gid_4]))
    create(index(:gadm_410, [:gid_5]))
    create(index(:gadm_410, [:country]))
  end

  def down do
    drop(index(:gadm_410, [:gid_0]))
    drop(index(:gadm_410, [:gid_1]))
    drop(index(:gadm_410, [:gid_2]))
    drop(index(:gadm_410, [:gid_3]))
    drop(index(:gadm_410, [:gid_4]))
    drop(index(:gadm_410, [:gid_5]))
    drop(index(:gadm_410, [:country]))
  end
end

Ecto.Migrator.down(GADMRepo, 0, GADM.Migrations.AddIndexes)
Ecto.Migrator.up(GADMRepo, 0, GADM.Migrations.AddIndexes)

I’m then making a relatively simple query using Ecto:

import Ecto.Query

id_field_name = String.to_atom("gid_3")
child_id_field_name = String.to_atom("gid_4")
name_field_name = String.to_atom("name_3")

GADM.Region
|> where([r], field(r, ^id_field_name) != "")
|> where([r], field(r, ^child_id_field_name) == "")
|> select([r], %{
  gid: field(r, ^id_field_name),
  name: field(r, ^name_field_name)
})
|> distinct(true)
|> GADMRepo.all()

This works, but is very slow:


10:26:39.908 [debug] QUERY OK source="gadm_410" db=2820.3ms queue=0.1ms idle=1446.3ms
SELECT DISTINCT g0."gid_3", g0."name_3" FROM "gadm_410" AS g0 WHERE (g0."gid_3" != '') AND (g0."gid_4" = '') []
↳ :elixir.eval_external_handler/3, at: src/elixir.erl:386

In contrast, when I paste exactly the same SQL as the output shows into SQLIte Studio, it executes in ~1ms, which is what I’d have anticipated with indexes available for all of the columns I’m working with.

To diagnose, firstly I’ve tried to use explain but can’t see any straightforward way to get Ecto to explain a query like this. Is there a function for that?

Secondly, since the indexes are clearly being built and then used when I execute the query elsewhere, why would Ecto not be using them? Even without the explain I can intuit that this is what’s happening as deleting the indexes yields almost identical execution times, so it seems they’re not having any effect.

Most Liked

ruslandoga

ruslandoga

:wave: @cd-slash

Just a guess: gid_4 might be picked because your query uses the equality operator with it, and on gid_3 it uses != which probably gets ignored.

Possible workarounds:

LostKobrakai

LostKobrakai

Ecto has no influence on the db using an index or not. You’d want to make sure the queries are indeed the same (including using parameters and prepared queries or not).

cd-slash

cd-slash

Ecto.Adapters.SQL — Ecto SQL v3.12.0

Thanks, I’d tried that but couldn’t get it working for SQLite, seemed to only be for Postgres / MySQL?

Ecto has no influence on the db using an index or not. You’d want to make sure the queries are indeed the same (including using parameters and prepared queries or not).

Noted, I’ll try to ensure the queries are identical and continue trying to find a way to profile the query from Ecto

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement