Always change some cost to sequential, and always update total cost

This commit is contained in:
Andrew Kane
2023-06-10 02:10:53 -07:00
parent 49e6a72d36
commit a33e72d58e

View File

@@ -71,8 +71,8 @@ ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
int lists; int lists;
double ratio; double ratio;
double spc_seq_page_cost; double spc_seq_page_cost;
double seqPageRatio = 0.5;
Relation indexRel; Relation indexRel;
Cost startupCost;
#if PG_VERSION_NUM < 120000 #if PG_VERSION_NUM < 120000
List *qinfos; List *qinfos;
#endif #endif
@@ -115,25 +115,17 @@ ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
get_tablespace_page_costs(path->indexinfo->reltablespace, NULL, &spc_seq_page_cost); get_tablespace_page_costs(path->indexinfo->reltablespace, NULL, &spc_seq_page_cost);
/* Use total cost since most work happens before first tuple is returned */ /* Change some page cost from random to sequential */
startupCost = costs.indexTotalCost; costs.indexTotalCost -= seqPageRatio * costs.numIndexPages * (costs.spc_random_page_cost - spc_seq_page_cost);
if (costs.spc_random_page_cost >= spc_seq_page_cost) /* Adjust cost if needed since TOAST not included in seq scan cost */
if (costs.numIndexPages > path->indexinfo->rel->pages && ratio < 0.5)
{ {
/* Adjust cost if needed since TOAST not included in seq scan cost */ /* Change rest of page cost from random to sequential */
if (costs.numIndexPages > path->indexinfo->rel->pages && ratio < 0.5) costs.indexTotalCost -= (1 - seqPageRatio) * costs.numIndexPages * (costs.spc_random_page_cost - spc_seq_page_cost);
{
/* Change page cost from random to sequential */
startupCost -= costs.numIndexPages * (costs.spc_random_page_cost - spc_seq_page_cost);
/* Remove cost of extra pages */ /* Remove cost of extra pages */
startupCost -= (costs.numIndexPages - path->indexinfo->rel->pages) * spc_seq_page_cost; costs.indexTotalCost -= (costs.numIndexPages - path->indexinfo->rel->pages) * spc_seq_page_cost;
}
else if (ratio < 0.5)
{
/* Change some page cost from random to sequential */
startupCost -= 0.5 * costs.numIndexPages * (costs.spc_random_page_cost - spc_seq_page_cost);
}
} }
/* /*
@@ -143,7 +135,8 @@ ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
if (ratio < costs.indexSelectivity) if (ratio < costs.indexSelectivity)
costs.indexSelectivity = ratio; costs.indexSelectivity = ratio;
*indexStartupCost = startupCost; /* Use total cost since most work happens before first tuple is returned */
*indexStartupCost = costs.indexTotalCost;
*indexTotalCost = costs.indexTotalCost; *indexTotalCost = costs.indexTotalCost;
*indexSelectivity = costs.indexSelectivity; *indexSelectivity = costs.indexSelectivity;
*indexCorrelation = costs.indexCorrelation; *indexCorrelation = costs.indexCorrelation;