List:Commits« Previous MessageNext Message »
From:kgeorge Date:July 16 2007 2:11pm
Subject:bk commit into 5.0 tree (gkodinov:1.2530) BUG#28591
View as plain text  
Below is the list of changes that have just been committed into a local
5.0 repository of kgeorge. When kgeorge does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet@stripped, 2007-07-16 17:11:32+03:00, gkodinov@stripped +4 -0
  Bug #28591: MySQL need not sort the records in case of 
  ORDER BY primary_key on InnoDB table
  
  Queries that use an InnoDB secondary index to retrieve
  data don't need to sort in case of ORDER BY primary key.
  This is because InnoDB returns the rows in order of the
  primary key.
  Fixed by preventing temp table sort for queries that use
  secondary index to access the qualifying table data and 
  are ordered on the primary key.

  mysql-test/r/innodb_mysql.result@stripped, 2007-07-16 17:11:30+03:00, gkodinov@stripped +16 -0
    Bug #28591: test case

  mysql-test/t/innodb_mysql.test@stripped, 2007-07-16 17:11:30+03:00, gkodinov@stripped +12 -0
    Bug #28591: test case

  sql/sql_select.cc@stripped, 2007-07-16 17:11:30+03:00, gkodinov@stripped +9 -3
    Bug #28591: try primary key if it's contained in the 
    key used.

  sql/table.cc@stripped, 2007-07-16 17:11:30+03:00, gkodinov@stripped +4 -0
    Bug #28591: can use the primary key
    to sort as well.

diff -Nrup a/mysql-test/r/innodb_mysql.result b/mysql-test/r/innodb_mysql.result
--- a/mysql-test/r/innodb_mysql.result	2007-07-07 22:17:02 +03:00
+++ b/mysql-test/r/innodb_mysql.result	2007-07-16 17:11:30 +03:00
@@ -735,4 +735,20 @@ COUNT(*)
 3072
 set @@sort_buffer_size=default;
 DROP TABLE t1,t2;
+CREATE TABLE t1 (a int, b int, PRIMARY KEY (a), KEY bkey (b)) ENGINE=InnoDB;
+INSERT INTO t1 VALUES (1,1),(3,1),(2,1);
+EXPLAIN SELECT 1 FROM t1 WHERE b=10 ORDER BY a;
+id	1
+select_type	SIMPLE
+table	t1
+type	ref
+possible_keys	bkey
+key	bkey
+key_len	5
+ref	const
+rows	1
+Extra	Using where; Using index
+SELECT * FROM t1 WHERE b=10 ORDER BY a;
+a	b
+DROP TABLE t1;
 End of 5.0 tests
diff -Nrup a/mysql-test/t/innodb_mysql.test b/mysql-test/t/innodb_mysql.test
--- a/mysql-test/t/innodb_mysql.test	2007-07-07 22:19:29 +03:00
+++ b/mysql-test/t/innodb_mysql.test	2007-07-16 17:11:30 +03:00
@@ -741,4 +741,16 @@ set @@sort_buffer_size=default;
 
 DROP TABLE t1,t2;
 
+#
+# Bug #28591: MySQL need not sort the records in case of ORDER BY
+# primary_key on InnoDB table
+#
+
+CREATE TABLE t1 (a int, b int, PRIMARY KEY (a), KEY bkey (b)) ENGINE=InnoDB;
+INSERT INTO t1 VALUES (1,1),(3,1),(2,1);
+query_vertical EXPLAIN SELECT 1 FROM t1 WHERE b=10 ORDER BY a;
+SELECT * FROM t1 WHERE b=10 ORDER BY a;
+
+DROP TABLE t1;
+
 --echo End of 5.0 tests
diff -Nrup a/sql/sql_select.cc b/sql/sql_select.cc
--- a/sql/sql_select.cc	2007-07-12 04:45:22 +03:00
+++ b/sql/sql_select.cc	2007-07-16 17:11:30 +03:00
@@ -12401,9 +12401,15 @@ test_if_skip_sort_order(JOIN_TAB *tab,OR
       }
     }
     /* Check if we get the rows in requested sorted order by using the key */
-    if (usable_keys.is_set(ref_key) &&
-	(order_direction = test_if_order_by_key(order,table,ref_key,
-						&used_key_parts)))
+    if ((usable_keys.is_set(ref_key) &&
+	(order_direction= test_if_order_by_key(order,table,ref_key,
+						&used_key_parts))) ||
+        ((table->file->table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
+         table->s->primary_key != MAX_KEY &&
+         usable_keys.is_set(table->s->primary_key) &&
+         (order_direction= test_if_order_by_key(order,table,table->s->primary_key,
+						&used_key_parts)) &&
+         (int) (ref_key= table->s->primary_key) >= 0))
     {
       if (order_direction == -1)		// If ORDER BY ... DESC
       {
diff -Nrup a/sql/table.cc b/sql/table.cc
--- a/sql/table.cc	2007-06-27 14:35:48 +03:00
+++ b/sql/table.cc	2007-07-16 17:11:30 +03:00
@@ -780,7 +780,11 @@ int openfrm(THD *thd, const char *name, 
 	      the primary key, then we can use any key to find this column
 	    */
 	    if (ha_option & HA_PRIMARY_KEY_IN_READ_INDEX)
+            {
 	      field->part_of_key= share->keys_in_use;
+              if (field->part_of_sortkey.is_set(key))
+                field->part_of_sortkey= share->keys_in_use;
+            }
 	  }
 	  if (field->key_length() != key_part->length)
 	  {
Thread
bk commit into 5.0 tree (gkodinov:1.2530) BUG#28591kgeorge16 Jul