Ecto.Adapters.SQL.explain
You're seeing just the function
explain
, go back to Ecto.Adapters.SQL module for more information.
Specs
explain( pid() | Ecto.Repo.t() | Ecto.Adapter.adapter_meta(), :all | :update_all | :delete_all, Ecto.Queryable.t(), opts :: Keyword.t() ) :: String.t() | Exception.t()
Executes an EXPLAIN statement or similar for the given query according to its kind and the adapter in the given repository.
Examples
# Postgres
iex> Ecto.Adapters.SQL.explain(Repo, :all, Post)
"Seq Scan on posts p0 (cost=0.00..12.12 rows=1 width=443)"
# MySQL
iex> Ecto.Adapters.SQL.explain(Repo, :all, from(p in Post, where: p.title == "title")) |> IO.puts()
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | p0 | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.0 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
# Shared opts
iex> Ecto.Adapters.SQL.explain(Repo, :all, Post, analyze: true, timeout: 20_000)
"Seq Scan on posts p0 (cost=0.00..11.70 rows=170 width=443) (actual time=0.013..0.013 rows=0 loops=1)\nPlanning Time: 0.031 ms\nExecution Time: 0.021 ms"
It's safe to execute it for updates and deletes, no data change will be committed:
iex> Ecto.Adapters.SQL.explain(Repo, :update_all, from(p in Post, update: [set: [title: "new title"]]))
"Update on posts p0 (cost=0.00..11.70 rows=170 width=449)\n -> Seq Scan on posts p0 (cost=0.00..11.70 rows=170 width=449)"
This function is also available under the repository with name explain
:
iex> Repo.explain(:all, from(p in Post, where: p.title == "title"))
"Seq Scan on posts p0 (cost=0.00..12.12 rows=1 width=443)\n Filter: ((title)::text = 'title'::text)"
Options
Built-in adapters support passing opts
to the EXPLAIN statement according to the following:
Adapter | Supported opts |
---|---|
Postgrex | analyze , verbose , costs , settings , buffers , timing , summary |
MyXQL | None |
Postgrex: Check PostgreSQL doc for version compatibility.
MyXQL: EXTENDED
and PARTITIONS
opts were deprecated and are enabled by default.
Also note that:
- Currently
:map
,:yaml
, and:text
format options are supported for PostgreSQL.:map
is the deserialized JSON encoding. The last two options return the result as a string. - Any other value passed to
opts
will be forwarded to the underlying adapter query function, including Repo shared options such as:timeout
; - Non built-in adapters may have specific behavior and you should consult their own documentation.