         guard let dbh = try? db.prepare(query) else {
             return AnyIterator { return nil }
         }
-        let quads = dbh.compactMap { (row) -> Quad? in
-            guard let s = term(for: row[subjColumn]) else { return nil }
-            guard let p = term(for: row[subjColumn]) else { return nil }
-            guard let o = term(for: row[subjColumn]) else { return nil }
-            guard let g = term(for: row[subjColumn]) else { return nil }
+        let quads = dbh.lazy.compactMap { (row) -> Quad? in
+            guard let s = self.term(for: row[self.subjColumn]) else { return nil }
+            guard let p = self.term(for: row[self.predColumn]) else { return nil }
+            guard let o = self.term(for: row[self.objColumn]) else { return nil }
+            guard let g = self.term(for: row[self.graphColumn]) else { return nil }
             return Quad(subject: s, predicate: p, object: o, graph: g)
         }
         return AnyIterator(quads.makeIterator())

                    guard let lang = row[termLangColumn] else {
                        return nil
                    }
                    return Term(value: value, type: .language(lang))
                } else {
                    return Term(value: value, type: .datatype(TermDataType(stringLiteral: dt)))
                }
            }
        } catch {}

    }
    
    var db: Connection
    var t2icache: LRUCache<Term, TermID>
    var i2tcache: LRUCache<TermID, Term>

    public init(filename: String, initialize: Bool = false) throws {
        db = try Connection(filename)
        i2tcache = LRUCache(capacity: 1024)
        t2icache = LRUCache(capacity: 1024)
        if initialize {
            try initializeTables()
        }
