[tracker/wip/carlosg/zero-or-more-fix: 3/3] libtracker-data: Hint access cost of "triples" virtual table usage




commit 4a08ea0f9babad4005655feb7bd4a12e36f37f5f
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Nov 7 15:26:44 2021 +0100

    libtracker-data: Hint access cost of "triples" virtual table usage
    
    We are not giving this information back to SQLite in the virtual table
    xBestIndex method, so it is left to choose the best strategy to access
    the "triples" table without further information, which might come out
    wrong.
    
    Hint SQLite about the estimated cost of accessing this table with the
    suggested index, so that it always makes the best choice (this happened
    to be the case at the time of doing
    https://gitlab.gnome.org/GNOME/tracker-miners/-/merge_requests/334, but
    seemed to regress substantially at some point).
    
    Accessing the "triples" table works best by providing multiple columns
    to match of graph/subject/predicate, so the combination of all values
    must get the lowest estimated cost. Likewise, having no columns to match
    represents the worst estimated cost. These columns are weighted though,
    as providing each of those represents a different reduction in the
    amount of disk accesses necessary to return the requested data. These
    weights are away by orders of magnitude, in order to ensure SQLite is
    strongly hinted of the preference order.
    
    Providing this estimated cost, we can trust that SQLite will use the
    best choice available. This should significantly improve (back again)
    performance when using the "triples" table in some situations.
    "tracker3 info <file>" is one of those, and a rather popular one.

 src/libtracker-data/tracker-vtab-triples.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
---
diff --git a/src/libtracker-data/tracker-vtab-triples.c b/src/libtracker-data/tracker-vtab-triples.c
index 5aa079ebe..d91a2c66e 100644
--- a/src/libtracker-data/tracker-vtab-triples.c
+++ b/src/libtracker-data/tracker-vtab-triples.c
@@ -55,6 +55,12 @@ enum {
        IDX_MATCH_PREDICATE_NEG = 1 << 5,
 };
 
+enum {
+       WEIGHT_GRAPH     = 1 << 10,
+       WEIGHT_PREDICATE = 1 << 20,
+       WEIGHT_SUBJECT   = 1 << 30,
+};
+
 typedef struct {
        sqlite3 *db;
        TrackerOntologies *ontologies;
@@ -169,6 +175,7 @@ triples_best_index (sqlite3_vtab       *vtab,
 {
        gboolean order_by_consumed = FALSE;
        int i, argv_idx = 1, idx = 0;
+       int cost_divisor = 1;
        char *idx_str;
 
        idx_str = sqlite3_malloc (sizeof (char) * N_COLS);
@@ -221,12 +228,20 @@ triples_best_index (sqlite3_vtab       *vtab,
                info->aConstraintUsage[i].argvIndex = argv_idx;
                info->aConstraintUsage[i].omit = FALSE;
                argv_idx++;
+
+               if (info->aConstraint[i].iColumn == COL_SUBJECT)
+                       cost_divisor |= WEIGHT_SUBJECT;
+               else if (info->aConstraint[i].iColumn == COL_PREDICATE)
+                       cost_divisor |= WEIGHT_PREDICATE;
+               else if (info->aConstraint[i].iColumn == COL_GRAPH)
+                       cost_divisor |= WEIGHT_GRAPH;
        }
 
        info->idxNum = idx;
        info->orderByConsumed = order_by_consumed;
        info->idxStr = idx_str;
        info->needToFreeIdxStr = TRUE;
+       info->estimatedCost = info->estimatedCost / cost_divisor;
 
        return SQLITE_OK;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]