Skip to content

Commit

Permalink
Use subqueries for counting total entries
Browse files Browse the repository at this point in the history
(cherry picked from commit 62681ff)
  • Loading branch information
drewolson committed Jan 16, 2017
1 parent 666fcdb commit c6d1c45
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions lib/scrivener/paginater/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,10 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
end

defp total_entries(query, repo) do
primary_key =
query.from
|> elem(1)
|> apply(:__schema__, [:primary_key])
|> hd

total_entries =
query
|> exclude(:order_by)
|> exclude(:preload)
|> exclude(:select)
|> exclude(:group_by)
|> select([m], count(field(m, ^primary_key), :distinct))
|> subquery
|> select(count("*"))
|> repo.one

total_entries || 0
Expand Down

0 comments on commit c6d1c45

Please sign in to comment.