如何使这个mysql加入更快

时间:2017-03-14 16:12:21

标签: mysql

mysql 5.7.17

create temporary table tmp(_ip INT UNSIGNED primary key) engine = myisam

CREATE TABLE `t_rep` (
  `_ip` int(10) unsigned NOT NULL,
  `_timeInsert` int(10) unsigned NOT NULL,
  `_timeUpdate` int(10) unsigned NOT NULL,
  `_other1` int(10) unsigned NOT NULL,
  `_other2` tinyint(3) unsigned NOT NULL,
  `_other3` tinyint(3) unsigned NOT NULL,
  `_subRatting` tinyint(3) unsigned NOT NULL,
  `_other4` int(10) unsigned NOT NULL,
  `_other5` tinyint(3) unsigned NOT NULL,
  `_other6` int(10) unsigned NOT NULL,
  `_other7` tinyint(3) unsigned NOT NULL,
  `_other8` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY (`_ip`),
  KEY `ix_cb1` (`_ip`,`_timeInsert`,`_timeUpdate`,`_subRatting`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
/*!50100 PARTITION BY RANGE (_ip DIV 256 % 256 % 10)
(PARTITION part0 VALUES LESS THAN (1) ENGINE = MyISAM,
 PARTITION part1 VALUES LESS THAN (2) ENGINE = MyISAM,
 PARTITION part2 VALUES LESS THAN (3) ENGINE = MyISAM,
 PARTITION part3 VALUES LESS THAN (4) ENGINE = MyISAM,
 PARTITION part4 VALUES LESS THAN (5) ENGINE = MyISAM,
 PARTITION part5 VALUES LESS THAN (6) ENGINE = MyISAM,
 PARTITION part6 VALUES LESS THAN (7) ENGINE = MyISAM,
 PARTITION part7 VALUES LESS THAN (8) ENGINE = MyISAM,
 PARTITION part8 VALUES LESS THAN (9) ENGINE = MyISAM,
 PARTITION part9 VALUES LESS THAN (10) ENGINE = MyISAM) */

表tmp有~10M记录; table t_repu有~1B记录;

select count(*) from tmp as a
    inner join t_rep as b 
      on b._ip = a._ip 
     and b._timeInsert < unix_timestamp('2016-03-01')
     and b._timeUpdate < unix_timestamp('2016-03-01')
     and b._subRatting <= 4;

问题: 1.为什么我不能使用索引t_rep.ix_cb1? 2.我必须为表tmp设置主键吗? 3.如何让它运行得更快?

# id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows, filtered, Extra
1, SIMPLE, a, , index, PRIMARY, PRIMARY, 4, , 18878329, 100.00, Using index
1, SIMPLE, b, part0~9, eq_ref, PRIMARY,ix_cb1, PRIMARY, 4, a._ip, 1, 5.00, Using where

0 个答案:

没有答案