           |> preprocess_fn.()
         end)
         |> ordering_fn.()
+        |> limit_fn.()
 
         {length(result), result}
       _ ->
-        {length(records), nil}
+        {min(limit, length(records)), nil}
     end
   end
 

  """
  def prepare(operation, %Ecto.Query{from: {table, schema}, order_bys: order_bys, limit: limit} = query) do
    ordering_fn = order_bys |> Ordering.get_ordering_fn()
    context = table |> Context.new(schema)
    limit = limit |> get_limit()
    {:nocache, {operation, query, limit, context, ordering_fn}}
  end

  @doc """
  Perform `mnesia:select` on prepared query and convert the results to Ecto Schema.
  """
  def execute(_repo, %{sources: {{table, _schema}}, fields: fields, take: _take},
                      {:nocache, {:all, %Ecto.Query{} = query, limit, context, ordering_fn}},
                      sources, preprocess, _opts) do
    context = context |> Context.assign_query(query, sources)
    match_spec = Context.get_match_spec(context)
    Logger.debug("Selecting all records by match specification `#{inspect match_spec}` with limit `#{inspect limit}`")

    result = table
    |> Table.select(match_spec, limit)
    |> Enum.map(&process_row(&1, preprocess, fields))
    |> ordering_fn.()

    {length(result), result}
  end

        {:ok, result} = Table.update(table, List.first(record), update)
        result
      end)
      |> return_all(ordering_fn, preprocess_fn, {limit, limit_fn}, opts)
    end)
  end

  # Constructs return for `*_all` methods.
  defp return_all(records, ordering_fn, preprocess_fn, {limit, limit_fn}, opts) do
    case Keyword.get(opts, :returning) do
      true ->
        result = records
