Bug #31974 Wrong EXPLAIN output
Submitted: 31 Oct 2007 7:06 Modified: 12 Nov 2007 19:24
Reporter: Alexander Nozdrin Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:5.1-bk OS:Any
Assigned to: Georgi Kodinov CPU Architecture:Any
Tags: regression

[31 Oct 2007 7:06] Alexander Nozdrin
Description:
Regression introduced by the patch for BUG#31035.

In some cases EXPLAIN does not show used indexes.

How to repeat:
* Test case 1

> CREATE TABLE t1 (a INT, b INT, INDEX (a,b));

> EXPLAIN SELECT 1 FROM t1 AS t1_outer WHERE 
  (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2) > 12;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
2	SUBQUERY	t1	range	NULL	a	5	NULL	8	

The last row should be:
2	SUBQUERY	t1	range	NULL	a	5	NULL	8	Using index for group-by

* Test case 2

> CREATE TABLE t1 (a INT, b INT, INDEX (a,b));

> EXPLAIN SELECT 1 FROM t1 AS t1_outer1 JOIN t1 AS t1_outer2 
  ON t1_outer1.a = (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2) 
  AND t1_outer1.b = t1_outer2.b;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1_outer1	ref	a	a	5	const	1	Using where; Using index
1	PRIMARY	t1_outer2	index	NULL	a	10	NULL	15	Using where; Using index; Using join buffer
2	SUBQUERY	t1	range	NULL	a	5	NULL	8	

The last row should be:
2	SUBQUERY	t1	range	NULL	a	5	NULL	8	Using index for group-by

* Test case 3

> create table t0 (key1 int not null, INDEX i1(key1));

> create table t1 like t0;

> explain select * from (select * from t1 where key1 = 3 or key2 =3) as Z
  where key8 >5;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	1	
2	DERIVED	t1	index_merge	i1,i2	i1,i2	4,4	NULL	2	Using union(i1,i2); Using where

The last row should be:
2	DERIVED	t1	index_merge	i1,i2	i1,i2	4,4	NULL	2	Using union(i1,i2); Using where; Using index

Suggested fix:
select_describe() contains the following code:

...
if (key_read)
{
  if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
    extra.append(STRING_WITH_LEN("; Using index for group-by"));
  else
    extra.append(STRING_WITH_LEN("; Using index"));
}
...

The problem is that key_read is set to 0 at this point.
[31 Oct 2007 8:39] Timour Katchaounov
Changed to P2 as this is regression.
[31 Oct 2007 8:52] Alexander Nozdrin
Wrong bug number in the original description.
The regression caused by BUG#31148: bool close_thread_table(THD*, TABLE**):
Assertion `table->key_read == 0' failed.
[5 Nov 2007 11:20] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/37078

ChangeSet@1.2607, 2007-11-05 13:19:56+02:00, gkodinov@magare.gmz +3 -0
  Bug #31974: Wrong EXPLAIN output
  
  The fix for bug 31148 is not correct. It does not
  have a relation to the problem described in this bug.
  And removing the fix will not make the bug to re-appear.
  Fixed the bug #31974 by removing the fix for bug 31148
  and adding a test case.
[6 Nov 2007 13:30] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/37180

ChangeSet@1.2608, 2007-11-06 15:29:55+02:00, gkodinov@magare.gmz +2 -0
  Bug #31974: additional commit (test case updates)
   loose index scan enabled for subqueries
[7 Nov 2007 21:59] Bugs System
Pushed into 6.0.4-alpha
[7 Nov 2007 22:00] Bugs System
Pushed into 5.1.23-rc
[12 Nov 2007 19:24] Paul DuBois
No changelog entry is needed, per Joro.