Commit 5b8cdbf4 authored by unknown's avatar unknown

Fixed bug#36006: Optimizer does table scan for SELECT COUNT(*)

                 for ENGINE=MRG_MYISAM (should be optimized out).

Before WL#3281 MERGE engine had the HA_NOT_EXACT_COUNT flag
unset, and it worked with COUNT optimization as desired.
After the removal of the HA_NOT_EXACT_COUNT flag neither
HA_STATS_RECORDS_IS_EXACT (opposite to former HA_NOT_EXACT_COUNT
flag) nor modern HA_HAS_RECORDS flag were not added to MERGE
table flag mask.

1. The HA_HAS_RECORDS table flag has been set.
2. The ha_myisammrg::records method has been overridden to
   calculate total number of records in underlying tables.



storage/myisammrg/myrg_records.c:
  Fixed bug#36006: Optimizer does table scan for select count(*).
  The myrg_records function has been added to calculate total number
  of records in underlying tables.
include/myisammrg.h:
  Fixed bug#36006: Optimizer does table scan for select count(*).
  The myrg_records function declaration has been added.
mysql-test/r/merge.result:
  Added test case for bug#36006.
mysql-test/t/merge.test:
  Added test case for bug#36006.
storage/myisammrg/CMakeLists.txt:
  Fixed bug#36006: Optimizer does table scan for select count(*).
  New myrg_records.c file has been added.
storage/myisammrg/Makefile.am:
  Fixed bug#36006: Optimizer does table scan for select count(*).
  New myrg_records.c file has been added.
storage/myisammrg/ha_myisammrg.cc:
  Fixed bug#36006: Optimizer does table scan for select count(*).
  The ha_myisammrg::records method has been overridden.
storage/myisammrg/ha_myisammrg.h:
  Fixed bug#36006: Optimizer does table scan for select count(*).
  1. The HA_HAS_RECORDS table flag has been set.
  2. The ha_myisammrg::records method has been overridden.
