Skip to content

Instantly share code, notes, and snippets.

@avimar
Created February 17, 2017 06:09
Show Gist options
  • Save avimar/16824955e21781c7e2c6594ef743bc6f to your computer and use it in GitHub Desktop.
Save avimar/16824955e21781c7e2c6594ef743bc6f to your computer and use it in GitHub Desktop.
innodb not optimizing?
`show create table logs_fs`
CREATE TABLE `logs_fs` (
`uuid` char(36) DEFAULT NULL,
`host` varchar(4) DEFAULT NULL,
`date` datetime(6) NOT NULL,
`hrtime` bigint(20) unsigned NOT NULL,
`level` tinyint(4) DEFAULT NULL,
`value` text,
KEY `uuid` (`uuid`),
KEY `date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!50100 PARTITION BY RANGE (level)
(PARTITION p0 VALUES LESS THAN (7) ENGINE = InnoDB,
PARTITION p1 VALUES LESS THAN (8) ENGINE = InnoDB) */
My naive query: sql should know which partition!
explain select SQL_NO_CACHE count(*) FROM `logs_fs` WHERE `date` < date_sub(now(), interval 1 week) and level=7
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE logs_fs ALL date NULL NULL NULL 1,777,576 Using where
I had to specify the partition AND leave out the level=7 to get down to a lower number!
explain select SQL_NO_CACHE count(*) FROM `logs_fs` PARTITION (p1) WHERE `date` < date_sub(now(), interval 1 week)
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE logs_fs range date date 8 NULL 423,360 Using where; Using index
Indeed, it works in myisam! (Funny, it says it's querying 196k rows, but it returns the proper count of 210701 which matches innodb)
CREATE TABLE `logs_fs_myisam` (
`uuid` char(36) DEFAULT NULL,
`host` varchar(4) DEFAULT NULL,
`date` datetime(6) NOT NULL,
`hrtime` bigint(20) unsigned NOT NULL,
`level` tinyint(4) DEFAULT NULL,
`value` text,
KEY `uuid` (`uuid`),
KEY `date` (`date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
/*!50100 PARTITION BY RANGE (level)
(PARTITION p0 VALUES LESS THAN (7) ENGINE = MyISAM,
PARTITION p1 VALUES LESS THAN (8) ENGINE = MyISAM) */
`explain select SQL_NO_CACHE count(*) FROM `logs_fs_myisam` WHERE `date` < date_sub(now(), interval 1 week) and level=7`
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE logs_fs_myisam range date date 8 NULL 195,890 Using where
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment