summaryrefslogtreecommitdiff
path: root/sql/opt_range.cc
diff options
context:
space:
mode:
Diffstat (limited to 'sql/opt_range.cc')
-rw-r--r--sql/opt_range.cc86
1 files changed, 69 insertions, 17 deletions
diff --git a/sql/opt_range.cc b/sql/opt_range.cc
index 0315b91b8be..7e987c3cfba 100644
--- a/sql/opt_range.cc
+++ b/sql/opt_range.cc
@@ -12,7 +12,7 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
/*
TODO:
@@ -1340,8 +1340,7 @@ QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT()
- Use rowids from unique to run a disk-ordered sweep
*/
-QUICK_INDEX_SORT_SELECT::QUICK_INDEX_SORT_SELECT(THD *thd_param,
- TABLE *table)
+QUICK_INDEX_SORT_SELECT::QUICK_INDEX_SORT_SELECT(THD *thd_param, TABLE *table)
:unique(NULL), pk_quick_select(NULL), thd(thd_param)
{
DBUG_ENTER("QUICK_INDEX_SORT_SELECT::QUICK_INDEX_SORT_SELECT");
@@ -1569,6 +1568,7 @@ failure:
head->column_bitmaps_set(save_read_set, save_write_set);
delete file;
file= save_file;
+ free_file= false;
DBUG_RETURN(1);
}
@@ -2420,12 +2420,16 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
records= head->stat_records();
if (!records)
records++; /* purecov: inspected */
- scan_time= (double) records / TIME_FOR_COMPARE + 1;
- read_time= (double) head->file->scan_time() + scan_time + 1.1;
- if (head->force_index)
+
+ if (head->force_index || force_quick_range)
scan_time= read_time= DBL_MAX;
- if (limit < records)
- read_time= (double) records + scan_time + 1; // Force to use index
+ else
+ {
+ scan_time= (double) records / TIME_FOR_COMPARE + 1;
+ read_time= (double) head->file->scan_time() + scan_time + 1.1;
+ if (limit < records)
+ read_time= (double) records + scan_time + 1; // Force to use index
+ }
possible_keys.clear_all();
@@ -2738,13 +2742,18 @@ bool create_key_parts_for_pseudo_indexes(RANGE_OPT_PARAM *param,
for (field_ptr= table->field; *field_ptr; field_ptr++)
{
- if (bitmap_is_set(used_fields, (*field_ptr)->field_index))
+ Field *field= *field_ptr;
+ if (bitmap_is_set(used_fields, field->field_index) &&
+ is_eits_usable(field))
parts++;
}
KEY_PART *key_part;
uint keys= 0;
+ if (!parts)
+ return TRUE;
+
if (!(key_part= (KEY_PART *) alloc_root(param->mem_root,
sizeof(KEY_PART) * parts)))
return TRUE;
@@ -2753,9 +2762,12 @@ bool create_key_parts_for_pseudo_indexes(RANGE_OPT_PARAM *param,
uint max_key_len= 0;
for (field_ptr= table->field; *field_ptr; field_ptr++)
{
- if (bitmap_is_set(used_fields, (*field_ptr)->field_index))
+ Field *field= *field_ptr;
+ if (bitmap_is_set(used_fields, field->field_index))
{
- Field *field= *field_ptr;
+ if (!is_eits_usable(field))
+ continue;
+
uint16 store_length;
uint16 max_key_part_length= (uint16) table->file->max_key_part_length();
key_part->key= keys;
@@ -2913,7 +2925,18 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item **cond)
table->cond_selectivity= 1.0;
- if (!*cond || table_records == 0)
+ if (table_records == 0)
+ DBUG_RETURN(FALSE);
+
+ QUICK_SELECT_I *quick;
+ if ((quick=table->reginfo.join_tab->quick) &&
+ quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
+ {
+ table->cond_selectivity*= (quick->records/table_records);
+ DBUG_RETURN(FALSE);
+ }
+
+ if (!*cond)
DBUG_RETURN(FALSE);
if (table->pos_in_table_list->schema_table)
@@ -3030,7 +3053,8 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item **cond)
*/
if (thd->variables.optimizer_use_condition_selectivity > 2 &&
- !bitmap_is_clear_all(used_fields))
+ !bitmap_is_clear_all(used_fields) &&
+ thd->variables.use_stat_tables > 0 && table->stats_is_read)
{
PARAM param;
MEM_ROOT alloc;
@@ -3118,6 +3142,12 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item **cond)
}
+ if (quick && (quick->get_type() == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
+ quick->get_type() == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE))
+ {
+ table->cond_selectivity*= (quick->records/table_records);
+ }
+
bitmap_union(used_fields, &handled_columns);
/* Check if we can improve selectivity estimates by using sampling */
@@ -5084,6 +5114,16 @@ typedef struct st_partial_index_intersect_info
key_map filtered_scans; /* scans to be filtered by cpk conditions */
MY_BITMAP *intersect_fields; /* bitmap of fields used in intersection */
+
+ void init()
+ {
+ common_info= NULL;
+ intersect_fields= NULL;
+ records_sent_to_unique= records= length= in_memory= use_cpk_filter= 0;
+ cost= index_read_cost= in_memory_cost= 0.0;
+ filtered_scans.init();
+ filtered_scans.clear_all();
+ }
} PARTIAL_INDEX_INTERSECT_INFO;
@@ -5220,8 +5260,7 @@ bool prepare_search_best_index_intersect(PARAM *param,
if (!n_index_scans)
return 1;
- bzero(init, sizeof(*init));
- init->filtered_scans.init();
+ init->init();
init->common_info= common;
init->cost= cutoff_cost;
@@ -6531,6 +6570,8 @@ TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
if (ror_intersect_add(intersect, cpk_scan, TRUE) &&
(intersect->total_cost < min_cost))
intersect_best= intersect; //just set pointer here
+ else
+ cpk_scan= 0; // Don't use cpk_scan
}
else
cpk_scan= 0; // Don't use cpk_scan
@@ -7307,7 +7348,8 @@ SEL_TREE *Item_bool_func::get_full_func_mm_tree(RANGE_OPT_PARAM *param,
param->current_table);
#ifdef HAVE_SPATIAL
Field::geometry_type sav_geom_type;
- if (field_item->field->type() == MYSQL_TYPE_GEOMETRY)
+ const bool geometry= field_item->field->type() == MYSQL_TYPE_GEOMETRY;
+ if (geometry)
{
sav_geom_type= ((Field_geom*) field_item->field)->geom_type;
/* We have to be able to store all sorts of spatial features here */
@@ -7342,7 +7384,7 @@ SEL_TREE *Item_bool_func::get_full_func_mm_tree(RANGE_OPT_PARAM *param,
}
#ifdef HAVE_SPATIAL
- if (field_item->field->type() == MYSQL_TYPE_GEOMETRY)
+ if (geometry)
{
((Field_geom*) field_item->field)->geom_type= sav_geom_type;
}
@@ -10141,6 +10183,16 @@ ha_rows check_quick_select(PARAM *param, uint idx, bool index_only,
bufsize, mrr_flags, cost);
if (rows != HA_POS_ERROR)
{
+ ha_rows table_records= param->table->stat_records();
+ if (rows > table_records)
+ {
+ /*
+ For any index the total number of records within all ranges
+ cannot be be bigger than the number of records in the table
+ */
+ rows= table_records;
+ set_if_bigger(rows, 1);
+ }
param->quick_rows[keynr]= rows;
param->possible_keys.set_bit(keynr);
if (update_tbl_stats)