forked from postgrespro/aqo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cardinality_estimation.c
68 lines (61 loc) · 1.6 KB
/
cardinality_estimation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
*******************************************************************************
*
* CARDINALITY ESTIMATION
*
* This is the module in which cardinality estimation problem obtained from
* cardinality_hooks turns into machine learning problem.
*
*******************************************************************************
*
* Copyright (c) 2016-2021, Postgres Professional
*
* IDENTIFICATION
* aqo/cardinality_estimation.c
*
*/
#include "aqo.h"
#include "optimizer/optimizer.h"
/*
* General method for prediction the cardinality of given relation.
*/
double
predict_for_relation(List *restrict_clauses, List *selectivities,
List *relids, int *fss_hash)
{
int nfeatures;
double *matrix[aqo_K];
double targets[aqo_K];
double *features;
double result;
int rows;
int i;
*fss_hash = get_fss_for_object(restrict_clauses, selectivities, relids,
&nfeatures, &features);
if (nfeatures > 0)
for (i = 0; i < aqo_K; ++i)
matrix[i] = palloc0(sizeof(**matrix) * nfeatures);
if (load_fss(query_context.fspace_hash, *fss_hash, nfeatures, matrix,
targets, &rows))
result = OkNNr_predict(rows, nfeatures, matrix, targets, features);
else
{
/*
* Due to planning optimizer tries to build many alternate paths. Many
* of these not used in final query execution path. Consequently, only
* small part of paths was used for AQO learning and fetch into the AQO
* knowledge base.
*/
result = -1;
}
pfree(features);
if (nfeatures > 0)
{
for (i = 0; i < aqo_K; ++i)
pfree(matrix[i]);
}
if (result < 0)
return -1;
else
return clamp_row_est(exp(result));
}