parent 304c4381
......@@ -112,6 +112,7 @@ extern int myrg_reset(MYRG_INFO *info);
extern void myrg_extrafunc(MYRG_INFO *info,invalidator_by_filename inv);
extern ha_rows myrg_records_in_range(MYRG_INFO *info, int inx,
key_range *min_key, key_range *max_key);
extern ha_rows myrg_records(MYRG_INFO *info);
extern ulonglong myrg_position(MYRG_INFO *info);
#ifdef __cplusplus
......
......@@ -2006,3 +2006,19 @@ test.t1 optimize status OK
FLUSH TABLES m1, t1;
UNLOCK TABLES;
DROP TABLE t1, m1;
CREATE TABLE t1(C1 INT, C2 INT, KEY C1(C1), KEY C2(C2)) ENGINE=MYISAM;
CREATE TABLE t2(C1 INT, C2 INT, KEY C1(C1), KEY C2(C2)) ENGINE=MYISAM;
CREATE TABLE t3(C1 INT, C2 INT, KEY C1(C1), KEY C2(C2)) ENGINE=MYISAM;
CREATE TABLE t4(C1 INT, C2 INT, KEY C1(C1), KEY C2(C2))
ENGINE=MRG_MYISAM UNION=(t1, t2, t3);
INSERT INTO t1 VALUES (1,1), (1,2),(1,3), (1,4);
INSERT INTO t2 VALUES (2,1), (2,2),(2,3), (2,4);
INSERT INTO t3 VALUES (3,1), (3,2),(3,3), (3,4);
EXPLAIN SELECT COUNT(*) FROM t1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
EXPLAIN SELECT COUNT(*) FROM t4;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
DROP TABLE t1, t2, t3, t4;
End of 5.1 tests
......@@ -1394,3 +1394,19 @@ FLUSH TABLES m1, t1;
UNLOCK TABLES;
DROP TABLE t1, m1;
#
# Bug#36006 - Optimizer does table scan for select count(*)
#
CREATE TABLE t1(C1 INT, C2 INT, KEY C1(C1), KEY C2(C2)) ENGINE=MYISAM;
CREATE TABLE t2(C1 INT, C2 INT, KEY C1(C1), KEY C2(C2)) ENGINE=MYISAM;
CREATE TABLE t3(C1 INT, C2 INT, KEY C1(C1), KEY C2(C2)) ENGINE=MYISAM;
CREATE TABLE t4(C1 INT, C2 INT, KEY C1(C1), KEY C2(C2))
ENGINE=MRG_MYISAM UNION=(t1, t2, t3);
INSERT INTO t1 VALUES (1,1), (1,2),(1,3), (1,4);
INSERT INTO t2 VALUES (2,1), (2,2),(2,3), (2,4);
INSERT INTO t3 VALUES (3,1), (3,2),(3,3), (3,4);
EXPLAIN SELECT COUNT(*) FROM t1;
EXPLAIN SELECT COUNT(*) FROM t4;
DROP TABLE t1, t2, t3, t4;
--echo End of 5.1 tests
......@@ -26,7 +26,7 @@ SET(MYISAMMRG_SOURCES myrg_close.c myrg_create.c myrg_delete.c myrg_extra.c myr
myrg_locking.c myrg_open.c myrg_panic.c myrg_queue.c myrg_range.c
myrg_rfirst.c myrg_rkey.c myrg_rlast.c myrg_rnext.c myrg_rnext_same.c
myrg_rprev.c myrg_rrnd.c myrg_rsame.c myrg_static.c myrg_update.c
myrg_write.c)
myrg_write.c myrg_records.c)
IF(NOT SOURCE_SUBLIBS)
ADD_LIBRARY(myisammrg ${MYISAMMRG_SOURCES})
......
......@@ -35,7 +35,7 @@ libmyisammrg_a_SOURCES = myrg_open.c myrg_extra.c myrg_info.c myrg_locking.c \
myrg_rkey.c myrg_rfirst.c myrg_rlast.c myrg_rnext.c \
myrg_rprev.c myrg_queue.c myrg_write.c myrg_range.c \
ha_myisammrg.cc \
myrg_rnext_same.c
myrg_rnext_same.c myrg_records.c
EXTRA_DIST = CMakeLists.txt plug.in
......
......@@ -1154,6 +1154,12 @@ int ha_myisammrg::check(THD* thd, HA_CHECK_OPT* check_opt)
}
ha_rows ha_myisammrg::records()
{
return myrg_records(file);
}
extern int myrg_panic(enum ha_panic_function flag);
int myisammrg_panic(handlerton *hton, ha_panic_function flag)
{
......
......@@ -42,6 +42,7 @@ class ha_myisammrg: public handler
HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE |
HA_NULL_IN_KEY | HA_CAN_INDEX_BLOBS | HA_FILE_BASED |
HA_ANY_INDEX_MAY_BE_UNIQUE | HA_CAN_BIT_FIELD |
HA_HAS_RECORDS |
HA_NO_COPY_ON_ALTER);
}
ulong index_flags(uint inx, uint part, bool all_parts) const
......@@ -94,4 +95,5 @@ class ha_myisammrg: public handler
TABLE *table_ptr() { return table; }
bool check_if_incompatible_data(HA_CREATE_INFO *info, uint table_changes);
int check(THD* thd, HA_CHECK_OPT* check_opt);
ha_rows records();
};
/* Copyright (C) 2008 MySQL AB
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
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include "myrg_def.h"
ha_rows myrg_records(MYRG_INFO *info)
{
ha_rows records=0;
MYRG_TABLE *file;
DBUG_ENTER("myrg_records");
for (file=info->open_tables ; file != info->end_table ; file++)
records+= file->table->s->state.state.records;
DBUG_RETURN(records);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment