I have two schemas like this:
schema "projects" do
field :title, :string
field :is_shared, :boolean
timestamps()
end
schema "cards" do
field :title, :string
field :type, :string
belongs_to(:project, Project)
end
now I want to find the cards of the project that not shared.
I use this query:
from(c0 in Card,
join:
p1 in subquery(
from p0 in Project,
where: p0.is_shared == false,
select: p0.id
),
on: c0.project_id != p1.id,
where: c0.type == ^"golden"
)
but It gives me a Postgrex error:
Postgrex.Protocol (#PID<0.494.0>) disconnected: ** (DBConnection.ConnectionError) client #PID<0.527.0> timed out because it queued and checked out the connection for longer than 15000ms
But when I use the raw sql that debug print, there’s no problem.
SELECT c0."id",
c0."title",
c0."inserted_at",
c0."updated_at"
FROM "cards" AS c0
INNER JOIN (SELECT p0."id" AS "id" FROM "projects" AS p0 WHERE (p0."is_shared" = FALSE)) AS s1
ON c0."project_id" != s1."id"
WHERE (c0."type" = 'golden')
the raw sql above is very fast. It only cost 100-200 ms.