|
1 | 1 | import SQL
|
| 2 | +import Ecto.Query |
| 3 | +defmodule SQL.Repo do |
| 4 | + use Ecto.Repo, otp_app: :sql, adapter: Ecto.Adapters.Postgres |
| 5 | +end |
| 6 | +Application.put_env(:sql, :ecto_repos, [SQL.Repo]) |
| 7 | +Application.put_env(:sql, SQL.Repo, username: "postgres", password: "postgres", hostname: "localhost", database: "sql_test#{System.get_env("MIX_TEST_PARTITION")}", pool: Ecto.Adapters.SQL.Sandbox, pool_size: 10) |
| 8 | +SQL.Repo.start_link() |
| 9 | + |
2 | 10 | range = 1..10_000
|
| 11 | +sql = ~SQL[with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)] |
| 12 | +query = "temp" |> recursive_ctes(true) |> with_cte("temp", as: ^union_all(select("temp", [t], %{n: 0, fact: 1}), ^where(select("temp", [t], [t.n+1, t.n+1*t.fact]), [t], t.n < 9))) |> select([t], [t.n]) |
3 | 13 | Benchee.run(
|
4 | 14 | %{
|
5 |
| - "to_stirng" => fn -> for _ <- range, do: to_string(~SQL[with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)]) end, |
6 |
| - "to_sql" => fn -> for _ <- range, do: SQL.to_sql(~SQL[with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)]) end, |
7 |
| - "inspect" => fn -> for _ <- range, do: inspect(~SQL[with recursive temp (n, fact) as (select 0, 1 union all select n+1, (n+1)*fact from temp where n < 9)]) end |
| 15 | + "to_stirng" => fn -> for _ <- range, do: to_string(sql) end, |
| 16 | + "to_sql" => fn -> for _ <- range, do: SQL.to_sql(sql) end, |
| 17 | + "inspect" => fn -> for _ <- range, do: inspect(sql) end, |
| 18 | + "Ecto.Repo.to_sql" => fn -> for _ <- range, do: SQL.Repo.to_sql(:all, query) end |
8 | 19 | },
|
9 | 20 | time: 10,
|
10 | 21 | memory_time: 2
|
|
0 commit comments