         guard let dbh = try? db.prepare(query) else {
             return AnyIterator { return nil }
         }
-        let results = dbh.compactMap { (row) -> TermResult? in
+        let results = dbh.lazy.compactMap { (row) -> TermResult? in
             var bindings = [String: Term]()
             for (name, cols) in mapping {
                 if cols.count == 1 {
                     let col = cols.first!
                     let id = row[col]
-                    guard let t = term(for: id) else { return nil }
+                    guard let t = self.term(for: id) else { return nil }
                     bindings[name] = t
                 } else {
                     let ids = cols.map { row[$0] }
-                    let terms = ids.compactMap { term(for: $0) }
+                    let terms = ids.compactMap { self.term(for: $0) }
                     guard terms.count == cols.count else { return nil }
                     if let t = terms.first {
                         guard terms.allSatisfy({ t == $0 }) else {

            let value = row[termValueColumn]
            switch type {
            case .iri:
                return Term(iri: value)
            case .blank:
                return Term(value: value, type: .blank)
            case .literal:
                guard let datatypeID = row[termDatatypeColumn] else {
                    return nil

//                print("------------")
                return nil
            }
            let i = row[idColumn]
            t2icache[term] = i
            return i
        } catch {}
        return nil
    }
    
    internal func term(for id: TermID) -> Term? {
        if let t = i2tcache[id] {
            return t
        }
        
        let query = termsTable.select(termsTable[*]).filter(idColumn == id)
        do {
            guard let row = try db.pluck(query) else {
