mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-13 16:16:56 +08:00
Added basic cost estimation [skip ci]
This commit is contained in:
20
src/hnsw.c
20
src/hnsw.c
@@ -1,6 +1,7 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include "access/amapi.h"
|
#include "access/amapi.h"
|
||||||
#include "commands/vacuum.h"
|
#include "commands/vacuum.h"
|
||||||
@@ -69,6 +70,9 @@ hnswcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
|
|||||||
double *indexPages)
|
double *indexPages)
|
||||||
{
|
{
|
||||||
GenericCosts costs;
|
GenericCosts costs;
|
||||||
|
int m;
|
||||||
|
int entryLevel;
|
||||||
|
Relation index;
|
||||||
#if PG_VERSION_NUM < 120000
|
#if PG_VERSION_NUM < 120000
|
||||||
List *qinfos;
|
List *qinfos;
|
||||||
#endif
|
#endif
|
||||||
@@ -86,6 +90,17 @@ hnswcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
|
|||||||
|
|
||||||
MemSet(&costs, 0, sizeof(costs));
|
MemSet(&costs, 0, sizeof(costs));
|
||||||
|
|
||||||
|
index = index_open(path->indexinfo->indexoid, NoLock);
|
||||||
|
m = HnswGetM(index);
|
||||||
|
index_close(index, NoLock);
|
||||||
|
|
||||||
|
/* Approximate entry level */
|
||||||
|
entryLevel = (int) -log(1.0 / path->indexinfo->tuples) * HnswGetMl(m);
|
||||||
|
|
||||||
|
/* TODO Improve estimate of visited tuples (currently underestimates) */
|
||||||
|
/* Account for number of tuples (or entry level), m, and ef_search */
|
||||||
|
costs.numIndexTuples = (entryLevel + 2) * m;
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 120000
|
#if PG_VERSION_NUM >= 120000
|
||||||
genericcostestimate(root, path, loop_count, &costs);
|
genericcostestimate(root, path, loop_count, &costs);
|
||||||
#else
|
#else
|
||||||
@@ -93,9 +108,8 @@ hnswcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
|
|||||||
genericcostestimate(root, path, loop_count, qinfos, &costs);
|
genericcostestimate(root, path, loop_count, qinfos, &costs);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* TODO Improve cost estimate */
|
/* Use total cost since most work happens before first tuple is returned */
|
||||||
|
*indexStartupCost = costs.indexTotalCost;
|
||||||
*indexStartupCost = costs.indexStartupCost;
|
|
||||||
*indexTotalCost = costs.indexTotalCost;
|
*indexTotalCost = costs.indexTotalCost;
|
||||||
*indexSelectivity = costs.indexSelectivity;
|
*indexSelectivity = costs.indexSelectivity;
|
||||||
*indexCorrelation = costs.indexCorrelation;
|
*indexCorrelation = costs.indexCorrelation;
|
||||||
|
|||||||
Reference in New Issue
Block a user