24 #include "sql_delete.h"
25 #include "sql_cache.h"
27 #include "sql_table.h"
33 #include "opt_explain.h"
36 #include "sql_optimizer.h"
37 #include "sql_resolver.h"
55 bool using_limit=limit != HA_POS_ERROR;
56 bool transactional_table, safe_update, const_cond;
57 bool const_cond_result;
60 bool read_removal=
false;
62 bool need_sort= FALSE;
64 ORDER *order= (
ORDER *) ((order_list && order_list->elements) ?
65 order_list->
first : NULL);
66 uint usable_index= MAX_KEY;
67 SELECT_LEX *select_lex= &thd->lex->select_lex;
68 THD::killed_state killed_status= THD::NOT_KILLED;
69 THD::enum_binlog_query_type query_type= THD::ROW_QUERY_TYPE;
70 DBUG_ENTER(
"mysql_delete");
75 if (!(table= table_list->table))
77 my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0),
78 table_list->view_db.str, table_list->view_name.str);
81 THD_STAGE_INFO(thd, stage_init);
84 if (mysql_prepare_delete(thd, table_list, &conds))
94 memset(&tables, 0,
sizeof(tables));
96 tables.alias = table_list->alias;
98 if (select_lex->setup_ref_array(thd, order_list->elements) ||
99 setup_order(thd, select_lex->ref_pointer_array, &tables,
100 fields, all_fields, order))
107 #ifdef WITH_PARTITION_STORAGE_ENGINE
112 if (prune_partitions(thd, table, conds))
114 if (table->all_partitions_pruned_away)
115 goto exit_all_parts_pruned_away;
118 if (
lock_tables(thd, table_list, thd->lex->table_count, 0))
121 const_cond= (!conds || conds->const_item());
122 safe_update=
test(thd->variables.option_bits & OPTION_SAFE_UPDATES);
123 if (safe_update && const_cond)
125 my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
126 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
130 select_lex->no_error= thd->lex->ignore;
132 const_cond_result= const_cond && (!conds || conds->val_int());
153 if (!using_limit && const_cond_result &&
154 !(specialflag & SPECIAL_NO_NEW_FUNC) &&
155 (!thd->is_current_stmt_binlog_format_row() &&
156 !(table->triggers && table->triggers->has_delete_triggers())))
159 table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
160 ha_rows
const maybe_deleted= table->file->stats.records;
162 if (thd->lex->describe)
164 err= explain_no_table(thd,
"Deleting all rows", maybe_deleted);
165 goto exit_without_my_ok;
168 DBUG_PRINT(
"debug", (
"Trying to use delete_all_rows()"));
175 query_type= THD::STMT_QUERY_TYPE;
177 deleted= maybe_deleted;
180 if (error != HA_ERR_WRONG_COMMAND)
192 Item::cond_result result;
194 conds=
optimize_cond(thd, conds, &cond_equal, select_lex->join_list,
196 if (result == Item::COND_FALSE)
200 if (thd->lex->describe)
202 err= explain_no_table(thd,
"Impossible WHERE");
203 goto exit_without_my_ok;
209 conds->update_used_tables();
214 table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
216 table->covering_keys.clear_all();
217 table->quick_keys.clear_all();
218 table->possible_quick_keys.clear_all();
220 #ifdef WITH_PARTITION_STORAGE_ENGINE
222 if (prune_partitions(thd, table, conds))
224 if (table->all_partitions_pruned_away)
225 goto exit_all_parts_pruned_away;
228 select=make_select(table, 0, 0, conds, 0, &error);
234 wrapper.add_utf8_table(table);
236 if ((select && select->check_quick(thd, safe_update, limit)) || !limit)
238 if (thd->lex->describe && !error && !thd->is_error())
240 err= explain_no_table(thd,
"Impossible WHERE");
241 goto exit_without_my_ok;
259 if (table->quick_keys.is_clear_all())
261 thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
262 if (safe_update && !using_limit)
266 my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
267 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
278 &need_sort, &reverse);
281 if (thd->lex->describe)
283 err= explain_single_table_modification(thd, table, select, usable_index,
284 limit,
false, need_sort,
false);
285 goto exit_without_my_ok;
288 if (options & OPTION_QUICK)
289 (void) table->file->extra(HA_EXTRA_QUICK);
293 ha_rows examined_rows;
297 Filesort fsort(order, HA_POS_ERROR, select);
298 DBUG_ASSERT(usable_index == MAX_KEY);
300 MYF(MY_FAE | MY_ZEROFILL));
302 if ((table->sort.found_records=
filesort(thd, table, &fsort,
true,
303 &examined_rows, &found_rows))
306 goto exit_without_my_ok;
308 thd->inc_examined_row_count(examined_rows);
320 if (select && select->quick && (error= select->quick->reset()))
323 goto exit_without_my_ok;
325 if (usable_index==MAX_KEY || (select && select->quick))
326 error= init_read_record(&info, thd, table, select, 1, 1, FALSE);
331 goto exit_without_my_ok;
332 init_ftfuncs(thd, select_lex, 1);
333 THD_STAGE_INFO(thd, stage_updating);
335 if (table->triggers &&
336 table->triggers->has_triggers(TRG_EVENT_DELETE,
344 (void) table->file->extra(HA_EXTRA_DELETE_CANNOT_BATCH);
350 table->mark_columns_needed_for_delete();
352 if ((table->file->
ha_table_flags() & HA_READ_BEFORE_WRITE_REMOVAL) &&
354 select && select->quick && select->quick->index != MAX_KEY)
357 while (!(error=info.read_record(&info)) && !thd->killed &&
360 thd->inc_examined_row_count(1);
362 if (!select || (!select->skip_record(thd, &skip_record) && !skip_record))
365 if (table->triggers &&
367 TRG_ACTION_BEFORE, FALSE))
373 if (!(error= table->file->ha_delete_row(table->record[0])))
376 if (table->triggers &&
378 TRG_ACTION_AFTER, FALSE))
383 if (!--limit && using_limit)
408 else if (!thd->is_error())
409 table->file->unlock_row();
413 killed_status= thd->killed;
414 if (killed_status != THD::NOT_KILLED || thd->is_error())
427 THD_STAGE_INFO(thd, stage_end);
428 end_read_record(&info);
429 if (options & OPTION_QUICK)
430 (void) table->file->extra(HA_EXTRA_NORMAL);
433 DBUG_ASSERT(!thd->lex->describe);
440 query_cache_invalidate3(thd, table_list, 1);
445 transactional_table= table->file->has_transactions();
447 if (!transactional_table && deleted > 0)
448 thd->transaction.stmt.mark_modified_non_trans_table();
451 if ((error < 0) || thd->transaction.stmt.cannot_safely_rollback())
453 if (mysql_bin_log.is_open())
459 errcode= query_error_code(thd, killed_status == THD::NOT_KILLED);
467 int log_result= thd->binlog_query(query_type,
468 thd->query(), thd->query_length(),
469 transactional_table, FALSE, FALSE,
478 DBUG_ASSERT(transactional_table || !deleted || thd->transaction.stmt.cannot_safely_rollback());
481 (thd->lex->ignore && !thd->is_error() && !thd->is_fatal_error))
484 DBUG_PRINT(
"info",(
"%ld records deleted",(
long) deleted));
486 DBUG_RETURN(thd->is_error() || thd->killed);
488 #ifdef WITH_PARTITION_STORAGE_ENGINE
489 exit_all_parts_pruned_away:
491 if (!thd->lex->describe)
496 err= explain_no_table(thd,
"No matching rows after partition pruning");
502 table->set_keyread(
false);
503 DBUG_RETURN((err || thd->is_error() || thd->killed) ? 1 : 0);
520 int mysql_prepare_delete(THD *thd,
TABLE_LIST *table_list,
Item **conds)
523 SELECT_LEX *select_lex= &thd->lex->select_lex;
524 DBUG_ENTER(
"mysql_prepare_delete");
527 thd->lex->allow_sum_func= 0;
529 &thd->lex->select_lex.top_join_list,
531 &select_lex->leaf_tables, FALSE,
532 DELETE_ACL, SELECT_ACL) ||
533 setup_conds(thd, table_list, select_lex->leaf_tables, conds) ||
534 setup_ftfuncs(select_lex))
536 if (!table_list->updatable || check_key_in_view(thd, table_list))
538 my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias,
"DELETE");
543 if ((duplicate=
unique_table(thd, table_list, table_list->next_global, 0)))
545 update_non_unique_table_error(table_list,
"DELETE", duplicate);
550 if (select_lex->inner_refs_list.elements &&
551 fix_inner_refs(thd, all_fields, select_lex, select_lex->ref_pointer_array))
554 select_lex->fix_prepare_information(thd, conds, &fake_conds);
563 #define MEM_STRIP_BUF_SIZE current_thd->variables.sortbuff_size
565 extern "C" int refpos_order_cmp(
const void* arg,
const void *a,
const void *b)
568 return file->cmp_ref((
const uchar*)a, (
const uchar*)b);
581 int mysql_multi_delete_prepare(THD *thd, uint *table_count)
584 TABLE_LIST *aux_tables= lex->auxiliary_table_list.first;
586 DBUG_ENTER(
"mysql_multi_delete_prepare");
595 &thd->lex->select_lex.top_join_list,
597 &lex->select_lex.leaf_tables, FALSE,
598 DELETE_ACL, SELECT_ACL))
607 lex->select_lex.exclude_from_table_unique_test= TRUE;
611 target_tbl= target_tbl->next_local)
615 if (!(target_tbl->table= target_tbl->correspondent_table->table))
617 DBUG_ASSERT(target_tbl->correspondent_table->view &&
618 target_tbl->correspondent_table->multitable_view);
619 my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0),
620 target_tbl->correspondent_table->view_db.str,
621 target_tbl->correspondent_table->view_name.str);
625 if (!target_tbl->correspondent_table->updatable ||
626 check_key_in_view(thd, target_tbl->correspondent_table))
628 my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
629 target_tbl->table_name,
"DELETE");
638 if ((duplicate=
unique_table(thd, target_tbl->correspondent_table,
639 lex->query_tables, 0)))
641 update_non_unique_table_error(target_tbl->correspondent_table,
642 "DELETE", duplicate);
651 lex->select_lex.exclude_from_table_unique_test= FALSE;
656 multi_delete::multi_delete(
TABLE_LIST *dt, uint num_of_tables_arg)
657 : delete_tables(dt), deleted(0), found(0),
658 num_of_tables(num_of_tables_arg), error(0),
659 do_delete(0), transactional_tables(0), normal_tables(0), error_handled(0)
661 tempfiles= (Unique **) sql_calloc(
sizeof(Unique *) * num_of_tables);
666 multi_delete::prepare(
List<Item> &values, SELECT_LEX_UNIT *u)
668 DBUG_ENTER(
"multi_delete::prepare");
671 THD_STAGE_INFO(thd, stage_deleting_from_main_table);
677 multi_delete::initialize_tables(
JOIN *join)
680 Unique **tempfiles_ptr;
681 DBUG_ENTER(
"initialize_tables");
686 table_map tables_to_delete_from=0;
687 delete_while_scanning= 1;
688 for (walk= delete_tables; walk; walk= walk->next_local)
690 tables_to_delete_from|= walk->table->map;
691 if (delete_while_scanning &&
699 delete_while_scanning= 0;
708 if (tab->table->map & tables_to_delete_from)
711 TABLE *tbl=walk->table=tab->table;
712 walk= walk->next_local;
717 tbl->covering_keys.clear_all();
718 if (tbl->file->has_transactions())
719 transactional_tables= 1;
723 tbl->triggers->has_triggers(TRG_EVENT_DELETE,
731 (void) tbl->file->extra(HA_EXTRA_DELETE_CANNOT_BATCH);
734 tbl->mark_columns_needed_for_delete();
736 else if ((tab->type != JT_SYSTEM && tab->type != JT_CONST) &&
737 walk == delete_tables)
744 delete_while_scanning= 0;
748 tempfiles_ptr= tempfiles;
749 if (delete_while_scanning)
751 table_being_deleted= delete_tables;
752 walk= walk->next_local;
754 for (;walk ;walk= walk->next_local)
756 TABLE *table=walk->table;
757 *tempfiles_ptr++=
new Unique (refpos_order_cmp,
758 (
void *) table->file,
762 init_ftfuncs(thd, thd->lex->current_select, 1);
763 DBUG_RETURN(thd->is_fatal_error != 0);
767 multi_delete::~multi_delete()
769 for (table_being_deleted= delete_tables;
771 table_being_deleted= table_being_deleted->next_local)
773 TABLE *table= table_being_deleted->table;
777 for (uint counter= 0; counter < num_of_tables; counter++)
779 if (tempfiles[counter])
780 delete tempfiles[counter];
785 bool multi_delete::send_data(
List<Item> &values)
787 int secure_counter= delete_while_scanning ? -1 : 0;
789 DBUG_ENTER(
"multi_delete::send_data");
791 bool ignore= thd->lex->current_select->no_error;
793 for (del_table= delete_tables;
795 del_table= del_table->next_local, secure_counter++)
797 TABLE *table= del_table->table;
800 if (table->status & (STATUS_NULL_ROW | STATUS_DELETED))
803 table->file->position(table->record[0]);
806 if (secure_counter < 0)
809 DBUG_ASSERT(del_table == table_being_deleted);
810 if (table->triggers &&
812 TRG_ACTION_BEFORE, FALSE))
814 table->status|= STATUS_DELETED;
815 if (!(error=table->file->ha_delete_row(table->record[0])))
818 if (!table->file->has_transactions())
819 thd->transaction.stmt.mark_modified_non_trans_table();
820 if (table->triggers &&
822 TRG_ACTION_AFTER, FALSE))
837 error=tempfiles[secure_counter]->unique_add((
char*) table->file->ref);
849 void multi_delete::send_error(uint errcode,
const char *err)
851 DBUG_ENTER(
"multi_delete::send_error");
854 my_message(errcode, err, MYF(0));
860 void multi_delete::abort_result_set()
862 DBUG_ENTER(
"multi_delete::abort_result_set");
866 (!thd->transaction.stmt.cannot_safely_rollback() && !deleted))
871 query_cache_invalidate3(thd, delete_tables, 1);
879 if (do_delete && normal_tables &&
880 (table_being_deleted != delete_tables ||
881 !table_being_deleted->table->file->has_transactions()))
889 DBUG_ASSERT(error_handled);
893 if (thd->transaction.stmt.cannot_safely_rollback())
898 if (mysql_bin_log.is_open())
900 int errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED);
902 (void) thd->binlog_query(THD::ROW_QUERY_TYPE,
903 thd->query(), thd->query_length(),
904 transactional_tables, FALSE, FALSE, errcode);
923 int multi_delete::do_deletes()
925 DBUG_ENTER(
"do_deletes");
926 DBUG_ASSERT(do_delete);
932 table_being_deleted= (delete_while_scanning ? delete_tables->next_local :
935 for (uint counter= 0; table_being_deleted;
936 table_being_deleted= table_being_deleted->next_local, counter++)
938 TABLE *table = table_being_deleted->table;
939 if (tempfiles[counter]->
get(table))
943 do_table_deletes(table, thd->lex->current_select->no_error);
945 if (thd->killed && !local_error)
948 if (local_error == -1)
952 DBUG_RETURN(local_error);
973 int multi_delete::do_table_deletes(
TABLE *table,
bool ignore)
977 ha_rows last_deleted= deleted;
978 DBUG_ENTER(
"do_deletes_for_table");
979 if (init_read_record(&info, thd, table, NULL, 0, 1, FALSE))
985 info.ignore_not_found_rows= 1;
987 while (!(local_error= info.read_record(&info)) && !thd->killed)
989 if (table->triggers &&
991 TRG_ACTION_BEFORE, FALSE))
997 local_error= table->file->ha_delete_row(table->record[0]);
998 if (local_error && !ignore)
1012 if (table->triggers &&
1014 TRG_ACTION_AFTER, FALSE))
1024 if (tmp_error && !local_error)
1026 local_error= tmp_error;
1030 if (last_deleted != deleted && !table->file->has_transactions())
1031 thd->transaction.stmt.mark_modified_non_trans_table();
1033 end_read_record(&info);
1035 DBUG_RETURN(local_error);
1045 bool multi_delete::send_eof()
1047 THD::killed_state killed_status= THD::NOT_KILLED;
1048 THD_STAGE_INFO(thd, stage_deleting_from_reference_tables);
1051 int local_error= do_deletes();
1054 local_error= local_error || error;
1055 killed_status= (local_error == 0)? THD::NOT_KILLED : thd->killed;
1057 THD_STAGE_INFO(thd, stage_end);
1065 query_cache_invalidate3(thd, delete_tables, 1);
1067 if ((local_error == 0) || thd->transaction.stmt.cannot_safely_rollback())
1069 if (mysql_bin_log.is_open())
1072 if (local_error == 0)
1075 errcode= query_error_code(thd, killed_status == THD::NOT_KILLED);
1076 if (thd->binlog_query(THD::ROW_QUERY_TYPE,
1077 thd->query(), thd->query_length(),
1078 transactional_tables, FALSE, FALSE, errcode) &&
1085 if (local_error != 0)
1086 error_handled= TRUE;
1090 ::my_ok(thd, deleted);