Speed up index page queries

pull/2/head
Doc Ritezel 7 years ago
parent da0c9db07a
commit 876de9ae5a
  1. 18
      lib/explorer_web/controllers/block_controller.ex
  2. 2
      lib/explorer_web/controllers/chain_controller.ex
  3. 20
      lib/explorer_web/controllers/transaction_controller.ex
  4. 2
      test/explorer_web/controllers/transaction_controller_test.exs

@ -1,16 +1,18 @@
defmodule ExplorerWeb.BlockController do
use ExplorerWeb, :controller
import Ecto.Query
alias Explorer.Block
alias Explorer.Repo
alias Explorer.BlockForm
alias Explorer.Repo
import Ecto.Query
use ExplorerWeb, :controller
def index(conn, params) do
blocks = Block
|> order_by(desc: :number)
|> preload(:transactions)
|> Repo.paginate(params)
render(conn, "index.html", blocks: blocks)
blocks = from b in Block,
order_by: [desc: b.number],
preload: :transactions
render(conn, "index.html", blocks: Repo.paginate(blocks, params))
end
def show(conn, params) do

@ -2,8 +2,6 @@ defmodule ExplorerWeb.ChainController do
alias Explorer.Block
alias Explorer.Transaction
alias Explorer.Repo
alias Explorer.BlockForm
alias Explorer.TransactionForm
import Ecto.Query

@ -1,16 +1,20 @@
defmodule ExplorerWeb.TransactionController do
use ExplorerWeb, :controller
import Ecto.Query
alias Explorer.Transaction
alias Explorer.Block
alias Explorer.Repo
alias Explorer.Transaction
alias Explorer.TransactionForm
import Ecto.Query
use ExplorerWeb, :controller
def index(conn, params) do
transactions = Transaction
|> order_by(desc: :inserted_at)
|> preload(:block)
|> Repo.paginate(params)
render(conn, "index.html", transactions: transactions)
transactions = from t in Transaction,
join: b in Block, on: b.id == t.block_id,
order_by: [desc: b.number],
preload: :block
render(conn, "index.html", transactions: Repo.paginate(transactions, params))
end
def show(conn, params) do

@ -13,7 +13,7 @@ defmodule ExplorerWeb.TransactionControllerTest do
test "returns all blocks", %{conn: conn} do
transaction_ids = insert_list(4, :transaction) |> Enum.map(fn (transaction) -> transaction.id end)
conn = get(conn, "/en/transactions")
assert conn.assigns.transactions |> Enum.map(fn (transaction) -> transaction.id end) == transaction_ids
assert conn.assigns.transactions |> Enum.reverse |> Enum.map(fn (transaction) -> transaction.id end) == transaction_ids
end
end
end

Loading…
Cancel
Save