【问题标题】:mysql partition pruning not workmysql分区修剪不起作用
【发布时间】:2012-08-16 09:08:07
【问题描述】:

我通过hash(to_days(...))创建了一个带有 MySQL 分区的表。

CREATE TABLE `requestlog` (
  `remotehost` varchar(40) DEFAULT NULL,
  `user` varchar(255) DEFAULT NULL,
  `request_time_str` varchar(40) DEFAULT NULL,
  `request_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `request_line` varchar(255) DEFAULT NULL,
  `status` int(11) DEFAULT NULL,
  `bytes` int(11) DEFAULT NULL,
  `referer` text,
  `useragent` text,
  `host` text,
  `instance` text,
  `ms` int(11) DEFAULT NULL,
  `cpu_ms` int(11) DEFAULT NULL,
  `api_cpu_ms` int(11) DEFAULT NULL,
  `cpm_usd` float DEFAULT NULL,
  `queue_name` varchar(40) DEFAULT NULL,
  `task_name` varchar(40) DEFAULT NULL,
  `loading_request` tinyint(1) DEFAULT NULL,
  `pending_ms` int(11) DEFAULT NULL,
  `exit_code` int(11) DEFAULT NULL,
  `throttle_code` int(11) DEFAULT NULL,
  `method` varchar(40) DEFAULT NULL,
  `path` varchar(255) DEFAULT NULL,
  `querystring` text,
  `protocol` varchar(40) DEFAULT NULL,
  `applog` text,
  `applog0` text,
  `applog1` text,
  `applog2` text,
  `applog3` text,
  `applog4` text,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMES
TAMP,
  PRIMARY KEY (`request_time`,`id`),
  UNIQUE KEY `path` (`path`,`request_time`,`remotehost`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
/*!50100 PARTITION BY HASH (to_days(request_time))
PARTITIONS 1000 */

但是,当我执行以下查询时。 explain partitions 结果显示分区修剪不起作用,因为它扫描了属于该表的所有分区...

explain partitions select count(*) from requestlog where to_days(request_time) = '2012-08-01';

我尝试了本文中的示例。解释分区仍然显示它扫描所有分区。 how to partition a table by datetime column?

如何让分区修剪起作用?有什么提示吗?

【问题讨论】:

    标签: mysql database-partitioning pruning


    【解决方案1】:

    不用to_days试试这个:

    explain partitions select count(*) from requestlog where request_time = '2012-08-01';
    

    编辑:

    explain partitions 
            select count(*) 
            from requestlog 
            where request_time BETWEEN '2012-08-01 00:00:00' AND '2012-08-01 23:59:59';
    

    【讨论】:

    • 分区修剪在这种情况下有效,但查询本身只返回 request_time = '2012-08-01 00:00:00'的结果
    • 之所以有效,是因为您在列 request_time 上有索引。还 To_DAYS 函数返回整数值,您正在与日期值进行比较。
    • 分区修剪在此查询中有效,但结果不正确。例如,此查询忽略 request_time = '2012-08-01 00:00:01' 的数据
    • 尝试新编辑的查询,或者您也可以使用DATE(request_time) = '2012-08-01';
    • where request_time between ...where date(request_time) = ... 都会扫描所有分区。我猜 MySQL 只是不支持这种分区修剪。
    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多