mirror of
https://github.com/MariaDB/server.git
synced 2025-08-08 11:22:35 +03:00
A better fix for UNION and slow query log.
sql/sql_union.cc: A better fix for UNION and slow query log. This patch also fixes some indentation issues
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (C) 2000 MySQL AB
|
/* Copyright (C) 2000-2003 MySQL AB
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -68,6 +68,7 @@ int select_union::prepare(List<Item> &list, SELECT_LEX_UNIT *u)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool select_union::send_data(List<Item> &values)
|
bool select_union::send_data(List<Item> &values)
|
||||||
{
|
{
|
||||||
if (unit->offset_limit_cnt)
|
if (unit->offset_limit_cnt)
|
||||||
@@ -91,11 +92,13 @@ bool select_union::send_data(List<Item> &values)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool select_union::send_eof()
|
bool select_union::send_eof()
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool select_union::flush()
|
bool select_union::flush()
|
||||||
{
|
{
|
||||||
int error;
|
int error;
|
||||||
@@ -108,9 +111,13 @@ bool select_union::flush()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int st_select_lex_unit::prepare(THD *thd, select_result *result,
|
|
||||||
|
int st_select_lex_unit::prepare(THD *thd, select_result *sel_result,
|
||||||
bool tables_and_fields_initied)
|
bool tables_and_fields_initied)
|
||||||
{
|
{
|
||||||
|
SELECT_LEX_NODE *lex_select_save= thd->lex.current_select;
|
||||||
|
SELECT_LEX *select_cursor;
|
||||||
|
TMP_TABLE_PARAM tmp_table_param;
|
||||||
DBUG_ENTER("st_select_lex_unit::prepare");
|
DBUG_ENTER("st_select_lex_unit::prepare");
|
||||||
|
|
||||||
if (prepared)
|
if (prepared)
|
||||||
@@ -118,11 +125,8 @@ int st_select_lex_unit::prepare(THD *thd, select_result *result,
|
|||||||
prepared= 1;
|
prepared= 1;
|
||||||
res= 0;
|
res= 0;
|
||||||
found_rows_for_union= 0;
|
found_rows_for_union= 0;
|
||||||
TMP_TABLE_PARAM tmp_table_param;
|
result= sel_result;
|
||||||
this->result= result;
|
|
||||||
t_and_f= tables_and_fields_initied;
|
t_and_f= tables_and_fields_initied;
|
||||||
SELECT_LEX_NODE *lex_select_save= thd->lex.current_select;
|
|
||||||
SELECT_LEX *select_cursor;
|
|
||||||
|
|
||||||
thd->lex.current_select= select_cursor= first_select_in_union();
|
thd->lex.current_select= select_cursor= first_select_in_union();
|
||||||
/* Global option */
|
/* Global option */
|
||||||
@@ -144,7 +148,8 @@ int st_select_lex_unit::prepare(THD *thd, select_result *result,
|
|||||||
TABLE_LIST *first_table= (TABLE_LIST*) select_cursor->table_list.first;
|
TABLE_LIST *first_table= (TABLE_LIST*) select_cursor->table_list.first;
|
||||||
|
|
||||||
if (setup_tables(first_table) ||
|
if (setup_tables(first_table) ||
|
||||||
setup_wild(thd, first_table, select_cursor->item_list, 0, select_cursor->with_wild))
|
setup_wild(thd, first_table, select_cursor->item_list, 0,
|
||||||
|
select_cursor->with_wild))
|
||||||
goto err;
|
goto err;
|
||||||
List_iterator<Item> it(select_cursor->item_list);
|
List_iterator<Item> it(select_cursor->item_list);
|
||||||
Item *item;
|
Item *item;
|
||||||
@@ -156,8 +161,8 @@ int st_select_lex_unit::prepare(THD *thd, select_result *result,
|
|||||||
(item_list.elements + select_cursor->with_sum_func +
|
(item_list.elements + select_cursor->with_sum_func +
|
||||||
select_cursor->order_list.elements +
|
select_cursor->order_list.elements +
|
||||||
select_cursor->group_list.elements)) ||
|
select_cursor->group_list.elements)) ||
|
||||||
setup_fields(thd, select_cursor->ref_pointer_array, first_table, item_list,
|
setup_fields(thd, select_cursor->ref_pointer_array, first_table,
|
||||||
0, 0, 1))
|
item_list, 0, 0, 1))
|
||||||
goto err;
|
goto err;
|
||||||
t_and_f= 1;
|
t_and_f= 1;
|
||||||
}
|
}
|
||||||
@@ -183,11 +188,11 @@ int st_select_lex_unit::prepare(THD *thd, select_result *result,
|
|||||||
union_result->not_describe=1;
|
union_result->not_describe=1;
|
||||||
union_result->tmp_table_param=&tmp_table_param;
|
union_result->tmp_table_param=&tmp_table_param;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
the following piece of code is placed here solely for the purpose of
|
The following piece of code is placed here solely for the purpose of
|
||||||
getting correct results with EXPLAIN when UNION is withing a sub-select
|
getting correct results with EXPLAIN when UNION is withing a sub-select
|
||||||
or derived table ...
|
or derived table ...
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (thd->lex.describe)
|
if (thd->lex.describe)
|
||||||
{
|
{
|
||||||
@@ -216,7 +221,7 @@ int st_select_lex_unit::prepare(THD *thd, select_result *result,
|
|||||||
(ORDER*) NULL,
|
(ORDER*) NULL,
|
||||||
sl, this, t_and_f);
|
sl, this, t_and_f);
|
||||||
t_and_f= 0;
|
t_and_f= 0;
|
||||||
if (res | thd->is_fatal_error)
|
if (res || thd->is_fatal_error)
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -235,7 +240,7 @@ int st_select_lex_unit::prepare(THD *thd, select_result *result,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DBUG_RETURN(res | thd->is_fatal_error);
|
DBUG_RETURN(res || thd->is_fatal_error ? 1 : 0);
|
||||||
err:
|
err:
|
||||||
thd->lex.current_select= lex_select_save;
|
thd->lex.current_select= lex_select_save;
|
||||||
DBUG_RETURN(-1);
|
DBUG_RETURN(-1);
|
||||||
@@ -244,17 +249,18 @@ err:
|
|||||||
|
|
||||||
int st_select_lex_unit::exec()
|
int st_select_lex_unit::exec()
|
||||||
{
|
{
|
||||||
DBUG_ENTER("st_select_lex_unit::exec");
|
int do_print_slow= 0;
|
||||||
SELECT_LEX_NODE *lex_select_save= thd->lex.current_select;
|
SELECT_LEX_NODE *lex_select_save= thd->lex.current_select;
|
||||||
SELECT_LEX *select_cursor=first_select_in_union(), *last_select;
|
SELECT_LEX *select_cursor=first_select_in_union(), *last_select;
|
||||||
|
DBUG_ENTER("st_select_lex_unit::exec");
|
||||||
|
|
||||||
LINT_INIT(last_select);
|
LINT_INIT(last_select);
|
||||||
bool do_print_slow=0;
|
|
||||||
|
|
||||||
if (executed && !(dependent || uncacheable))
|
if (executed && !(dependent || uncacheable))
|
||||||
DBUG_RETURN(0);
|
DBUG_RETURN(0);
|
||||||
executed= 1;
|
executed= 1;
|
||||||
|
|
||||||
if ((dependent||uncacheable) || !item || !item->assigned())
|
if ((dependent || uncacheable) || !item || !item->assigned())
|
||||||
{
|
{
|
||||||
if (optimized && item && item->assigned())
|
if (optimized && item && item->assigned())
|
||||||
{
|
{
|
||||||
@@ -314,7 +320,7 @@ int st_select_lex_unit::exec()
|
|||||||
thd->lex.current_select= lex_select_save;
|
thd->lex.current_select= lex_select_save;
|
||||||
DBUG_RETURN(res);
|
DBUG_RETURN(res);
|
||||||
}
|
}
|
||||||
do_print_slow = do_print_slow || (select_cursor->options & (QUERY_NO_INDEX_USED | QUERY_NO_GOOD_INDEX_USED));
|
do_print_slow|= select_cursor->options;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
optimized= 1;
|
optimized= 1;
|
||||||
@@ -322,8 +328,8 @@ int st_select_lex_unit::exec()
|
|||||||
/* Send result to 'result' */
|
/* Send result to 'result' */
|
||||||
|
|
||||||
// to correct ORDER BY reference resolving
|
// to correct ORDER BY reference resolving
|
||||||
thd->lex.current_select = select_cursor;
|
thd->lex.current_select= select_cursor;
|
||||||
res =-1;
|
res= -1;
|
||||||
{
|
{
|
||||||
List<Item_func_match> empty_list;
|
List<Item_func_match> empty_list;
|
||||||
empty_list.empty();
|
empty_list.empty();
|
||||||
@@ -332,9 +338,11 @@ int st_select_lex_unit::exec()
|
|||||||
{
|
{
|
||||||
SELECT_LEX *fake_select = new SELECT_LEX();
|
SELECT_LEX *fake_select = new SELECT_LEX();
|
||||||
fake_select->make_empty_select(last_select);
|
fake_select->make_empty_select(last_select);
|
||||||
offset_limit_cnt= (select_cursor->braces) ? global_parameters->offset_limit : 0;
|
offset_limit_cnt= (select_cursor->braces ?
|
||||||
select_limit_cnt= (select_cursor->braces) ? global_parameters->select_limit+
|
global_parameters->offset_limit : 0);
|
||||||
global_parameters->offset_limit : HA_POS_ERROR;
|
select_limit_cnt= (select_cursor->braces ?
|
||||||
|
global_parameters->select_limit+
|
||||||
|
global_parameters->offset_limit : HA_POS_ERROR);
|
||||||
if (select_limit_cnt < global_parameters->select_limit)
|
if (select_limit_cnt < global_parameters->select_limit)
|
||||||
select_limit_cnt= HA_POS_ERROR; // no limit
|
select_limit_cnt= HA_POS_ERROR; // no limit
|
||||||
if (select_limit_cnt == HA_POS_ERROR)
|
if (select_limit_cnt == HA_POS_ERROR)
|
||||||
@@ -351,14 +359,23 @@ int st_select_lex_unit::exec()
|
|||||||
thd->limit_found_rows = (ulonglong)table->file->records;
|
thd->limit_found_rows = (ulonglong)table->file->records;
|
||||||
fake_select->exclude();
|
fake_select->exclude();
|
||||||
delete fake_select;
|
delete fake_select;
|
||||||
if (select_cursor == &thd->lex.select_lex && !do_print_slow)
|
/*
|
||||||
select_cursor->options &= ~(QUERY_NO_INDEX_USED | QUERY_NO_GOOD_INDEX_USED);
|
Mark for slow query log if any of the union parts didn't use
|
||||||
|
indexes efficiently
|
||||||
|
*/
|
||||||
|
select_cursor->options= ((select_cursor->options &
|
||||||
|
~(QUERY_NO_INDEX_USED |
|
||||||
|
QUERY_NO_GOOD_INDEX_USED)) |
|
||||||
|
do_print_slow &
|
||||||
|
(QUERY_NO_INDEX_USED |
|
||||||
|
QUERY_NO_GOOD_INDEX_USED));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
thd->lex.current_select= lex_select_save;
|
thd->lex.current_select= lex_select_save;
|
||||||
DBUG_RETURN(res);
|
DBUG_RETURN(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int st_select_lex_unit::cleanup()
|
int st_select_lex_unit::cleanup()
|
||||||
{
|
{
|
||||||
int error= 0;
|
int error= 0;
|
||||||
|
Reference in New Issue
Block a user