diff -r ab59095a9cbf include/my_time.h
--- a/include/my_time.h	Tue Aug 14 12:28:04 2007 +0200
+++ b/include/my_time.h	Wed Oct 31 12:01:13 2007 +0100
@@ -140,7 +140,7 @@ int my_date_to_str(const MYSQL_TIME *l_t
 int my_date_to_str(const MYSQL_TIME *l_time, char *to);
 int my_datetime_to_str(const MYSQL_TIME *l_time, char *to);
 int my_TIME_to_str(const MYSQL_TIME *l_time, char *to);
-
+ulonglong my_timer(ulonglong *ltime, ulonglong frequency);
 C_MODE_END
 
 #endif /* _my_time_h_ */
diff -r ab59095a9cbf innobase/buf/buf0buf.c
--- a/innobase/buf/buf0buf.c	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/buf/buf0buf.c	Wed Oct 31 12:01:13 2007 +0100
@@ -37,6 +37,7 @@ Created 11/5/1995 Heikki Tuuri
 #include "log0log.h"
 #include "trx0undo.h"
 #include "srv0srv.h"
+#include "thr0loc.h"
 
 /*
 		IMPLEMENTATION OF THE BUFFER POOL
@@ -1086,6 +1087,31 @@ buf_page_reset_file_page_was_freed(
 	return(block);
 }
 
+inline void _increment_page_get_statistics(buf_block_t* block, trx_t* trx)
+{
+	ulint           block_hash;
+	ulint           block_hash_byte;
+	byte            block_hash_offset;
+
+	ut_ad(block);
+
+	if (!trx || !trx->distinct_page_access_hash)
+		return;
+
+        block_hash = ut_hash_ulint((block->space << 20) + block->space +
+					block->offset, DPAH_SIZE << 3);
+	block_hash_byte = block_hash >> 3;
+	block_hash_offset = (byte) block_hash & 0x07;
+	if (block_hash_byte < 0 || block_hash_byte >= DPAH_SIZE)
+		fprintf(stderr, "!!! block_hash_byte = %lu  block_hash_offset = %lu !!!\n", block_hash_byte, block_hash_offset);
+	if (block_hash_offset < 0 || block_hash_offset > 7)
+		fprintf(stderr, "!!! block_hash_byte = %lu  block_hash_offset = %lu !!!\n", block_hash_byte, block_hash_offset);
+	if ((trx->distinct_page_access_hash[block_hash_byte] & ((byte) 0x01 << block_hash_offset)) == 0)
+		trx->distinct_page_access++;
+	trx->distinct_page_access_hash[block_hash_byte] |= (byte) 0x01 << block_hash_offset;
+	return;
+}
+
 /************************************************************************
 This is the general function used to get access to a database page. */
 
@@ -1108,6 +1134,11 @@ buf_page_get_gen(
 	ulint		fix_type;
 	ibool		success;
 	ibool		must_read;
+	trx_t*          trx;
+	ulint           sec;
+	ulint           ms;
+	ib_longlong     start_time;
+	ib_longlong     finish_time;
 	
 	ut_ad(mtr);
 	ut_ad((rw_latch == RW_S_LATCH)
@@ -1119,6 +1150,7 @@ buf_page_get_gen(
 #ifndef UNIV_LOG_DEBUG
 	ut_ad(!ibuf_inside() || ibuf_page(space, offset));
 #endif
+	trx = thr_local_get_trx(os_thread_get_curr_id());
 	buf_pool->n_page_gets++;
 loop:
 	block = NULL;
@@ -1148,7 +1180,7 @@ loop:
 			return(NULL);
 		}
 
-		buf_read_page(space, offset);
+		buf_read_page(space, offset, trx);
 
 #ifdef UNIV_DEBUG
 		buf_dbg_counter++;
@@ -1253,6 +1285,11 @@ loop:
 		        /* Let us wait until the read operation
 			completes */
 
+			if (trx)
+			{
+				ut_usectime(&sec, &ms);
+				start_time = (ib_longlong)sec * 1000000 + ms;
+			}
 		        for (;;) {
 				mutex_enter(&block->mutex);
 
@@ -1268,6 +1305,12 @@ loop:
 				       break;
 				}
 			}
+                	if (trx)
+			{
+				ut_usectime(&sec, &ms);
+        	        	finish_time = (ib_longlong)sec * 1000000 + ms;
+                		trx->io_reads_wait_timer += (ulint)(finish_time - start_time);
+			}
 		}
 
 		fix_type = MTR_MEMO_BUF_FIX;
@@ -1288,12 +1331,15 @@ loop:
 		/* In the case of a first access, try to apply linear
 		read-ahead */
 
-		buf_read_ahead_linear(space, offset);
+		buf_read_ahead_linear(space, offset, trx);
 	}
 
 #ifdef UNIV_IBUF_DEBUG
 	ut_a(ibuf_count_get(block->space, block->offset) == 0);
 #endif
+
+	_increment_page_get_statistics(block, trx);
+	
 	return(block->frame);		
 }
 
@@ -1318,6 +1364,7 @@ buf_page_optimistic_get_func(
 	ibool		accessed;
 	ibool		success;
 	ulint		fix_type;
+	trx_t*          trx;
 
 	ut_ad(mtr && block);
 	ut_ad((rw_latch == RW_S_LATCH) || (rw_latch == RW_X_LATCH));
@@ -1415,13 +1462,16 @@ buf_page_optimistic_get_func(
 		read-ahead */
 
 		buf_read_ahead_linear(buf_frame_get_space_id(guess),
-					buf_frame_get_page_no(guess));
+					buf_frame_get_page_no(guess), trx);
 	}
 
 #ifdef UNIV_IBUF_DEBUG
 	ut_a(ibuf_count_get(block->space, block->offset) == 0);
 #endif
 	buf_pool->n_page_gets++;
+
+	trx = thr_local_get_trx(os_thread_get_curr_id());
+	_increment_page_get_statistics(block, trx);
 
 	return(TRUE);
 }
@@ -1445,6 +1495,7 @@ buf_page_get_known_nowait(
 	buf_block_t*	block;
 	ibool		success;
 	ulint		fix_type;
+	trx_t*		trx;
 
 	ut_ad(mtr);
 	ut_ad((rw_latch == RW_S_LATCH) || (rw_latch == RW_X_LATCH));
@@ -1525,6 +1576,9 @@ buf_page_get_known_nowait(
 		|| (ibuf_count_get(block->space, block->offset) == 0));
 #endif
 	buf_pool->n_page_gets++;
+
+	trx = thr_local_get_trx(os_thread_get_curr_id());
+	_increment_page_get_statistics(block, trx);
 
 	return(TRUE);
 }
