         indexed (index aval)]
     (index-flatten1 indexed)))
 
+(defmethod exec :rmap-lookup [{:keys [rmap]} {:keys [id]}]
+  (get rmap id))
+
 (defmethod exec :index-multilookup [db {:keys [attr in]}]
   (let [index    (index-get db {:on attr})
         indexeds (map index in)]
     (index-flatten indexeds)))
 
+(defmethod exec :rmap-multilookup [{:keys [rmap]} {:keys [ids]}]
+  (map rmap ids))
+
 (def- one-sided-op-fns
   {:< < :<= <= :> > :>= >=})
 
 (def- two-sided-op-fns
   {:>< [> <] :>=< [>= <] :><= [> <=] :>=<= [>= <=]})
 
+(defn- index-range [index dir op ival]
+  (let [subseq-fn (if (= dir :asc) subseq rsubseq)
+        pairs     (if-let [op-fn (one-sided-op-fns op)]
+                    (subseq-fn index op-fn ival)
+                    (let [[op-fn1 op-fn2] (two-sided-op-fns op)
+                          [ival1  ival2]  ival]
+                      (subseq-fn index op-fn1 ival1 op-fn2 ival2)))]
+    (map val pairs)))
+
 (defmethod exec :index-range [db {:keys [attr dir op aval]}]
-  (let [index     (index-get db {:on attr})
-        subseq-fn (if (= dir :asc) subseq rsubseq)
-        pairs     (if-let [op-fn (one-sided-op-fns op)]
-                    (subseq-fn index op-fn aval)
-                    (let [[op-fn1 op-fn2] (two-sided-op-fns op)
-                          [aval1  aval2]   aval]
-                      (subseq-fn index op-fn1 aval1 op-fn2 aval2)))
-        indexeds  (map val pairs)]
-    (index-flatten indexeds)))
+  (let [index     (index-get db {:on attr})]
+    (index-flatten (index-range index dir op aval))))
+
+(defmethod exec :rmap-range [{:keys [rmap]} {:keys [dir op idval]}]
+  (index-range rmap dir op idval))
 
 (defmethod exec :union [db {:keys [source]}]
   (union-stream (map #(exec db %) source)))

      ; when reasonable index and order -> may be suboptimal, but reasonable
      (= := op)
        (let [[attr aval] wrest]
          (if (index-get db {:on attr})
            {:action  :index-lookup
             :attr    attr
             :aval    aval
             :ordered false}
            (root-filter-plan db where order)))

      ; as above
      (= :in op)
        (let [[attr in] wrest]
          (if (index-get db {:on attr})
            {:action  :index-multilookup
             :attr    attr
             :in      in
             :ordered false}
            (root-filter-plan db where order)))

      ; hmm
      (range-op? op)
        (let [[attr aval] wrest]
          (if (index-get db {:on attr})
            (let [[oattr odir] order
                  ordered      (= attr oattr)]
              {:action  :index-range
               :attr    attr
               :dir     (if ordered oattr)
               :op      op
               :aval    aval
               :ordered ordered})
            (root-filter-plan db where order)))

      ; when no indexes use root-filter-plan -> optimal
      ; when some reasonable, most selective first index -> probably optimal

    (fn [spec index] (index-delete index spec record))))

(defn- root-plan [db [attr dir :as order]]
  (cond
    (= attr :id)
      {:action  :rmap-scan
       :dir     dir
       :ordered true}
    (and order (index-get db {:on attr}))
      {:action  :index-scan
       :attr    attr
       :dir     dir
       :ordered true}
    :no-index
      {:action  :db-scan
       :ordered false}))

(defn- filter-plan [source where]
  (if-not where
