MySQL  8.0.27
Source Code Documentation
opt_statistics.cc File Reference
#include "sql/opt_statistics.h"
#include <assert.h>
#include <algorithm>
#include "my_base.h"
#include "my_macros.h"
#include "sql/handler.h"
#include "sql/key.h"
#include "sql/table.h"

Functions

rec_per_key_t guess_rec_per_key (const TABLE *const table, const KEY *const key, uint used_keyparts)
 This code for computing a guestimate for records per key is based on code in Optimize_table_order::find_best_ref(). More...
 

Function Documentation

◆ guess_rec_per_key()

rec_per_key_t guess_rec_per_key ( const TABLE *const  table,
const KEY *const  key,
uint  used_keyparts 
)

This code for computing a guestimate for records per key is based on code in Optimize_table_order::find_best_ref().

Guesstimate for "records per key" when index statistics is not available.

Assume that the first key part matches 1% of the file and that the whole key matches 10 (duplicates) or 1 (unique) records. For small tables, ensure there are at least ten different key values. Assume also that more key matches proportionally more records. This gives the formula:

records = a - (x-1)/(c-1)*(a-b)

where

b = records matched by whole key a = records matched by first key part (1% of all records?) c = number of key parts in key x = used key parts (1 <= x <= c)