0

这是关于 MySQL 5.5

我有这张桌子:

CREATE TABLE IF NOT EXISTS isup_trace_test (
             id int(11) NOT NULL AUTO_INCREMENT,
             datetime datetime DEFAULT NULL,
             microseconds int(11) DEFAULT NULL,
             opc int(11) DEFAULT NULL,
             dpc int(11) DEFAULT NULL,
             slc int(11) NOT NULL,
             operation varchar(45) DEFAULT NULL,
             release_text varchar(45) DEFAULT NULL,
             release_code int(11) DEFAULT NULL,
             calling varchar(45) DEFAULT NULL,
             nrn varchar(10) DEFAULT NULL,
             called varchar(45) DEFAULT NULL,
             cic int(11) DEFAULT NULL,
             message text,
             filename varchar(100) NOT NULL,
             PRIMARY KEY (id,datetime),
             UNIQUE KEY opc (opc,dpc,cic,operation,datetime,microseconds),
             KEY indice (datetime,microseconds,operation,cic,calling,called),
             KEY cic_datetime (cic,datetime,microseconds),
             KEY calling (calling),
             KEY called (called),
             KEY both_a_and_b (datetime,calling),
             KEY by_release_text (release_text),
             KEY by_release_code (release_code),
             KEY operation (datetime,operation),
             KEY per_opc (datetime,opc,dpc,operation),
             KEY for_search (cic,operation,datetime),
             KEY for_search_2 (datetime,calling,operation),
             KEY for_search_3 (datetime,called,operation),
             KEY search_1 (opc,dpc,cic,operation,datetime),
             KEY por_fecha (datetime),
             KEY tata (datetime,opc,dpc),
             KEY datetime_calling_called_operation (datetime,calling,called,operation),
             KEY filename (filename)
           ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 
           PARTITION BY LIST( HOUR(datetime) )(
               PARTITION p00 VALUES IN (0)  ,
               PARTITION p01 VALUES IN (1)  DATA DIRECTORY='/mnt/disk2' INDEX DIRECTORY='/mnt/disk2',
               PARTITION p02 VALUES IN (2)  DATA DIRECTORY='/mnt/disk3' INDEX DIRECTORY='/mnt/disk3',
               PARTITION p03 VALUES IN (3)  DATA DIRECTORY='/mnt/disk4' INDEX DIRECTORY='/mnt/disk4',
               PARTITION p04 VALUES IN (4)  DATA DIRECTORY='/mnt/disk5' INDEX DIRECTORY='/mnt/disk5',
               PARTITION p05 VALUES IN (5)  DATA DIRECTORY='/mnt/disk6' INDEX DIRECTORY='/mnt/disk6',
               PARTITION p06 VALUES IN (6)  ,                                                        
               PARTITION p07 VALUES IN (7)  DATA DIRECTORY='/mnt/disk2' INDEX DIRECTORY='/mnt/disk2',
               PARTITION p08 VALUES IN (8)  DATA DIRECTORY='/mnt/disk3' INDEX DIRECTORY='/mnt/disk3',
               PARTITION p09 VALUES IN (9) DATA DIRECTORY='/mnt/disk4' INDEX DIRECTORY='/mnt/disk4',
               PARTITION p10 VALUES IN (10) DATA DIRECTORY='/mnt/disk5' INDEX DIRECTORY='/mnt/disk5',
               PARTITION p11 VALUES IN (11) DATA DIRECTORY='/mnt/disk6' INDEX DIRECTORY='/mnt/disk6',
               PARTITION p12 VALUES IN (12) ,                                                        
               PARTITION p13 VALUES IN (13) DATA DIRECTORY='/mnt/disk2' INDEX DIRECTORY='/mnt/disk2',
               PARTITION p14 VALUES IN (14) DATA DIRECTORY='/mnt/disk3' INDEX DIRECTORY='/mnt/disk3',
               PARTITION p15 VALUES IN (15) DATA DIRECTORY='/mnt/disk4' INDEX DIRECTORY='/mnt/disk4',
               PARTITION p16 VALUES IN (16) DATA DIRECTORY='/mnt/disk5' INDEX DIRECTORY='/mnt/disk5',
               PARTITION p17 VALUES IN (17) DATA DIRECTORY='/mnt/disk6' INDEX DIRECTORY='/mnt/disk6',
               PARTITION p18 VALUES IN (18) ,                                                        
               PARTITION p19 VALUES IN (19) DATA DIRECTORY='/mnt/disk2' INDEX DIRECTORY='/mnt/disk2',
               PARTITION p20 VALUES IN (20) DATA DIRECTORY='/mnt/disk3' INDEX DIRECTORY='/mnt/disk3',
               PARTITION p21 VALUES IN (21) DATA DIRECTORY='/mnt/disk4' INDEX DIRECTORY='/mnt/disk4',
               PARTITION p22 VALUES IN (22) DATA DIRECTORY='/mnt/disk5' INDEX DIRECTORY='/mnt/disk5',
               PARTITION p23 VALUES IN (23) DATA DIRECTORY='/mnt/disk6' INDEX DIRECTORY='/mnt/disk6'
           )

现在选择有分区的想法是选择只搜索相关的分区,我希望这样:

EXPLAIN PARTITIONS SELECT * 
FROM isup_trace_test PARTITION (p18)
use index (operation)
WHERE
hour(datetime) = "18" and
DATETIME >= '2013-05-23 18:07:59' AND DATETIME <= '2013-05-23 18:37:59' AND 
operation =  'Release (12)'

返回类似的东西:

id  select_type table   partitions  type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  isup_trace_test p19 range   operation   operation   56  NULL    24  Using where

但实际上它返回:

id  select_type table   partitions  type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  isup_trace_test p00,p01,p02,p03,p04,p05,p06,p07,p08,p09,p10,p11,p1...   range   operation   operation   56  NULL    24  Using where

这意味着它将搜索所有分区......但它应该只搜索分区“p19”......

我究竟做错了什么??

谢谢!

大卫

4

1 回答 1

0

如果你浏览 MySQL5.5 的文档,你会发现以下几行,

"当分区表达式使用 YEAR() 或 TO_DAYS() 函数时,修剪也可以应用于在 DATE 或 DATETIME 列上分区的表。此外,在 MySQL 5.5 中,当分区表达式使用TO_SECONDS() 函数"

这意味着您用于分区的函数“HOUR()”未针对分区修剪进行优化。

据我说,你的表分区还有一个设计缺陷。有一条关于在分区中使用唯一约束的规则。简而言之,你不能对你没有分区的东西有唯一的约束。原因是当你插入一条记录并且需要检查唯一性时,我们不想搜索每个分区来验证唯一性。索引是分区本地的(未来可能会实现全局索引)。因此,如果约束中的所有列都在分区表达式中使用,则只能具有唯一约束。

如果它真的不那么重要,您可以决定删除您的主键,否则它会将您的插入到表中。

希望这可以帮助您理解问题。

于 2014-05-02T06:31:07.587 回答