Changeset: a4eb1da268a6 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/a4eb1da268a6
Modified Files:
        gdk/gdk_join.c
Branch: Dec2025
Log Message:

When one side of a hash join might cause serious thrashing, favor the other.
Thanks, Roberto.


diffs (38 lines):

diff --git a/gdk/gdk_join.c b/gdk/gdk_join.c
--- a/gdk/gdk_join.c
+++ b/gdk/gdk_join.c
@@ -3677,6 +3677,20 @@ joincost(BAT *r, BUN lcount, struct cand
                if (rhash) {
                        /* average chain length */
                        rcost *= (double) cnt / nheads;
+                       /* If the hash exists, but the working set
+                        * exceeds physical RAM, random-access probes
+                        * will cause massive page fault thrashing.
+                        * Multiply the probe cost by an I/O latency
+                        * factor to encourage swapping to a sequential
+                        * scan instead. */
+                       if (!GDKinmemory(r->theap->farmid) &&
+                           (size_t)cnt * ATOMsize(r->ttype) + (r->tvheap ? 
r->tvheap->free : 0) > GDK_mem_maxsize / 4) {
+                               /* Disk random access is ~100x to 1000x
+                                * slower than RAM.  A 100x penalty
+                                * forces the optimizer to treat these
+                                * probes as highly toxic. */
+                               rcost *= 100.0;
+                       }
                } else if ((parent = VIEWtparent(r)) != 0 &&
                           (b = BATdescriptor(parent)) != NULL) {
                        if (BATcheckhash(b)) {
@@ -3705,7 +3719,12 @@ joincost(BAT *r, BUN lcount, struct cand
                        /* only count the cost of creating the hash for
                         * non-persistent bats */
                        MT_lock_set(&r->theaplock);
-                       if (r->batRole != PERSISTENT /* || r->theap->dirty */ 
|| GDKinmemory(r->theap->farmid))
+                       /* If the BAT is persistent but so large that
+                        * building a hash might thrash memory, consider
+                        * it very expensive, so encourage choosing a
+                        * linear scan of this side instead. */
+                       if (r->batRole != PERSISTENT /* || r->theap->dirty */ 
|| GDKinmemory(r->theap->farmid) ||
+                           (size_t)cnt * ATOMsize(r->ttype) + (r->tvheap ? 
r->tvheap->free : 0) > GDK_mem_maxsize / 4)
                                rcost += cnt * 2.0;
                        MT_lock_unset(&r->theaplock);
                }
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to