Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aea6e94

Browse files
author
Nikita Glukhov
committed
Optimize jsonSelectivityContains() by using prefix in constructed path string
1 parent 822a686 commit aea6e94

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/backend/utils/adt/jsonb_selfuncs.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,13 +1108,13 @@ static void
11081108
jsonAccumulateSubPathSelectivity(Selectivity subpath_abs_sel,
11091109
Selectivity path_freq,
11101110
Selectivity *path_relative_sel,
1111-
StringInfo pathstr,
1111+
bool is_array_accessor,
11121112
JsonPathStats path_stats)
11131113
{
11141114
Selectivity sel = subpath_abs_sel / path_freq; /* relative selectivity */
11151115

11161116
/* XXX Try to take into account array length */
1117-
if (pathstr->data[pathstr->len - 1] == ']')
1117+
if (is_array_accessor)
11181118
sel = 1.0 - pow(1.0 - sel, jsonPathStatsGetAvgArraySize(path_stats));
11191119

11201120
/* Accumulate selectivity of subpath into parent path */
@@ -1144,21 +1144,23 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
11441144
Selectivity freq; /* absolute frequence of path */
11451145
Selectivity sel; /* relative selectivity of subpaths */
11461146
JsonPathStats stats; /* statistics for the path */
1147+
bool is_array_accesor; /* is it '[*]' ? */
11471148
} root, /* root path entry */
11481149
*path = &root; /* path entry stack */
11491150
Selectivity sel; /* resulting selectivity */
11501151
Selectivity scalarSel; /* selectivity of 'jsonb == scalar' */
11511152

11521153
/* Initialize root path string */
11531154
initStringInfo(&pathstr);
1154-
appendStringInfo(&pathstr, JSON_PATH_ROOT);
1155+
appendBinaryStringInfo(&pathstr, stats->prefix, stats->prefixlen);
11551156

11561157
/* Initialize root path entry */
11571158
root.parent = NULL;
11581159
root.len = pathstr.len;
1159-
root.stats = jsonStatsGetPathByStr(stats, pathstr.data, pathstr.len);
1160+
root.stats = jsonStatsFindPath(stats, pathstr.data, pathstr.len);
11601161
root.freq = jsonPathStatsGetFreq(root.stats, 0.0);
11611162
root.sel = 1.0;
1163+
root.is_array_accesor = pathstr.data[pathstr.len - 1] == ']';
11621164

11631165
/* Return 0, if NULL fraction is 1. */
11641166
if (root.freq <= 0.0)
@@ -1202,6 +1204,7 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
12021204
p->stats = NULL;
12031205
p->freq = freq;
12041206
p->sel = 1.0;
1207+
p->is_array_accesor = false;
12051208
path = p;
12061209
break;
12071210
}
@@ -1214,7 +1217,7 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
12141217

12151218
/* Appeend path string entry for array elements, get stats. */
12161219
jsonPathAppendEntry(&pathstr, NULL);
1217-
pstats = jsonStatsGetPathByStr(stats, pathstr.data, pathstr.len);
1220+
pstats = jsonStatsFindPath(stats, pathstr.data, pathstr.len);
12181221
freq = jsonPathStatsGetFreq(pstats, 0.0);
12191222

12201223
/* If there are no arrays, return 0 or scalar selectivity */
@@ -1228,6 +1231,7 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
12281231
p->stats = pstats;
12291232
p->freq = freq;
12301233
p->sel = 1.0;
1234+
p->is_array_accesor = true;
12311235
path = p;
12321236
break;
12331237
}
@@ -1247,7 +1251,8 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
12471251

12481252
/* Accumulate selectivity into parent path */
12491253
jsonAccumulateSubPathSelectivity(abs_sel, path->freq,
1250-
&path->sel, &pathstr,
1254+
&path->sel,
1255+
path->is_array_accesor,
12511256
path->stats);
12521257
break;
12531258
}
@@ -1273,7 +1278,7 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
12731278
* same statistics that was extracted in WJB_BEGIN_ARRAY.
12741279
*/
12751280
JsonPathStats pstats = r == WJB_ELEM ? path->stats :
1276-
jsonStatsGetPathByStr(stats, pathstr.data, pathstr.len);
1281+
jsonStatsFindPath(stats, pathstr.data, pathstr.len);
12771282
/* Make scalar jsonb datum */
12781283
Datum scalar = JsonbPGetDatum(JsonbValueToJsonb(&v));
12791284
/* Absolute selectivity of 'path == scalar' */
@@ -1282,7 +1287,8 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
12821287

12831288
/* Accumulate selectivity into parent path */
12841289
jsonAccumulateSubPathSelectivity(abs_sel, path->freq,
1285-
&path->sel, &pathstr,
1290+
&path->sel,
1291+
path->is_array_accesor,
12861292
path->stats);
12871293
break;
12881294
}

0 commit comments

Comments
 (0)