         "((localVisitDate > \(sixMonthsAgo)) OR (remoteVisitDate > \(sixMonthsAgo)))" +    // Exclude really old items.
         ") ORDER BY frecency DESC" +
         " LIMIT 1000"                                 // Don't even look at a huge set. This avoids work.
-
+        
         // Next: merge by domain and sum frecency, ordering by that sum and reducing to a (typically much lower) limit.
         let historySQL =
         "SELECT historyID, url, title, guid, domain_id, domain" +

                                                     groupClause: String = "GROUP BY historyID ",
                                                     whereData: String? = nil,
                                                     includeIcon: Bool = true) -> Deferred<Maybe<Cursor<Site>>> {
        let localFrecencySQL = getLocalFrecencySQL()
        let remoteFrecencySQL = getRemoteFrecencySQL()
        let sixMonthsInMicroseconds: UInt64 = 15_724_800_000_000      // 182 * 1000 * 1000 * 60 * 60 * 24

        groupClause + " " +
        "ORDER BY frecencies DESC " +
        "LIMIT \(limit) "
        
        // Finally: join this small list to the favicon data.
        if includeIcon {
            // We select the history items then immediately join to get the largest icon.
            // We do this so that we limit and filter *before* joining against icons.
            let sql = "SELECT" +
            " historyID, url, title, guid, domain_id, domain" +
            ", localVisitDate, remoteVisitDate, localVisitCount, remoteVisitCount" +
            ", iconID, iconURL, iconDate, iconType, iconWidth, frecencies" +
            " FROM (\(historySQL)) LEFT OUTER JOIN " +
            "view_history_id_favicon ON historyID = view_history_id_favicon.id"
            return (sql, args)
        }

        return (historySQL, args)
    }
}

