Bug #72893 PRIMARY KEY not used on partitioned table
Submitted: 6 Jun 2014 0:51 Modified: 6 Jun 2014 18:02
Reporter: Justin Swanhart Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:5.6.17 OS:Any
Assigned to: CPU Architecture:Any

[6 Jun 2014 0:51] Justin Swanhart
Description:
I have a test case where the database should use the PRIMARY KEY to find a 
row in a partitioned table but entire partitions are scanned instead.

How to repeat:
mysql> show create table t1\G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `week_of_year` tinyint(2) unsigned NOT NULL,
  `type` tinyint(2) unsigned NOT NULL,
  `external_userid` bigint(20) unsigned NOT NULL,
  `status_id` varchar(20) NOT NULL,
  `text` varchar(255) NOT NULL DEFAULT '',
  `link` varchar(255) NOT NULL,
  PRIMARY KEY (`status_id`,`external_userid`,`type`,`week_of_year`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!50100 PARTITION BY LIST (MOD(week_of_year,10))
(PARTITION week0 VALUES IN (0) ENGINE = InnoDB,
 PARTITION week1 VALUES IN (1) ENGINE = InnoDB,
 PARTITION week2 VALUES IN (2) ENGINE = InnoDB,
 PARTITION week3 VALUES IN (3) ENGINE = InnoDB,
 PARTITION week4 VALUES IN (4) ENGINE = InnoDB,
 PARTITION week5 VALUES IN (5) ENGINE = InnoDB,
 PARTITION week6 VALUES IN (6) ENGINE = InnoDB,
 PARTITION week7 VALUES IN (7) ENGINE = InnoDB,
 PARTITION week8 VALUES IN (8) ENGINE = InnoDB,
 PARTITION week9 VALUES IN (9) ENGINE = InnoDB) */
1 row in set (0.01 sec)

mysql> show create table t2\G
ERROR 1146 (42S02): Table 'test.t2' doesn't exist
mysql> show create table t1_hash\G
*************************** 1. row ***************************
       Table: t1_hash
Create Table: CREATE TABLE `t1_hash` (
  `week_of_year` tinyint(2) unsigned NOT NULL,
  `type` tinyint(2) unsigned NOT NULL,
  `external_userid` bigint(20) unsigned NOT NULL,
  `status_id` varchar(20) NOT NULL,
  `text` varchar(255) NOT NULL DEFAULT '',
  `link` varchar(255) NOT NULL,
  PRIMARY KEY (`status_id`,`external_userid`,`type`,`week_of_year`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!50100 PARTITION BY HASH (week_of_year)
PARTITIONS 10 */
1 row in set (0.00 sec)

As you can see, neither of these tables use the PRIMARY KEY when
all of the columns for the PRIMARY KEY have been provided.

mysql> explain partitions select * from t1 where status_id = 10152429551487952 and type =2 and external_userid = 8062627951 and week_of_year = 22\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t1
   partitions: week2
         type: ALL
possible_keys: PRIMARY
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 26863
        Extra: Using where
1 row in set (0.00 sec)

mysql> explain partitions select * from t1_hash where status_id = 10152429551487952 and type =2 and external_userid = 8062627951 and week_of_year = 22\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t1_hash
   partitions: p2
         type: ALL
possible_keys: PRIMARY
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 26513
        Extra: Using where
1 row in set (0.00 sec)
[6 Jun 2014 0:54] Justin Swanhart
Tables for test case

Attachment: mysql-bug-data-72893.sql.xz (application/octet-stream, text), 1.83 MiB.

[6 Jun 2014 5:12] MySQL Verification Team
Hi Justin,  strings should be quoted.  

At least on current mysql-trunk the explain reveals warnings to indicate this:

Cannot use ref access on index 'PRIMARY' due to type or collation conversion on field 'status_id'
Cannot use range access on index 'PRIMARY' due to type or collation conversion on field 'status_id'
[6 Jun 2014 18:02] Justin Swanhart
Facepalm.  Thanks Shane.