   """
   def prepare(operation, %Ecto.Query{from: {table, schema}, order_bys: order_bys, limit: limit} = query) do
     ordering_fn = order_bys |> Ordering.get_ordering_fn()
+    limit = limit |> get_limit()
+    limit_fn = if limit == nil, do: &(&1), else: &Enum.take(&1, limit)
     context = table |> Context.new(schema)
-    limit = limit |> get_limit()
-    {:nocache, {operation, query, limit, context, ordering_fn}}
+    {:nocache, {operation, query, {limit, limit_fn}, 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}},
+                      {:nocache, {:all, %Ecto.Query{} = query, {limit, limit_fn}, 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}`")
+    Logger.debug("Selecting all records by match specification `#{inspect match_spec}` with limit #{inspect limit}")
 
     result = table
-    |> Table.select(match_spec, limit)
+    |> Table.select(match_spec)
     |> Enum.map(&process_row(&1, preprocess, fields))
     |> ordering_fn.()
+    |> limit_fn.()
 
     {length(result), result}
   end

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

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

          |> preprocess_fn.()
        end)
        |> ordering_fn.()
        |> limit_fn.()

        {length(result), result}
      _ ->
        {min(limit, length(records)), nil}
    end
  end