diff -r ab59095a9cbf innobase/buf/buf0rea.c
--- a/innobase/buf/buf0rea.c	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/buf/buf0rea.c	Wed Oct 31 12:01:13 2007 +0100
@@ -70,7 +70,8 @@ buf_read_page_low(
 			treat the tablespace as dropped; this is a timestamp we
 			use to stop dangling page reads from a tablespace
 			which we have DISCARDed + IMPORTed back */
-	ulint	offset)	/* in: page number */
+	ulint	offset,	/* in: page number */
+	trx_t*  trx)
 {
 	buf_block_t*	block;
 	ulint		wake_later;
@@ -140,10 +141,10 @@ buf_read_page_low(
 
 	ut_a(block->state == BUF_BLOCK_FILE_PAGE);
 
-	*err = fil_io(OS_FILE_READ | wake_later,
+	*err = _fil_io(OS_FILE_READ | wake_later,
 			sync, space,
 			offset, 0, UNIV_PAGE_SIZE,
-			(void*)block->frame, (void*)block);
+			(void*)block->frame, (void*)block, trx);
 	ut_a(*err == DB_SUCCESS);
 
 	if (sync) {
@@ -174,8 +175,9 @@ buf_read_ahead_random(
 			the page at the given page number does not get
 			read even if we return a value > 0! */
 	ulint	space,	/* in: space id */
-	ulint	offset)	/* in: page number of a page which the current thread
+	ulint	offset,	/* in: page number of a page which the current thread
 			wants to access */
+	trx_t*  trx)
 {
 	ib_longlong	tablespace_version;
 	buf_block_t*	block;
@@ -270,7 +272,7 @@ buf_read_ahead_random(
 		if (!ibuf_bitmap_page(i)) {
 			count += buf_read_page_low(&err, FALSE, ibuf_mode
 					| OS_AIO_SIMULATED_WAKE_LATER,
-				        space, tablespace_version, i);
+				        space, tablespace_version, i, trx);
 			if (err == DB_TABLESPACE_DELETED) {
 				ut_print_timestamp(stderr);
 				fprintf(stderr,
@@ -314,7 +316,8 @@ buf_read_page(
 			/* out: number of page read requests issued: this can
 			be > 1 if read-ahead occurred */
 	ulint	space,	/* in: space id */
-	ulint	offset)	/* in: page number */
+	ulint	offset,	/* in: page number */
+	trx_t*  trx)
 {
 	ib_longlong	tablespace_version;
 	ulint		count;
@@ -323,13 +326,13 @@ buf_read_page(
 
 	tablespace_version = fil_space_get_version(space);
 
-	count = buf_read_ahead_random(space, offset);
+	count = buf_read_ahead_random(space, offset, trx);
 
 	/* We do the i/o in the synchronous aio mode to save thread
 	switches: hence TRUE */
 
 	count2 = buf_read_page_low(&err, TRUE, BUF_READ_ANY_PAGE, space,
-					tablespace_version, offset);
+					tablespace_version, offset, trx);
         srv_buf_pool_reads+= count2;
 	if (err == DB_TABLESPACE_DELETED) {
 	        ut_print_timestamp(stderr);
@@ -374,8 +377,9 @@ buf_read_ahead_linear(
 /*==================*/
 			/* out: number of page read requests issued */
 	ulint	space,	/* in: space id */
-	ulint	offset)	/* in: page number of a page; NOTE: the current thread
+	ulint	offset,	/* in: page number of a page; NOTE: the current thread
 			must want access to this page (see NOTE 3 above) */
+	trx_t*  trx)
 {
 	ib_longlong	tablespace_version;
 	buf_block_t*	block;
@@ -556,7 +560,7 @@ buf_read_ahead_linear(
 		if (!ibuf_bitmap_page(i)) {
 			count += buf_read_page_low(&err, FALSE, ibuf_mode
 					| OS_AIO_SIMULATED_WAKE_LATER,
-					space, 	tablespace_version, i);
+					space, 	tablespace_version, i, trx);
 			if (err == DB_TABLESPACE_DELETED) {
 				ut_print_timestamp(stderr);
 				fprintf(stderr,
@@ -625,10 +629,10 @@ buf_read_ibuf_merge_pages(
 	for (i = 0; i < n_stored; i++) {
 		if ((i + 1 == n_stored) && sync) {
 			buf_read_page_low(&err, TRUE, BUF_READ_ANY_PAGE,
-				space_ids[i], space_versions[i], page_nos[i]);
+				space_ids[i], space_versions[i], page_nos[i], NULL);
 		} else {
 			buf_read_page_low(&err, FALSE, BUF_READ_ANY_PAGE,
-				space_ids[i], space_versions[i], page_nos[i]);
+				space_ids[i], space_versions[i], page_nos[i], NULL);
 		}
 
 		if (err == DB_TABLESPACE_DELETED) {
@@ -704,11 +708,11 @@ buf_read_recv_pages(
 
 		if ((i + 1 == n_stored) && sync) {
 			buf_read_page_low(&err, TRUE, BUF_READ_ANY_PAGE, space,
-					tablespace_version, page_nos[i]);
+					tablespace_version, page_nos[i], NULL);
 		} else {
 			buf_read_page_low(&err, FALSE, BUF_READ_ANY_PAGE
 					| OS_AIO_SIMULATED_WAKE_LATER,
-				       space, tablespace_version, page_nos[i]);
+				       space, tablespace_version, page_nos[i], NULL);
 		}
 	}
 	
diff -r ab59095a9cbf innobase/fil/fil0fil.c
--- a/innobase/fil/fil0fil.c	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/fil/fil0fil.c	Wed Oct 31 12:01:13 2007 +0100
@@ -3527,7 +3527,7 @@ fil_extend_space_to_desired_size(
 			node->name, node->handle, buf,
 			offset_low, offset_high,
 			UNIV_PAGE_SIZE * n_pages,
-			NULL, NULL);
+			NULL, NULL, NULL);
 #endif
 		if (success) {
 			node->size += n_pages;
@@ -3851,7 +3851,7 @@ Reads or writes data. This operation is 
 Reads or writes data. This operation is asynchronous (aio). */
 
 ulint
-fil_io(
+_fil_io(
 /*===*/
 				/* out: DB_SUCCESS, or DB_TABLESPACE_DELETED
 				if we are trying to do i/o on a tablespace
@@ -3877,8 +3877,9 @@ fil_io(
 	void*	buf,		/* in/out: buffer where to store read data
 				or from where to write; in aio this must be
 				appropriately aligned */
-	void*	message)	/* in: message for aio handler if non-sync
+	void*	message,	/* in: message for aio handler if non-sync
 				aio used, else ignored */
+	trx_t*  trx)
 {
 	fil_system_t*	system		= fil_system;
 	ulint		mode;
@@ -4018,7 +4019,7 @@ fil_io(
 #else
 	/* Queue the aio request */
 	ret = os_aio(type, mode | wake_later, node->name, node->handle, buf,
-				offset_low, offset_high, len, node, message);
+				offset_low, offset_high, len, node, message, trx);
 #endif
 	ut_a(ret);
 
diff -r ab59095a9cbf innobase/include/buf0rea.h
--- a/innobase/include/buf0rea.h	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/include/buf0rea.h	Wed Oct 31 12:01:13 2007 +0100
@@ -10,6 +10,7 @@ Created 11/5/1995 Heikki Tuuri
 #define buf0rea_h
 
 #include "univ.i"
+#include "trx0types.h"
 #include "buf0types.h"
 
 /************************************************************************
@@ -25,7 +26,8 @@ buf_read_page(
 			/* out: number of page read requests issued: this can
 			be > 1 if read-ahead occurred */
 	ulint	space,	/* in: space id */
-	ulint	offset);/* in: page number */
+	ulint	offset,	/* in: page number */
+	trx_t*  trx);
 /************************************************************************
 Applies linear read-ahead if in the buf_pool the page is a border page of
 a linear read-ahead area and all the pages in the area have been accessed.
@@ -55,8 +57,9 @@ buf_read_ahead_linear(
 /*==================*/
 			/* out: number of page read requests issued */
 	ulint	space,	/* in: space id */
-	ulint	offset);/* in: page number of a page; NOTE: the current thread
+	ulint	offset,	/* in: page number of a page; NOTE: the current thread
 			must want access to this page (see NOTE 3 above) */
+	trx_t*  trx);
 /************************************************************************
 Issues read requests for pages which the ibuf module wants to read in, in
 order to contract the insert buffer tree. Technically, this function is like
diff -r ab59095a9cbf innobase/include/fil0fil.h
--- a/innobase/include/fil0fil.h	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/include/fil0fil.h	Wed Oct 31 12:01:13 2007 +0100
@@ -534,8 +534,11 @@ fil_space_get_n_reserved_extents(
 /************************************************************************
 Reads or writes data. This operation is asynchronous (aio). */
 
-ulint
-fil_io(
+#define fil_io(type, sync, space_id, block_offset, byte_offset, len, buf, message) \
+	_fil_io(type, sync, space_id, block_offset, byte_offset, len, buf, message, NULL)
+
+ulint
+_fil_io(
 /*===*/
 				/* out: DB_SUCCESS, or DB_TABLESPACE_DELETED
 				if we are trying to do i/o on a tablespace
@@ -561,8 +564,9 @@ fil_io(
 	void*	buf,		/* in/out: buffer where to store read data
 				or from where to write; in aio this must be
 				appropriately aligned */
-	void*	message);	/* in: message for aio handler if non-sync
+	void*	message,	/* in: message for aio handler if non-sync
 				aio used, else ignored */
+	trx_t*  trx);
 /************************************************************************
 Reads data from a space to a buffer. Remember that the possible incomplete
 blocks at the end of file are ignored: they are not taken into account when
diff -r ab59095a9cbf innobase/include/os0file.h
--- a/innobase/include/os0file.h	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/include/os0file.h	Wed Oct 31 12:01:13 2007 +0100
@@ -10,6 +10,8 @@ Created 10/21/1995 Heikki Tuuri
 #define os0file_h
 
 #include "univ.i"
+
+#include "trx0types.h"
 
 #ifndef __WIN__
 #include <dirent.h>
@@ -420,8 +422,11 @@ os_file_get_last_error(
 /***********************************************************************
 Requests a synchronous read operation. */
 
-ibool
-os_file_read(
+#define os_file_read(file, buf, offset, offset_high, n)         \
+		_os_file_read(file, buf, offset, offset_high, n, NULL)
+
+ibool
+_os_file_read(
 /*=========*/
 				/* out: TRUE if request was
 				successful, FALSE if fail */
@@ -431,7 +436,8 @@ os_file_read(
 				offset where to read */
 	ulint		offset_high,/* in: most significant 32 bits of
 				offset */
-	ulint		n);	/* in: number of bytes to read */	
+	ulint		n,	/* in: number of bytes to read */
+	trx_t*		trx);
 /***********************************************************************
 Rewind file to its start, read at most size - 1 bytes from it to str, and
 NUL-terminate str. All errors are silently ignored. This function is
@@ -583,7 +589,8 @@ os_aio(
 				can be used to identify a completed aio
 				operation); if mode is OS_AIO_SYNC, these
 				are ignored */
-	void*		message2);
+	void*		message2,
+	trx_t*          trx);
 /****************************************************************************
 Wakes up all async i/o threads so that they know to exit themselves in
 shutdown. */
diff -r ab59095a9cbf innobase/include/thr0loc.h
--- a/innobase/include/thr0loc.h	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/include/thr0loc.h	Wed Oct 31 12:01:13 2007 +0100
@@ -15,6 +15,7 @@ OS handle to the current thread, or its 
 
 #include "univ.i"
 #include "os0thread.h"
+#include "trx0trx.h"
 
 /********************************************************************
 Initializes the thread local storage module. */
@@ -36,6 +37,14 @@ thr_local_free(
 /*===========*/
 	os_thread_id_t	id);	/* in: thread id */
 /***********************************************************************
+Gets trx */
+
+trx_t*
+thr_local_get_trx(
+/*==================*/
+				/* out: trx for mysql */
+	os_thread_id_t	id);	/* in: thread id of the thread */
+/***********************************************************************
 Gets the slot number in the thread table of a thread. */
 
 ulint
@@ -43,6 +52,14 @@ thr_local_get_slot_no(
 /*==================*/
 				/* out: slot number */
 	os_thread_id_t	id);	/* in: thread id of the thread */
+/***********************************************************************
+Sets in the local storage the slot number in the thread table of a thread. */
+
+void
+thr_local_set_trx(
+/*==================*/
+	os_thread_id_t	id,	/* in: thread id of the thread */
+	trx_t*		trx);	/* in: slot number */
 /***********************************************************************
 Sets in the local storage the slot number in the thread table of a thread. */
 
diff -r ab59095a9cbf innobase/include/trx0trx.h
--- a/innobase/include/trx0trx.h	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/include/trx0trx.h	Wed Oct 31 12:01:13 2007 +0100
@@ -668,6 +668,17 @@ struct trx_struct{
 	/*------------------------------*/
 	char detailed_error[256];	/* detailed error message for last
 					error, or empty. */
+	/*------------------------------*/
+	os_thread_id_t	trx_thread_id;
+	ulint		io_reads;
+	ib_longlong     io_read;
+	ulint		io_reads_wait_timer;
+	ib_longlong     lock_que_wait_ustarted;
+	ulint           lock_que_wait_timer;
+	ulint           innodb_que_wait_timer;
+	ulint           distinct_page_access;
+#define	DPAH_SIZE	8192
+	byte*		distinct_page_access_hash;
 };
 
 #define TRX_MAX_N_THREADS	32	/* maximum number of concurrent
diff -r ab59095a9cbf innobase/lock/lock0lock.c
--- a/innobase/lock/lock0lock.c	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/lock/lock0lock.c	Wed Oct 31 12:01:13 2007 +0100
@@ -1806,6 +1806,8 @@ lock_rec_enqueue_waiting(
 {
 	lock_t*	lock;
 	trx_t*	trx;
+	ulint   sec;
+	ulint   ms;
 	
 #ifdef UNIV_SYNC_DEBUG
 	ut_ad(mutex_own(&kernel_mutex));
@@ -1861,6 +1863,8 @@ lock_rec_enqueue_waiting(
 	trx->que_state = TRX_QUE_LOCK_WAIT;
 	trx->was_chosen_as_deadlock_victim = FALSE;
 	trx->wait_started = time(NULL);
+	ut_usectime(&sec, &ms);
+	trx->lock_que_wait_ustarted = (ib_longlong)sec * 1000000 + ms;
 
 	ut_a(que_thr_stop(thr));
 
@@ -3514,7 +3518,9 @@ lock_table_enqueue_waiting(
 {
 	lock_t*	lock;
 	trx_t*	trx;
-	
+	ulint   sec;
+	ulint   ms;
+
 #ifdef UNIV_SYNC_DEBUG
 	ut_ad(mutex_own(&kernel_mutex));
 #endif /* UNIV_SYNC_DEBUG */
@@ -3563,7 +3569,10 @@ lock_table_enqueue_waiting(
 	
 		return(DB_SUCCESS);
 	}
-	
+
+	trx->wait_started = time(NULL);
+	ut_usectime(&sec, &ms);
+	trx->lock_que_wait_ustarted = (ib_longlong)sec * 1000000 + ms;
 	trx->que_state = TRX_QUE_LOCK_WAIT;
 	trx->was_chosen_as_deadlock_victim = FALSE;
 	trx->wait_started = time(NULL);
@@ -4289,7 +4298,7 @@ lock_print_info_all_transactions(
 	ulint	i;
 	mtr_t	mtr;
 	trx_t*	trx;
-
+	
 	fprintf(file, "LIST OF TRANSACTIONS FOR EACH SESSION:\n");
 
 	/* First print info on non-active transactions */
diff -r ab59095a9cbf innobase/os/os0file.c
--- a/innobase/os/os0file.c	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/os/os0file.c	Wed Oct 31 12:01:13 2007 +0100
@@ -14,6 +14,7 @@ Created 10/21/1995 Heikki Tuuri
 #include "srv0start.h"
 #include "fil0fil.h"
 #include "buf0buf.h"
+#include "trx0sys.h"
 
 #if defined(UNIV_HOTBACKUP) && defined(__WIN__)
 /* Add includes for the _stat() call to compile on Windows */
@@ -101,6 +102,7 @@ struct os_aio_slot_struct{
 	struct aiocb	control;	/* Posix control block for aio
 					request */
 #endif
+        trx_t*		trx;
 };
 
 /* The aio array structure */
@@ -1889,9 +1891,13 @@ os_file_flush(
 #ifndef __WIN__
 /***********************************************************************
 Does a synchronous read operation in Posix. */
+
+#define os_file_pread(file, buf, n, offset, offset_high)        \
+		_os_file_pread(file, buf, n, offset, offset_high, NULL);
+
 static
 ssize_t
-os_file_pread(
+_os_file_pread(
 /*==========*/
 				/* out: number of bytes read, -1 if error */
 	os_file_t	file,	/* in: handle to a file */
@@ -1899,12 +1905,17 @@ os_file_pread(
 	ulint		n,	/* in: number of bytes to read */	
 	ulint		offset,	/* in: least significant 32 bits of file
 				offset from where to read */
-	ulint		offset_high) /* in: most significant 32 bits of
+	ulint		offset_high, /* in: most significant 32 bits of
 				offset */
+        trx_t*		trx)
 {
         off_t	offs;
 	ssize_t	n_bytes;
-
+	ulint           sec;
+	ulint           ms;
+	ib_longlong     start_time;
+	ib_longlong     finish_time;
+	
 	ut_a((offset & 0xFFFFFFFFUL) == offset);
         
         /* If off_t is > 4 bytes in size, then we assume we can pass a
@@ -1923,7 +1934,13 @@ os_file_pread(
         }
 
 	os_n_file_reads++;
-
+	if (trx)
+	{
+	        trx->io_reads++;
+		trx->io_read += n;
+		ut_usectime(&sec, &ms);
+		start_time = (ib_longlong)sec * 1000000 + ms;
+	}
 #if defined(HAVE_PREAD) && !defined(HAVE_BROKEN_PREAD)
         os_mutex_enter(os_file_count_mutex);
 	os_file_n_pending_preads++;
@@ -1936,6 +1953,13 @@ os_file_pread(
 	os_file_n_pending_preads--;
 	os_n_pending_reads--;
         os_mutex_exit(os_file_count_mutex);
+
+        if (trx)
+        {
+		ut_usectime(&sec, &ms);
+        	finish_time = (ib_longlong)sec * 1000000 + ms;
+                trx->io_reads_wait_timer += (ulint)(finish_time - start_time);
+	}
 
 	return(n_bytes);
 #else
@@ -1966,6 +1990,13 @@ os_file_pread(
         os_mutex_enter(os_file_count_mutex);
 	os_n_pending_reads--;
         os_mutex_exit(os_file_count_mutex);
+
+        if (trx)
+        {
+		ut_usectime(&sec, &ms);
+        	finish_time = (ib_longlong)sec * 1000000 + ms;
+                trx->io_reads_wait_timer += (ulint)(finish_time - start_time);
+	}
 
 	return(ret);
 	}
@@ -2089,7 +2120,7 @@ Requests a synchronous positioned read o
 Requests a synchronous positioned read operation. */
 
 ibool
-os_file_read(
+_os_file_read(
 /*=========*/
 				/* out: TRUE if request was
 				successful, FALSE if fail */
@@ -2099,7 +2130,8 @@ os_file_read(
 				offset where to read */
 	ulint		offset_high, /* in: most significant 32 bits of
 				offset */
-	ulint		n)	/* in: number of bytes to read */	
+	ulint		n,	/* in: number of bytes to read */
+        trx_t*		trx)
 {
 #ifdef __WIN__
 	BOOL		ret;
@@ -2114,8 +2146,7 @@ os_file_read(
 
 	os_n_file_reads++;
 	os_bytes_read_since_printout += n;
-
-try_again:	
+try_again:
 	ut_ad(file);
 	ut_ad(buf);
 	ut_ad(n > 0);
@@ -2163,7 +2194,7 @@ try_again:
 	os_bytes_read_since_printout += n;
 
 try_again:
-	ret = os_file_pread(file, buf, n, offset, offset_high);
+	ret = _os_file_pread(file, buf, n, offset, offset_high, trx);
 
 	if ((ulint)ret == n) {
 
@@ -3123,7 +3154,8 @@ os_aio_array_reserve_slot(
 				offset */
 	ulint		offset_high, /* in: most significant 32 bits of
 				offset */
-	ulint		len)	/* in: length of the block to read or write */
+	ulint		len,	/* in: length of the block to read or write */
+	trx_t*          trx)
 {
 	os_aio_slot_t*	slot;
 #ifdef WIN_ASYNC_IO
@@ -3182,7 +3214,7 @@ loop:
 	slot->offset   = offset;
 	slot->offset_high = offset_high;
 	slot->io_already_done = FALSE;
-	
+
 #ifdef WIN_ASYNC_IO		
 	control = &(slot->control);
 	control->Offset = (DWORD)offset;
@@ -3376,7 +3408,8 @@ os_aio(
 				can be used to identify a completed aio
 				operation); if mode is OS_AIO_SYNC, these
 				are ignored */
-	void*		message2)
+	void*		message2,
+	trx_t*          trx)
 {
 	os_aio_array_t*	array;
 	os_aio_slot_t*	slot;
@@ -3415,8 +3448,8 @@ os_aio(
 		wait in the Windows case. */
 
 		if (type == OS_FILE_READ) {
-			return(os_file_read(file, buf, offset,
-							offset_high, n));
+			return(_os_file_read(file, buf, offset,
+							offset_high, n, trx));
 		}
 
 		ut_a(type == OS_FILE_WRITE);
@@ -3449,14 +3482,19 @@ try_again:
 		ut_error;
 	}
 	
+	if (trx && type == OS_FILE_READ)
+	{
+		trx->io_reads++;
+		trx->io_read += n;
+	}
 	slot = os_aio_array_reserve_slot(type, array, message1, message2, file,
-					name, buf, offset, offset_high, n);
+					name, buf, offset, offset_high, n, trx);
 	if (type == OS_FILE_READ) {
 		if (os_aio_use_native_aio) {
 #ifdef WIN_ASYNC_IO
 			os_n_file_reads++;
 			os_bytes_read_since_printout += len;
-			
+
 			ret = ReadFile(file, buf, (DWORD)n, &len,
 							&(slot->control));
 #elif defined(POSIX_ASYNC_IO)
@@ -4024,7 +4062,7 @@ consecutive_loop:
 
 			ut_memcpy(consecutive_ios[i]->buf, combined_buf + offs, 
 						consecutive_ios[i]->len);
-			offs += consecutive_ios[i]->len;
+			offs += consecutive_ios[i]->len;			
 		}
 	}
 
@@ -4036,9 +4074,8 @@ consecutive_loop:
 
 	/* Mark the i/os done in slots */
 
-	for (i = 0; i < n_consecutive; i++) {
+	for (i = 0; i < n_consecutive; i++) 
 		consecutive_ios[i]->io_already_done = TRUE;
-	}
 
 	/* We return the messages for the first slot now, and if there were
 	several slots, the messages will be returned with subsequent calls
diff -r ab59095a9cbf innobase/srv/srv0srv.c
--- a/innobase/srv/srv0srv.c	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/srv/srv0srv.c	Wed Oct 31 12:01:13 2007 +0100
@@ -996,6 +996,10 @@ srv_conc_enter_innodb(
 	ibool			has_slept = FALSE;
 	srv_conc_slot_t*	slot	  = NULL;
 	ulint			i;
+	ib_longlong             start_time = 0L;
+	ib_longlong             finish_time = 0L;
+	ulint                   sec;
+	ulint                   ms;
 
 	/* If trx has 'free tickets' to enter the engine left, then use one
 	such ticket */
@@ -1054,6 +1058,7 @@ retry:
     if (SRV_THREAD_SLEEP_DELAY > 0)
     {
       os_thread_sleep(SRV_THREAD_SLEEP_DELAY);
+      trx->innodb_que_wait_timer += SRV_THREAD_SLEEP_DELAY;
     }
 
 		trx->op_info = "";
@@ -1109,11 +1114,18 @@ retry:
 	/* Go to wait for the event; when a thread leaves InnoDB it will
 	release this thread */
 
+	ut_usectime(&sec, &ms);
+	start_time = (ib_longlong)sec * 1000000 + ms;
+
 	trx->op_info = "waiting in InnoDB queue";
 
 	os_event_wait(slot->event);
 
 	trx->op_info = "";
+
+	ut_usectime(&sec, &ms);
+	finish_time = (ib_longlong)sec * 1000000 + ms;
+	trx->innodb_que_wait_timer += (ulint)(finish_time - start_time);
 
 	os_fast_mutex_lock(&srv_conc_mutex);
 
diff -r ab59095a9cbf innobase/thr/thr0loc.c
--- a/innobase/thr/thr0loc.c	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/thr/thr0loc.c	Wed Oct 31 12:01:13 2007 +0100
@@ -45,6 +45,7 @@ struct thr_local_struct{
 				for this thread */
 	ibool		in_ibuf;/* TRUE if the the thread is doing an ibuf
 				operation */
+	trx_t*          trx;
 	hash_node_t	hash;	/* hash chain node */
 	ulint		magic_n;
 };
@@ -113,6 +114,29 @@ thr_local_get_slot_no(
 }
 
 /***********************************************************************
+Gets trx */
+
+trx_t*
+thr_local_get_trx(
+/*==================*/
+				/* out: trx for mysql */
+	os_thread_id_t	id)	/* in: thread id of the thread */
+{
+	trx_t*          trx;
+	thr_local_t*	local;
+
+	mutex_enter(&thr_local_mutex);
+
+	local = thr_local_get(id);
+
+	trx = local->trx;
+
+	mutex_exit(&thr_local_mutex);
+
+	return(trx);
+}
+
+/***********************************************************************
 Sets the slot number in the thread table of a thread. */
 
 void
@@ -124,11 +148,31 @@ thr_local_set_slot_no(
 	thr_local_t*	local;
 
 	mutex_enter(&thr_local_mutex);
-	
+
 	local = thr_local_get(id);
 
 	local->slot_no = slot_no;
-	
+
+	mutex_exit(&thr_local_mutex);
+}
+
+/***********************************************************************
+Sets trx */
+
+void
+thr_local_set_trx(
+/*==================*/
+	os_thread_id_t	id,	/* in: thread id of the thread */
+	trx_t*		trx)	/* in: trx */
+{
+	thr_local_t*	local;
+
+	mutex_enter(&thr_local_mutex);
+
+	local = thr_local_get(id);
+
+	local->trx = trx;
+
 	mutex_exit(&thr_local_mutex);
 }
 
@@ -172,6 +216,7 @@ thr_local_create(void)
 	local->magic_n = THR_LOCAL_MAGIC_N;
 
  	local->in_ibuf = FALSE;
+ 	local->trx = NULL;
 	
 	mutex_enter(&thr_local_mutex);
 
diff -r ab59095a9cbf innobase/trx/trx0trx.c
--- a/innobase/trx/trx0trx.c	Tue Aug 14 12:28:04 2007 +0200
+++ b/innobase/trx/trx0trx.c	Wed Oct 31 12:01:13 2007 +0100
@@ -190,6 +190,16 @@ trx_create(
 	trx->global_read_view_heap = mem_heap_create(256);
 	trx->global_read_view = NULL;
 	trx->read_view = NULL;
+	
+	trx->io_reads = 0;
+	trx->io_read = 0;
+	trx->io_reads_wait_timer = 0;
+	trx->lock_que_wait_timer = 0;
+	trx->innodb_que_wait_timer = 0;
+	trx->distinct_page_access = 0;
+	trx->distinct_page_access_hash = NULL;
+	trx->trx_thread_id = os_thread_get_curr_id();
+	thr_local_set_trx(trx->trx_thread_id, NULL);
 
 	/* Set X/Open XA transaction identification to NULL */
 	memset(&trx->xid, 0, sizeof(trx->xid));
@@ -230,6 +240,10 @@ trx_allocate_for_mysql(void)
 
 	trx->mysql_process_no = os_proc_get_number();
 	
+	trx->distinct_page_access_hash = mem_alloc(DPAH_SIZE);
+	memset(trx->distinct_page_access_hash, 0, DPAH_SIZE);
+	thr_local_set_trx(trx->mysql_thread_id, trx);
+
 	return(trx);
 }
 
@@ -355,6 +369,8 @@ trx_free(
 
 	ut_a(trx->read_view == NULL);
 	
+	thr_local_free(trx->trx_thread_id);
+	
 	mem_free(trx);
 }
 
@@ -366,6 +382,12 @@ trx_free_for_mysql(
 /*===============*/
 	trx_t*	trx)	/* in, own: trx object */
 {
+	if (trx->distinct_page_access_hash)
+	{
+		mem_free(trx->distinct_page_access_hash);
+		trx->distinct_page_access_hash= NULL;
+	}
+
 	thr_local_free(trx->mysql_thread_id);
 
 	mutex_enter(&kernel_mutex);
@@ -1064,7 +1086,10 @@ trx_end_lock_wait(
 	trx_t*	trx)	/* in: transaction */
 {
 	que_thr_t*	thr;
-
+	ulint           sec;
+	ulint           ms;
+	ib_longlong     now;
+	
 #ifdef UNIV_SYNC_DEBUG
 	ut_ad(mutex_own(&kernel_mutex));
 #endif /* UNIV_SYNC_DEBUG */
@@ -1080,6 +1105,9 @@ trx_end_lock_wait(
 		thr = UT_LIST_GET_FIRST(trx->wait_thrs);
 	}
 
+	ut_usectime(&sec, &ms);
+	now = (ib_longlong)sec * 1000000 + ms;
+	trx->lock_que_wait_timer += (ulint)(now - trx->lock_que_wait_ustarted);
 	trx->que_state = TRX_QUE_RUNNING;
 }
 
@@ -1093,6 +1121,9 @@ trx_lock_wait_to_suspended(
 	trx_t*	trx)	/* in: transaction in the TRX_QUE_LOCK_WAIT state */
 {
 	que_thr_t*	thr;
+	ulint           sec;
+	ulint           ms;
+	ib_longlong     now;
 
 #ifdef UNIV_SYNC_DEBUG
 	ut_ad(mutex_own(&kernel_mutex));
@@ -1109,6 +1140,9 @@ trx_lock_wait_to_suspended(
 		thr = UT_LIST_GET_FIRST(trx->wait_thrs);
 	}
 
+	ut_usectime(&sec, &ms);
+	now = (ib_longlong)sec * 1000000 + ms;
+	trx->lock_que_wait_timer += (ulint)(now - trx->lock_que_wait_ustarted);
 	trx->que_state = TRX_QUE_RUNNING;
 }
 
diff -r ab59095a9cbf scripts/mysqldumpslow.sh
--- a/scripts/mysqldumpslow.sh	Tue Aug 14 12:28:04 2007 +0200
+++ b/scripts/mysqldumpslow.sh	Wed Oct 31 12:01:13 2007 +0100
@@ -83,8 +83,8 @@ while ( defined($_ = shift @pending) or 
     s/^#? Time: \d{6}\s+\d+:\d+:\d+.*\n//;
     my ($user,$host) = s/^#? User\@Host:\s+(\S+)\s+\@\s+(\S+).*\n// ? ($1,$2) : ('','');
 
-    s/^# Query_time: (\d+)  Lock_time: (\d+)  Rows_sent: (\d+).*\n//;
-    my ($t, $l, $r) = ($1, $2, $3);
+    s/^# Query_time: (\d+(\.\d+)?)  Lock_time: (\d+(\.\d+)?)  Rows_sent: (\d+(\.\d+)?).*\n//;
+    my ($t, $l, $r) = ($1, $3, $5);
     $t -= $l unless $opt{l};
 
     # remove fluff that mysqld writes to log when it (re)starts:
diff -r ab59095a9cbf sql-common/my_time.c
--- a/sql-common/my_time.c	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql-common/my_time.c	Wed Oct 31 12:01:13 2007 +0100
@@ -1250,3 +1250,37 @@ ulonglong TIME_to_ulonglong(const MYSQL_
   return 0;
 }
 
+/*
+ int my_timer(ulonglong *ltime, ulonglong frequency)
+
+ For performance measurement this function returns the number
+ of microseconds since the epoch (SVr4, BSD 4.3, POSIX 1003.1-2001)
+ or system start (Windows platforms).
+
+ For windows platforms frequency value (obtained via 
+ QueryPerformanceFrequency) has to be specified. The global frequency
+ value is set in mysqld.cc. 
+
+ If Windows platform doesn't support QueryPerformanceFrequency we will
+ obtain the time via GetClockCount, which supports microseconds only.
+*/
+
+ulonglong my_timer(ulonglong *ltime, ulonglong frequency)
+{
+  ulonglong newtime= 0;
+#ifdef __WIN__
+  if (frequency) 
+  {
+    QueryPerformanceCounter((LARGE_INTEGER *)&newtime);
+    newtime/= (frequency * 1000000);
+  } else
+    newtime= (GetTickCount() * 1000; /* GetTickCount only returns milliseconds */
+#else
+  struct timeval t;
+  gettimeofday(&t, NULL);
+  newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
+#endif
+  if (ltime)
+    *ltime= newtime;
+  return newtime;
+}
diff -r ab59095a9cbf sql/filesort.cc
--- a/sql/filesort.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/filesort.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -172,6 +172,7 @@ ha_rows filesort(THD *thd, TABLE *table,
   {
     statistic_increment(thd->status_var.filesort_scan_count, &LOCK_status);
   }
+  thd->used_filesort= TRUE;
 #ifdef CAN_TRUST_RANGE
   if (select && select->quick && select->quick->records > 0L)
   {
@@ -238,6 +239,7 @@ ha_rows filesort(THD *thd, TABLE *table,
   }
   else
   {
+    thd->used_filesort_on_disk= TRUE;
     if (!table_sort.buffpek && table_sort.buffpek_len < maxbuffer &&
         !(table_sort.buffpek=
           (byte *) read_buffpek_from_file(&buffpek_pointers, maxbuffer)))
@@ -1058,6 +1060,7 @@ int merge_buffers(SORTPARAM *param, IO_C
 
   statistic_increment(current_thd->status_var.filesort_merge_passes,
 		      &LOCK_status);
+  current_thd->used_filesort_pass++;
   if (param->not_killable)
   {
     killed= &not_killable;
diff -r ab59095a9cbf sql/ha_innodb.cc
--- a/sql/ha_innodb.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/ha_innodb.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -1,3 +1,4 @@
+
 /* Copyright (C) 2000-2005 MySQL AB & Innobase Oy
 
    This program is free software; you can redistribute it and/or modify
@@ -6038,6 +6039,7 @@ ha_innobase::external_lock(
 {
 	row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt;
 	trx_t*		trx;
+	int i;
 
   	DBUG_ENTER("ha_innobase::external_lock");
 	DBUG_PRINT("enter",("lock_type: %d", lock_type));
@@ -6155,7 +6157,24 @@ ha_innobase::external_lock(
 
 	if (trx->n_mysql_tables_in_use == 0) {
 
-	        trx->mysql_n_tables_locked = 0;
+		current_thd->innodb_was_used = TRUE;
+		current_thd->innodb_io_reads += trx->io_reads;
+		current_thd->innodb_io_read += trx->io_read;
+		current_thd->innodb_io_reads_wait_timer += trx->io_reads_wait_timer;
+		current_thd->innodb_lock_que_wait_timer += trx->lock_que_wait_timer;
+		current_thd->innodb_innodb_que_wait_timer += trx->innodb_que_wait_timer;
+                current_thd->innodb_page_access += trx->distinct_page_access;
+
+		trx->io_reads = 0;
+		trx->io_read = 0;
+		trx->io_reads_wait_timer = 0;
+		trx->lock_que_wait_timer = 0;
+		trx->innodb_que_wait_timer = 0;
+		trx->distinct_page_access = 0;
+		if (trx->distinct_page_access_hash)
+			memset(trx->distinct_page_access_hash, 0, DPAH_SIZE);
+
+    		trx->mysql_n_tables_locked = 0;
 		prebuilt->used_in_HANDLER = FALSE;
 
 		/* Release a possible FIFO ticket and search latch. Since we
diff -r ab59095a9cbf sql/log.cc
--- a/sql/log.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/log.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -1930,10 +1930,11 @@ err:
 */
 
 bool MYSQL_LOG::write(THD *thd,const char *query, uint query_length,
-		      time_t query_start_arg)
+		      time_t query_start_arg, ulonglong query_start_timer)
 {
   bool error=0;
   time_t current_time;
+  ulonglong current_timer;
   if (!is_open())
     return 0;
   DBUG_ENTER("MYSQL_LOG::write");
@@ -1944,7 +1945,8 @@ bool MYSQL_LOG::write(THD *thd,const cha
     int tmp_errno=0;
     char buff[80],*end;
     end=buff;
-    if (!(thd->options & OPTION_UPDATE_LOG))
+    if (!(thd->options & OPTION_UPDATE_LOG) &&
+        !(thd->slave_thread && opt_log_slow_slave_statements))
     {
       VOID(pthread_mutex_unlock(&LOCK_log));
       DBUG_RETURN(0);
@@ -1961,7 +1963,7 @@ bool MYSQL_LOG::write(THD *thd,const cha
         localtime_r(&current_time,&tm_tmp);
         start=&tm_tmp;
         /* Note that my_b_write() assumes it knows the length for this */
-        sprintf(buff,"# Time: %02d%02d%02d %2d:%02d:%02d\n",
+        sprintf(buff,"# Time: %02d%02d%02d %2d:%02d:%02d ",
                 start->tm_year % 100,
                 start->tm_mon+1,
                 start->tm_mday,
@@ -1974,22 +1976,67 @@ bool MYSQL_LOG::write(THD *thd,const cha
       if (my_b_printf(&log_file, "# User@Host: %s[%s] @ %s [%s]\n",
                       sctx->priv_user ?
                       sctx->priv_user : "",
-                      sctx->user ? sctx->user : "",
+                      sctx->user ? sctx->user : (thd->slave_thread ? "SQL_SLAVE" : ""),
                       sctx->host ? sctx->host : "",
                       sctx->ip ? sctx->ip : "") ==
           (uint) -1)
         tmp_errno=errno;
     }
-    if (query_start_arg)
-    {
+    if (query_start_timer)
+    {
+      char buf[5][20];
+      ulonglong current_timer= my_timer(&current_timer, frequency);
+      sprintf(buf[0], "%.6f", (current_timer - query_start_timer) / 1000000.0);
+      sprintf(buf[1], "%.6f", (thd->timer_after_lock - query_start_timer) / 1000000.0);
+      if (!query_length)
+      {
+        thd->sent_row_count= thd->examined_row_count= 0;
+        thd->innodb_was_used= FALSE;
+        thd->used_tmp_table= thd->used_tmp_disk_table= FALSE;
+        thd->used_query_cache= thd->used_filesort= FALSE;
+        thd->used_filesort_on_disk= thd->used_full_scan= thd->used_full_join= FALSE;
+        thd->used_filesort_pass= 0;
+      }
+
       /* For slow query log */
       if (my_b_printf(&log_file,
-                      "# Query_time: %lu  Lock_time: %lu  Rows_sent: %lu  Rows_examined: %lu\n",
-                      (ulong) (current_time - query_start_arg),
-                      (ulong) (thd->time_after_lock - query_start_arg),
+                      "# Thread_id: %lu\n" \
+                      "# Query_time: %s  Lock_time: %s  Rows_sent: %lu  Rows_examined: %lu\n" \
+                      "# QC_Hit: %s  Full_scan: %s  Full_join: %s  Tmp_table: %s  Disk_tmp_table: %s\n" \
+                      "# Filesort: %s  Disk_filesort: %s  Merge_passes: %lu\n",
+                      (ulong) thd->thread_id,
+                      buf[0], buf[1],
                       (ulong) thd->sent_row_count,
-                      (ulong) thd->examined_row_count) == (uint) -1)
+                      (ulong) thd->examined_row_count,
+                      (thd->used_query_cache == TRUE ? "Yes" : "No"),
+                      (thd->used_full_scan == TRUE ? "Yes" : "No"),
+                      (thd->used_full_join == TRUE ? "Yes" : "No"),
+                      (thd->used_tmp_table == TRUE ? "Yes" : "No"),
+                      (thd->used_tmp_disk_table == TRUE ? "Yes" : "No"),
+                      (thd->used_filesort == TRUE ? "Yes" : "No"),
+                      (thd->used_filesort_on_disk == TRUE ? "Yes" : "No"),
+                      thd->used_filesort_pass) == (uint) -1)
         tmp_errno=errno;
+      if (thd->innodb_was_used)
+      {
+        sprintf(buf[2], "%.6f", thd->innodb_io_reads_wait_timer / 1000000.0);
+        sprintf(buf[3], "%.6f", thd->innodb_lock_que_wait_timer / 1000000.0);
+        sprintf(buf[4], "%.6f", thd->innodb_innodb_que_wait_timer / 1000000.0);
+        if (my_b_printf(&log_file,
+                        "#   InnoDB_IO_r_ops: %lu  InnoDB_IO_r_bytes: %lu  InnoDB_IO_r_wait: %s\n" \
+                        "#   InnoDB_rec_lock_wait: %s  InnoDB_queue_wait: %s\n" \
+                        "#   InnoDB_pages_distinct: %lu\n",
+                        (ulong) thd->innodb_io_reads,
+                        (ulong) thd->innodb_io_read,
+                        buf[2], buf[3], buf[4],
+                        (ulong) thd->innodb_page_access) == (uint) -1)
+          tmp_errno=errno;
+      } 
+      else
+      {
+        if (my_b_printf(&log_file,"# No InnoDB statistics available for this query\n") == (uint) -1)
+          tmp_errno=errno;
+      }
     }
     if (thd->db && strcmp(thd->db,db))
     {						// Database changed
diff -r ab59095a9cbf sql/log_event.cc
--- a/sql/log_event.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/log_event.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -1900,6 +1900,7 @@ int Query_log_event::exec_event(struct s
       /* Execute the query (note that we bypass dispatch_command()) */
       const char* found_semicolon= NULL;
       mysql_parse(thd, thd->query, thd->query_length, &found_semicolon);
+      log_slow_statement(thd);
 
     }
     else
diff -r ab59095a9cbf sql/my_time.c
--- a/sql/my_time.c	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/my_time.c	Wed Oct 31 12:01:13 2007 +0100
@@ -1250,3 +1250,37 @@ ulonglong TIME_to_ulonglong(const MYSQL_
   return 0;
 }
 
+/*
+ int my_timer(ulonglong *ltime, ulonglong frequency)
+
+ For performance measurement this function returns the number
+ of microseconds since the epoch (SVr4, BSD 4.3, POSIX 1003.1-2001)
+ or system start (Windows platforms).
+
+ For windows platforms frequency value (obtained via 
+ QueryPerformanceFrequency) has to be specified. The global frequency
+ value is set in mysqld.cc. 
+
+ If Windows platform doesn't support QueryPerformanceFrequency we will
+ obtain the time via GetClockCount, which supports microseconds only.
+*/
+
+ulonglong my_timer(ulonglong *ltime, ulonglong frequency)
+{
+  ulonglong newtime= 0;
+#ifdef __WIN__
+  if (frequency) 
+  {
+    QueryPerformanceCounter((LARGE_INTEGER *)&newtime);
+    newtime/= (frequency / 1000000);
+  } else
+    newtime= (GetTickCount() * 1000); /* GetTickCount only returns milliseconds */
+#else
+  struct timeval t;
+  gettimeofday(&t, NULL);
+  newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
+#endif
+  if (ltime)
+    *ltime= newtime;
+  return newtime;
+}
diff -r ab59095a9cbf sql/mysql_priv.h
--- a/sql/mysql_priv.h	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/mysql_priv.h	Wed Oct 31 12:01:13 2007 +0100
@@ -1323,7 +1323,7 @@ extern my_bool opt_enable_named_pipe, op
 extern my_bool opt_enable_named_pipe, opt_sync_frm, opt_allow_suspicious_udfs;
 extern my_bool opt_secure_auth;
 extern char* opt_secure_file_priv;
-extern my_bool opt_log_slow_admin_statements;
+extern my_bool opt_log_slow_admin_statements, opt_log_slow_slave_statements;
 extern my_bool sp_automatic_privileges, opt_noacl;
 extern my_bool opt_old_style_user_limits, trust_function_creators;
 extern uint opt_crash_binlog_innodb;
diff -r ab59095a9cbf sql/mysqld.cc
--- a/sql/mysqld.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/mysqld.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -169,7 +169,6 @@ static void getvolumeID(BYTE *volumeName
 static void getvolumeID(BYTE *volumeName);
 #endif /* __NETWARE__ */
 
-
 #ifdef _AIX41
 int initgroups(const char *,unsigned int);
 #endif
@@ -213,6 +212,7 @@ extern "C" int gethostname(char *name, i
 extern "C" int gethostname(char *name, int namelen);
 #endif
 
+ulonglong frequency= 0;
 
 /* Constants */
 
@@ -384,6 +384,7 @@ my_bool opt_secure_auth= 0;
 my_bool opt_secure_auth= 0;
 char* opt_secure_file_priv= 0;
 my_bool opt_log_slow_admin_statements= 0;
+my_bool opt_log_slow_slave_statements= 0;
 my_bool lower_case_file_system= 0;
 my_bool opt_large_pages= 0;
 uint    opt_large_page_size= 0;
@@ -3490,6 +3491,8 @@ int main(int argc, char **argv)
       unireg_abort(1);
     }
   }
+  if (!QueryPerformanceFrequency((LARGE_INTEGER *)&frequency))
+    frequency= 0;
 #endif /* __WIN__ */
 
   if (init_common_variables(MYSQL_CONFIG_NAME,
@@ -4724,7 +4727,7 @@ enum options_mysqld
   OPT_INTERACTIVE_TIMEOUT, OPT_JOIN_BUFF_SIZE,
   OPT_KEY_BUFFER_SIZE, OPT_KEY_CACHE_BLOCK_SIZE,
   OPT_KEY_CACHE_DIVISION_LIMIT, OPT_KEY_CACHE_AGE_THRESHOLD,
-  OPT_LONG_QUERY_TIME,
+  OPT_LONG_QUERY_TIME, OPT_MIN_EXAMINED_ROW_LIMIT,
   OPT_LOWER_CASE_TABLE_NAMES, OPT_MAX_ALLOWED_PACKET,
   OPT_MAX_BINLOG_CACHE_SIZE, OPT_MAX_BINLOG_SIZE,
   OPT_MAX_CONNECTIONS, OPT_MAX_CONNECT_ERRORS,
@@ -4815,6 +4818,7 @@ enum options_mysqld
   OPT_TIMED_MUTEXES,
   OPT_OLD_STYLE_USER_LIMITS,
   OPT_LOG_SLOW_ADMIN_STATEMENTS,
+  OPT_LOG_SLOW_SLAVE_STATEMENTS,
   OPT_TABLE_LOCK_WAIT_TIMEOUT,
   OPT_PORT_OPEN_TIMEOUT,
   OPT_MERGE,
@@ -5195,6 +5199,11 @@ Disable with --skip-innodb-doublewrite."
    "Log slow OPTIMIZE, ANALYZE, ALTER and other administrative statements to the slow log if it is open.",
    (gptr*) &opt_log_slow_admin_statements,
    (gptr*) &opt_log_slow_admin_statements,
+   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
+  {"log-slow-slave-statements", OPT_LOG_SLOW_SLAVE_STATEMENTS,
+   "Log slow replicated statements to the slow log if it is open.",
+   (gptr*) &opt_log_slow_slave_statements,
+   (gptr*) &opt_log_slow_slave_statements,
    0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
   {"log-slow-queries", OPT_SLOW_QUERY_LOG,
     "Log slow queries to this log file. Defaults logging to hostname-slow.log file. Must be enabled to activate other slow log options.",
@@ -5857,7 +5866,12 @@ log and this option does nothing anymore
    "Log all queries that have taken more than long_query_time seconds to execute to file.",
    (gptr*) &global_system_variables.long_query_time,
    (gptr*) &max_system_variables.long_query_time, 0, GET_ULONG,
-   REQUIRED_ARG, 10, 1, LONG_TIMEOUT, 0, 1, 0},
+    REQUIRED_ARG, 10000000, 0, LONG_TIMEOUT * 1000000, 0, 1, 0},
+   {"min_examined_row_limit", OPT_MIN_EXAMINED_ROW_LIMIT,
+    "Don't log queries which examine less than min_examined_row_limit rows to file.",
+    (gptr*) &global_system_variables.min_examined_row_limit,
+    (gptr*) &max_system_variables.min_examined_row_limit, 0, GET_ULONG,
+    REQUIRED_ARG, 0, 1, ~0L, 0, 1L, 0},
   {"lower_case_table_names", OPT_LOWER_CASE_TABLE_NAMES,
    "If set to 1 table names are stored in lowercase on disk and table names will be case-insensitive.  Should be set to 2 if you are using a case insensitive file system",
    (gptr*) &lower_case_table_names,
@@ -7401,10 +7415,14 @@ static void get_options(int argc,char **
   if (opt_bdb)
     sql_print_warning("this binary does not contain BDB storage engine");
 #endif
-  if ((opt_log_slow_admin_statements || opt_log_queries_not_using_indexes) &&
+  if ((opt_log_slow_admin_statements || opt_log_queries_not_using_indexes ||
+        opt_log_slow_slave_statements) &&
       !opt_slow_log)
-    sql_print_warning("options --log-slow-admin-statements and --log-queries-not-using-indexes have no effect if --log-slow-queries is not set");
-
+  {
+    sql_print_warning("options --log-slow-admin-statements, --log-slow-slave-statements and --log-queries-not-using-indexes have no effect if --log-slow-queries is not set");
+    opt_log_slow_slave_statements= FALSE;
+  }
+  
   if (argc > 0)
   {
     fprintf(stderr, "%s: Too many arguments (first extra is '%s').\nUse --help to get a list of available options\n", my_progname, *argv);
diff -r ab59095a9cbf sql/set_var.cc
--- a/sql/set_var.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/set_var.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -211,7 +211,7 @@ sys_var_bool_ptr
   sys_log_queries_not_using_indexes("log_queries_not_using_indexes",
                                     &opt_log_queries_not_using_indexes);
 sys_var_thd_ulong	sys_log_warnings("log_warnings", &SV::log_warnings);
-sys_var_thd_ulong	sys_long_query_time("long_query_time",
+sys_var_thd_ulonglong  sys_long_query_time("long_query_time",
 					     &SV::long_query_time);
 sys_var_thd_bool	sys_low_priority_updates("low_priority_updates",
 						 &SV::low_priority_updates,
@@ -276,6 +276,8 @@ sys_var_thd_ulong	sys_max_tmp_tables("ma
 					   &SV::max_tmp_tables);
 sys_var_long_ptr	sys_max_write_lock_count("max_write_lock_count",
 						 &max_write_lock_count);
+sys_var_thd_ulong	sys_min_examined_row_limit("min_examined_row_limit",
+					     &SV::min_examined_row_limit);
 sys_var_thd_ulong       sys_multi_range_count("multi_range_count",
                                               &SV::multi_range_count);
 sys_var_long_ptr	sys_myisam_data_pointer_size("myisam_data_pointer_size",
@@ -692,6 +694,7 @@ sys_var *sys_variables[]=
   &sys_max_tmp_tables,
   &sys_max_user_connections,
   &sys_max_write_lock_count,
+  &sys_min_examined_row_limit,
   &sys_multi_range_count,
   &sys_myisam_data_pointer_size,
   &sys_myisam_max_sort_file_size,
@@ -988,6 +991,7 @@ struct show_var_st init_vars[]= {
   {sys_max_tmp_tables.name,	(char*) &sys_max_tmp_tables,	    SHOW_SYS},
   {sys_max_user_connections.name,(char*) &sys_max_user_connections, SHOW_SYS},
   {sys_max_write_lock_count.name, (char*) &sys_max_write_lock_count,SHOW_SYS},
+  {sys_min_examined_row_limit.name, (char*) &sys_min_examined_row_limit, SHOW_SYS},
   {sys_multi_range_count.name,  (char*) &sys_multi_range_count,     SHOW_SYS},
   {sys_myisam_data_pointer_size.name, (char*) &sys_myisam_data_pointer_size, SHOW_SYS},
   {sys_myisam_max_sort_file_size.name, (char*) &sys_myisam_max_sort_file_size,
@@ -1581,6 +1585,53 @@ byte *sys_var_thd_ulong::value_ptr(THD *
   return (byte*) &(thd->variables.*offset);
 }
 
+void sys_var_thd_microtime::set_default(THD *thd, enum_var_type type)
+{
+  pthread_mutex_lock(&LOCK_global_system_variables);
+  global_system_variables.*offset= (ulonglong) option_limits->def_value;
+  pthread_mutex_unlock(&LOCK_global_system_variables);
+}
+
+bool sys_var_thd_microtime::check(THD *thd, set_var *var)
+{
+  if (var->value->result_type() == DECIMAL_RESULT)
+    var->save_result.ulonglong_value= (ulonglong)(var->value->val_real() * 1000000);
+  else 
+    var->save_result.ulonglong_value= (ulonglong)var->value->val_int() * 1000000;
+  return 0;
+}
+
+byte *sys_var_thd_microtime::value_ptr(THD *thd, enum_var_type type,
+				   LEX_STRING *base)
+{
+  if (type == OPT_GLOBAL)
+    return (byte*) &(global_system_variables.*offset);
+  return (byte*) &(thd->variables.*offset);
+}
+
+bool sys_var_thd_microtime::update(THD *thd,  set_var *var)
+{
+  ulonglong tmp= var->save_result.ulonglong_value;
+
+  if (tmp > max_system_variables.*offset)
+    tmp= max_system_variables.*offset;
+
+  if (option_limits)
+    tmp= getopt_ull_limit_value(tmp, option_limits);
+  
+  /* Lock is needed to make things safe on 32 bit systems */
+  if (var->type == OPT_GLOBAL)
+  {
+    /* Lock is needed to make things safe on 32 bit systems */
+    pthread_mutex_lock(&LOCK_global_system_variables);
+    global_system_variables.*offset= tmp;
+    pthread_mutex_unlock(&LOCK_global_system_variables);
+  }
+  else
+    thd->variables.*offset= (ulonglong) tmp;
+
+  return 0;
+}
 
 bool sys_var_thd_ha_rows::update(THD *thd, set_var *var)
 {
diff -r ab59095a9cbf sql/set_var.h
--- a/sql/set_var.h	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/set_var.h	Wed Oct 31 12:01:13 2007 +0100
@@ -123,6 +123,7 @@ public:
 };
 
 
+
 class sys_var_ulonglong_ptr :public sys_var
 {
 public:
@@ -287,7 +288,6 @@ public:
   }
 };
 
-
 class sys_var_thd_ulong :public sys_var_thd
 {
   sys_check_func check_func;
@@ -307,6 +307,23 @@ public:
   byte *value_ptr(THD *thd, enum_var_type type, LEX_STRING *base);
 };
 
+class sys_var_thd_microtime :public sys_var_thd
+{
+public:
+  ulonglong SV::*offset;
+  sys_var_thd_microtime(const char *name_arg, ulonglong SV::*offset_arg)
+    :sys_var_thd(name_arg), offset(offset_arg)
+  {}
+  bool update(THD *thd, set_var *var);
+  void set_default(THD *thd, enum_var_type type);
+  SHOW_TYPE type() { return SHOW_MICROTIME; }
+  byte *value_ptr(THD *thd, enum_var_type type, LEX_STRING *base);
+  bool check(THD *thd, set_var *var);
+  bool check_update_type(Item_result type)
+  { 
+    return type != INT_RESULT && type != DECIMAL_RESULT;
+  }
+};
 
 class sys_var_thd_ha_rows :public sys_var_thd
 {
@@ -324,7 +341,6 @@ public:
   SHOW_TYPE show_type() { return SHOW_HA_ROWS; }
   byte *value_ptr(THD *thd, enum_var_type type, LEX_STRING *base);
 };
-
 
 class sys_var_thd_ulonglong :public sys_var_thd
 {
@@ -352,7 +368,6 @@ public:
     return (only_global && type != OPT_GLOBAL);
   }
 };
-
 
 class sys_var_thd_bool :public sys_var_thd
 {
diff -r ab59095a9cbf sql/slave.cc
--- a/sql/slave.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/slave.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -2907,6 +2907,8 @@ static int init_slave_thread(THD* thd, S
     + MAX_LOG_EVENT_HEADER;  /* note, incr over the global not session var */
   thd->slave_thread = 1;
   set_slave_thread_options(thd);
+  if (opt_log_slow_slave_statements)
+    thd->enable_slow_log= TRUE;
   thd->client_capabilities = CLIENT_LOCAL_FILES;
   thd->real_id=pthread_self();
   pthread_mutex_lock(&LOCK_thread_count);
diff -r ab59095a9cbf sql/sql_cache.cc
--- a/sql/sql_cache.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/sql_cache.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -1297,6 +1297,7 @@ def_week_frmt: %lu",
 
   thd->limit_found_rows = query->found_rows();
   thd->status_var.last_query_cost= 0.0;
+  thd->used_query_cache= TRUE;
 
   BLOCK_UNLOCK_RD(query_block);
   DBUG_RETURN(1);				// Result sent to client
diff -r ab59095a9cbf sql/sql_class.cc
--- a/sql/sql_class.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/sql_class.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -173,7 +173,7 @@ THD::THD()
    Open_tables_state(refresh_version),
    lock_id(&main_lock_id),
    user_time(0), in_sub_stmt(0), global_read_lock(0), is_fatal_error(0),
-   rand_used(0), time_zone_used(0),
+   rand_used(0), time_zone_used(0), user_timer(0),
    last_insert_id_used(0), last_insert_id_used_bin_log(0), insert_id_used(0),
    clear_next_insert_id(0), in_lock_tables(0), bootstrap(0),
    derived_tables_processing(FALSE), spcont(NULL), m_lip(NULL)
@@ -2101,6 +2101,12 @@ void THD::reset_sub_statement_state(Sub_
   backup->cuted_fields=     cuted_fields;
   backup->client_capabilities= client_capabilities;
   backup->savepoints= transaction.savepoints;
+  backup->innodb_io_reads= innodb_io_reads;
+  backup->innodb_io_read= innodb_io_read;
+  backup->innodb_io_reads_wait_timer= innodb_io_reads_wait_timer;
+  backup->innodb_lock_que_wait_timer= innodb_lock_que_wait_timer;
+  backup->innodb_innodb_que_wait_timer= innodb_innodb_que_wait_timer;
+  backup->innodb_page_access= innodb_page_access;
 
   if (!lex->requires_prelocking() || is_update_query(lex->sql_command))
     options&= ~OPTION_BIN_LOG;
@@ -2117,7 +2123,13 @@ void THD::reset_sub_statement_state(Sub_
   sent_row_count= 0;
   cuted_fields= 0;
   transaction.savepoints= 0;
-
+  innodb_io_reads= 0;
+  innodb_io_read= 0;
+  innodb_io_reads_wait_timer= 0;
+  innodb_lock_que_wait_timer= 0;
+  innodb_innodb_que_wait_timer= 0;
+  innodb_page_access= 0;
+  
   /* Surpress OK packets in case if we will execute statements */
   net.no_send_ok= TRUE;
 }
@@ -2163,6 +2175,12 @@ void THD::restore_sub_statement_state(Su
   */
   examined_row_count+= backup->examined_row_count;
   cuted_fields+=       backup->cuted_fields;
+  innodb_io_reads+= backup->innodb_io_reads;
+  innodb_io_read+= backup->innodb_io_read;
+  innodb_io_reads_wait_timer+= backup->innodb_io_reads_wait_timer;
+  innodb_lock_que_wait_timer+= backup->innodb_lock_que_wait_timer;
+  innodb_innodb_que_wait_timer+= backup->innodb_innodb_que_wait_timer;
+  innodb_page_access+= backup->innodb_page_access;
 }
 
 
diff -r ab59095a9cbf sql/sql_class.h
--- a/sql/sql_class.h	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/sql_class.h	Wed Oct 31 12:01:13 2007 +0100
@@ -43,6 +43,13 @@ extern char internal_table_name[2];
 extern char internal_table_name[2];
 extern char empty_c_string[1];
 extern const char **errmesg;
+#ifdef __cplusplus
+__BEGIN_DECLS
+#endif
+extern ulonglong frequency;
+#ifdef __cplusplus
+__END_DECLS
+#endif
 
 #define TC_LOG_PAGE_SIZE   8192
 #define TC_LOG_MIN_SIZE    (3*TC_LOG_PAGE_SIZE)
@@ -308,7 +315,7 @@ public:
   bool write(THD *thd, enum enum_server_command command,
 	     const char *format, ...) ATTRIBUTE_FORMAT(printf, 4, 5);
   bool write(THD *thd, const char *query, uint query_length,
-	     time_t query_start=0);
+	     time_t query_start=0, ulonglong query_start_timer=0);
   bool write(Log_event* event_info); // binary log write
   bool write(THD *thd, IO_CACHE *cache, Log_event *commit_event);
 
@@ -514,13 +521,14 @@ struct system_variables
   ulong auto_increment_increment, auto_increment_offset;
   ulong bulk_insert_buff_size;
   ulong join_buff_size;
-  ulong long_query_time;
+  ulonglong long_query_time;
   ulong max_allowed_packet;
   ulong max_error_count;
   ulong max_length_for_sort_data;
   ulong max_sort_length;
   ulong max_tmp_tables;
   ulong max_insert_delayed_threads;
+  ulong min_examined_row_limit;
   ulong multi_range_count;
   ulong myisam_repair_threads;
   ulong myisam_sort_buff_size;
@@ -1086,6 +1094,12 @@ public:
   uint in_sub_stmt;
   bool enable_slow_log, insert_id_used, clear_next_insert_id;
   bool last_insert_id_used;
+  ulong      innodb_io_reads;
+  ulonglong  innodb_io_read;
+  ulong      innodb_io_reads_wait_timer;
+  ulong      innodb_lock_que_wait_timer;
+  ulong      innodb_innodb_que_wait_timer;
+  ulong      innodb_page_access;
   my_bool no_send_ok;
   SAVEPOINT *savepoints;
 };
@@ -1142,6 +1156,11 @@ class THD :public Statement,
 class THD :public Statement,
            public Open_tables_state
 {
+private:
+  inline ulonglong query_start_timer() { return start_timer; }
+  inline void set_timer()    { if (user_timer) start_timer=timer_after_lock=user_timer; else timer_after_lock=my_timer(&start_timer, frequency); }
+  inline void end_timer()    { my_timer(&start_timer, frequency); }
+  inline void lock_timer()   { my_timer(&timer_after_lock, frequency); }
 public:
   /*
     Constant for THD::where initialization in the beginning of every query.
@@ -1250,10 +1269,28 @@ public:
   */
   const char *where;
   time_t     start_time,time_after_lock,user_time;
+  ulonglong start_timer,timer_after_lock, user_timer;
   time_t     connect_time,thr_create_time; // track down slow pthread_create
   thr_lock_type update_lock_default;
   Delayed_insert *di;
 
+  bool       innodb_was_used;
+  ulong      innodb_io_reads;
+  ulonglong  innodb_io_read;
+  ulong      innodb_io_reads_wait_timer;
+  ulong      innodb_lock_que_wait_timer;
+  ulong      innodb_innodb_que_wait_timer;
+  ulong      innodb_page_access;
+
+  bool       used_tmp_table;
+  bool       used_tmp_disk_table;
+  bool       used_query_cache;
+  bool       used_full_join;
+  bool       used_full_scan;
+  ulong      used_filesort_pass;
+  bool       used_filesort;
+  bool       used_filesort_on_disk;
+  
   /* <> 0 if we are inside of trigger or stored function. */
   uint in_sub_stmt;
 
@@ -1589,11 +1626,11 @@ public:
     proc_info = old_msg;
     pthread_mutex_unlock(&mysys_var->mutex);
   }
-  inline time_t query_start() { query_start_used=1; return start_time; }
-  inline void	set_time()    { if (user_time) start_time=time_after_lock=user_time; else time_after_lock=time(&start_time); }
-  inline void	end_time()    { time(&start_time); }
-  inline void	set_time(time_t t) { time_after_lock=start_time=user_time=t; }
-  inline void	lock_time()   { time(&time_after_lock); }
+  inline time_t query_start() { query_start_timer(); query_start_used=1; return start_time; }
+  inline void set_time()    { set_timer(); if (user_time) start_time=time_after_lock=user_time; else time_after_lock=time(&start_time); }
+  inline void	end_time()    { end_timer(); time(&start_time); }
+  inline void	set_time(time_t t) { set_timer(); time_after_lock=start_time=user_time=t; }
+  inline void	lock_time()   { lock_timer(); time(&time_after_lock); }
   inline void	insert_id(ulonglong id_arg)
   {
     last_insert_id= id_arg;
diff -r ab59095a9cbf sql/sql_parse.cc
--- a/sql/sql_parse.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/sql_parse.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -20,6 +20,7 @@
 #include <m_ctype.h>
 #include <myisam.h>
 #include <my_dir.h>
+#include <my_time.h>
 
 #ifdef HAVE_INNOBASE_DB
 #include "ha_innodb.h"
@@ -2193,27 +2194,31 @@ void log_slow_statement(THD *thd)
     return;                                     // Don't set time for sub stmt
 
   start_of_query= thd->start_time;
+  ulonglong start_of_query_timer= thd->start_timer;
   thd->end_time();				// Set start time
 
   /*
     Do not log administrative statements unless the appropriate option is
     set; do not log into slow log if reading from backup.
   */
-  if (thd->enable_slow_log && !thd->user_time)
+  if (thd->enable_slow_log &&
+      (!thd->user_time || (thd->slave_thread && opt_log_slow_slave_statements))
+     )
   {
     thd_proc_info(thd, "logging slow query");
 
-    if ((ulong) (thd->start_time - thd->time_after_lock) >
-	thd->variables.long_query_time ||
-        (thd->server_status &
-	  (SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED)) &&
-        (specialflag & SPECIAL_LOG_QUERIES_NOT_USING_INDEXES) &&
-        /* == SQLCOM_END unless this is a SHOW command */
-        thd->lex->orig_sql_command == SQLCOM_END)
+    if (((ulong) (thd->start_timer - thd->timer_after_lock) >=
+         thd->variables.long_query_time ||
+         (thd->server_status &
+              (SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED)) &&
+         (specialflag & SPECIAL_LOG_QUERIES_NOT_USING_INDEXES) &&
+         /* == SQLCOM_END unless this is a SHOW command */
+         thd->lex->orig_sql_command == SQLCOM_END) &&
+        thd->examined_row_count >= thd->variables.min_examined_row_limit)
     {
       thd_proc_info(thd, "logging slow query");
       thd->status_var.long_query_count++;
-      mysql_slow_log.write(thd, thd->query, thd->query_length, start_of_query);
+      mysql_slow_log.write(thd, thd->query, thd->query_length, start_of_query, start_of_query_timer);
     }
   }
 }
@@ -5867,6 +5872,22 @@ void mysql_reset_thd_for_next_command(TH
     thd->total_warn_count=0;			// Warnings for this query
     thd->rand_used= 0;
     thd->sent_row_count= thd->examined_row_count= 0;
+    thd->innodb_was_used= FALSE;
+    thd->innodb_io_reads= 0;
+    thd->innodb_io_read= 0;
+    thd->innodb_io_reads_wait_timer= 0;
+    thd->innodb_lock_que_wait_timer= 0;
+    thd->innodb_innodb_que_wait_timer= 0;
+    thd->innodb_page_access= 0;
+    thd->used_tmp_table= FALSE;
+    thd->used_tmp_disk_table= FALSE;
+    thd->used_query_cache= FALSE;
+    thd->used_full_join= FALSE;
+    thd->used_full_scan= FALSE;
+    thd->used_filesort_on_disk= FALSE;
+    thd->used_filesort= FALSE;
+    thd->used_filesort_pass= 0;
+    
 #ifdef ENABLED_PROFILING
     thd->profiling.reset();
 #endif
diff -r ab59095a9cbf sql/sql_select.cc
--- a/sql/sql_select.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/sql_select.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -6170,8 +6170,11 @@ make_join_readinfo(JOIN *join, ulonglong
 	  {
 	    join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 	    if (statistics)
+	    {
 	      statistic_increment(join->thd->status_var.select_scan_count,
 				  &LOCK_status);
+				join->thd->used_full_scan= TRUE;
+      }
 	  }
 	}
 	else
@@ -6186,8 +6189,11 @@ make_join_readinfo(JOIN *join, ulonglong
 	  {
 	    join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 	    if (statistics)
+	    {
 	      statistic_increment(join->thd->status_var.select_full_join_count,
 				  &LOCK_status);
+				join->thd->used_full_join= TRUE;
+      }
 	  }
 	}
 	if (!table->no_keyread)
@@ -9201,6 +9207,7 @@ create_tmp_table(THD *thd,TMP_TABLE_PARA
 		      (ulong) rows_limit,test(group)));
 
   statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
+  thd->used_tmp_table= TRUE;
 
   if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
     temp_pool_slot = bitmap_set_next(&temp_pool);
@@ -10064,6 +10071,7 @@ static bool create_myisam_tmp_table(TABL
   }
   statistic_increment(table->in_use->status_var.created_tmp_disk_tables,
 		      &LOCK_status);
+	table->in_use->used_tmp_disk_table= TRUE;
   table->s->db_record_offset= 1;
   DBUG_RETURN(0);
  err:
diff -r ab59095a9cbf sql/sql_show.cc
--- a/sql/sql_show.cc	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/sql_show.cc	Wed Oct 31 12:01:13 2007 +0100
@@ -1467,6 +1467,9 @@ static bool show_status_array(THD *thd, 
           value= ((char *) status_var + (ulonglong) value);
         case SHOW_LONGLONG:
           end= longlong10_to_str(*(longlong*) value, buff, 10);
+          break;
+        case SHOW_MICROTIME:
+          end= buff + sprintf(buff, "%.6f", (*(ulonglong*)value) / 1000000.0);
           break;
         case SHOW_HA_ROWS:
           end= longlong10_to_str((longlong) *(ha_rows*) value, buff, 10);
diff -r ab59095a9cbf sql/structs.h
--- a/sql/structs.h	Tue Aug 14 12:28:04 2007 +0200
+++ b/sql/structs.h	Wed Oct 31 12:01:13 2007 +0100
@@ -168,8 +168,8 @@ enum SHOW_TYPE
 enum SHOW_TYPE
 {
   SHOW_UNDEF,
-  SHOW_LONG, SHOW_LONGLONG, SHOW_INT, SHOW_CHAR, SHOW_CHAR_PTR, 
-  SHOW_DOUBLE_STATUS,
+  SHOW_LONG, SHOW_LONGLONG, SHOW_MICROTIME, SHOW_INT, SHOW_CHAR, 
+  SHOW_CHAR_PTR, SHOW_DOUBLE_STATUS, 
   SHOW_BOOL, SHOW_MY_BOOL, SHOW_OPENTABLES, SHOW_STARTTIME, SHOW_QUESTION,
   SHOW_LONG_CONST, SHOW_INT_CONST, SHOW_HAVE, SHOW_SYS, SHOW_HA_ROWS,
   SHOW_VARS,
