Commit ff4507a9 authored by unknown's avatar unknown
Browse files

InnoDB: Implement fast TRUNCATE TABLE (Bug #7150)


innobase/dict/dict0boot.c:
  Added DICT_SYS_INDEXES_TYPE_FIELD
innobase/dict/dict0crea.c:
  Added dict_truncate_index_tree()
innobase/include/dict0boot.h:
  Added DICT_SYS_INDEXES_TYPE_FIELD
innobase/include/dict0crea.h:
  Added dict_truncate_index_tree()
innobase/include/row0mysql.h:
  Added row_truncate_table_for_mysql()
innobase/row/row0mysql.c:
  Added row_truncate_table_for_mysql()
sql/ha_innodb.cc:
  Added ha_innobase::delete_all_rows() in order to
  implement fast TRUNCATE TABLE
sql/ha_innodb.h:
  Added ha_innobase::delete_all_rows() in order to
  implement fast TRUNCATE TABLE
parent 0b7895b9
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -332,6 +332,9 @@ dict_boot(void)
#endif
#if DICT_SYS_INDEXES_SPACE_NO_FIELD != 5 + 2
#error "DICT_SYS_INDEXES_SPACE_NO_FIELD != 5 + 2"
#endif
#if DICT_SYS_INDEXES_TYPE_FIELD != 4 + 2
#error "DICT_SYS_INDEXES_TYPE_FIELD != 4 + 2"
#endif

	table->id = DICT_INDEXES_ID;
+95 −0
Original line number Diff line number Diff line
@@ -706,6 +706,101 @@ dict_drop_index_tree(
				DICT_SYS_INDEXES_PAGE_NO_FIELD, FIL_NULL, mtr);
}

/***********************************************************************
Truncates the index tree associated with a row in SYS_INDEXES table. */

void
dict_truncate_index_tree(
/*=====================*/
	dict_table_t*	table,	/* in: the table the index belongs to */
	rec_t*		rec,	/* in: record in the clustered index of
				SYS_INDEXES table */
	mtr_t*		mtr)	/* in: mtr having the latch
				on the record page */
{
	ulint		root_page_no;
	ulint		space;
	ulint		type;
	dulint		index_id;
	byte*		ptr;
	ulint		len;
	ibool		comp;
	dict_index_t*	index;

#ifdef UNIV_SYNC_DEBUG
	ut_ad(mutex_own(&(dict_sys->mutex)));
#endif /* UNIV_SYNC_DEBUG */

	ut_a(!dict_sys->sys_indexes->comp);
	ptr = rec_get_nth_field_old(rec, DICT_SYS_INDEXES_PAGE_NO_FIELD, &len);

	ut_ad(len == 4);

	root_page_no = mtr_read_ulint(ptr, MLOG_4BYTES, mtr);

	if (root_page_no == FIL_NULL) {
		/* The tree has been freed. */

		return;
	}

	ptr = rec_get_nth_field_old(rec,
				DICT_SYS_INDEXES_SPACE_NO_FIELD, &len);

	ut_ad(len == 4);

	space = mtr_read_ulint(ptr, MLOG_4BYTES, mtr);

	if (!fil_tablespace_exists_in_mem(space)) {
		/* It is a single table tablespace and the .ibd file is
		missing: do nothing */

		return;
	}

	ptr = rec_get_nth_field_old(rec,
				DICT_SYS_INDEXES_TYPE_FIELD, &len);
	ut_ad(len == 4);
	type = mach_read_from_4(ptr);

	ptr = rec_get_nth_field_old(rec, 1, &len);
	ut_ad(len == 8);
	index_id = mach_read_from_8(ptr);

	/* We free all the pages but the root page first; this operation
	may span several mini-transactions */

	btr_free_but_not_root(space, root_page_no);

	/* Then we free the root page in the same mini-transaction where
	we create the b-tree and write its new root page number to the
	appropriate field in the SYS_INDEXES record: this mini-transaction
	marks the B-tree totally truncated */

	comp = page_is_comp(btr_page_get(
				space, root_page_no, RW_X_LATCH, mtr));

	btr_free_root(space, root_page_no, mtr);

	/* Find the index corresponding to this SYS_INDEXES record. */
	for (index = UT_LIST_GET_FIRST(table->indexes);
			index;
			index = UT_LIST_GET_NEXT(indexes, index)) {
		if (!ut_dulint_cmp(index->id, index_id)) {
			break;
		}
	}

	root_page_no = btr_create(type, space, index_id, comp, mtr);
	if (index) {
		index->page_no = root_page_no;
	}

	page_rec_write_index_page_no(rec,
				DICT_SYS_INDEXES_PAGE_NO_FIELD,
				root_page_no, mtr);
}

/*************************************************************************
Creates a table create graph. */

+1 −0
Original line number Diff line number Diff line
@@ -119,6 +119,7 @@ dict_create(void);
clustered index */
#define DICT_SYS_INDEXES_PAGE_NO_FIELD	 8
#define DICT_SYS_INDEXES_SPACE_NO_FIELD	 7
#define DICT_SYS_INDEXES_TYPE_FIELD	 6
					
/* When a row id which is zero modulo this number (which must be a power of
two) is assigned, the field DICT_HDR_ROW_ID on the dictionary header page is
+11 −0
Original line number Diff line number Diff line
@@ -54,6 +54,17 @@ dict_create_index_step(
				/* out: query thread to run next or NULL */
	que_thr_t*	thr);	/* in: query thread */
/***********************************************************************
Truncates the index tree associated with a row in SYS_INDEXES table. */

void
dict_truncate_index_tree(
/*=====================*/
	dict_table_t*	table,	/* in: the table the index belongs to */
	rec_t*		rec,	/* in: record in the clustered index of
				SYS_INDEXES table */
	mtr_t*		mtr);	/* in: mtr having the latch
				on the record page */
/***********************************************************************
Drops the index tree associated with a row in SYS_INDEXES table. */

void
+9 −0
Original line number Diff line number Diff line
@@ -363,6 +363,15 @@ row_get_background_drop_list_len_low(void);
/*======================================*/
					/* out: how many tables in list */
/*************************************************************************
Truncates a table for MySQL. */

int
row_truncate_table_for_mysql(
/*=========================*/
				/* out: error code or DB_SUCCESS */
	dict_table_t*	table,	/* in: table handle */
	trx_t*		trx);	/* in: transaction handle */
/*************************************************************************
Drops a table for MySQL. If the name of the dropped table ends to
characters INNODB_MONITOR, then this also stops printing of monitor
output by the master thread. */
Loading