mysql离开连接多表的地方很慢

时间:2017-08-27 04:10:00

标签: mysql performance left-join

阅读其他左边加入慢文章,试过很多方法,但仍然没有答案。

t_pbl_list是ip子网黑名单。 800K排。

CREATE TABLE `t_pbl_list` (
    `_mask` tinyint(3) unsigned NOT NULL, #subnet
    `_ipNet` int(10) unsigned NOT NULL,
    `_attr` tinyint(3) unsigned NOT NULL, # bit0: exception
    PRIMARY KEY (`_mask`,`_ipNet`),
    KEY `ix_pbl_list` (`_mask`,`_ipNet`,`_attr`)
) ENGINE=MEMORY DEFAULT CHARSET=latin1

t_pbl_result是ip结果列表3.7M行..

CREATE TABLE `t_pbl_result` (
  `_ip` int(10) unsigned NOT NULL,
  `_utSlotAdd` mediumint(8) unsigned NOT NULL,
  PRIMARY KEY (`_ip`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

我想从_ip中选择t_pbl_result而不是t_pbl_list

我不知道为什么这个sql需要50秒的内存。为Myisam跑200秒。

1, SIMPLE, A, index, , PRIMARY, 4, , 3687929, Using index
1, SIMPLE, B32, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B31, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B30, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B29, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B28, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B27, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B26, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B25, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B24, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B23, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B22, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B21, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B20, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B19, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B18, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B17, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B16, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B15, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B14, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B13, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B12, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B11, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B10, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B09, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, 
1, SIMPLE, B08, eq_ref, PRIMARY,ix_pbl_list, PRIMARY, 5, const,func, 1, Using where

SQL:

SELECT 
A._ip
FROM t_pbl_result AS A
LEFT JOIN t_pbl_list AS B32 ON B32._mask = 32 AND B32._ipNet = A._ip >>  0  
LEFT JOIN t_pbl_list AS B31 ON B31._mask = 31 AND B31._ipNet = A._ip >> 01
LEFT JOIN t_pbl_list AS B30 ON B30._mask = 30 AND B30._ipNet = A._ip >> 02
LEFT JOIN t_pbl_list AS B29 ON B29._mask = 29 AND B29._ipNet = A._ip >> 03
LEFT JOIN t_pbl_list AS B28 ON B28._mask = 28 AND B28._ipNet = A._ip >> 04
LEFT JOIN t_pbl_list AS B27 ON B27._mask = 27 AND B27._ipNet = A._ip >> 05
LEFT JOIN t_pbl_list AS B26 ON B26._mask = 26 AND B26._ipNet = A._ip >> 06
LEFT JOIN t_pbl_list AS B25 ON B25._mask = 25 AND B25._ipNet = A._ip >> 07
LEFT JOIN t_pbl_list AS B24 ON B24._mask = 24 AND B24._ipNet = A._ip >> 08
LEFT JOIN t_pbl_list AS B23 ON B23._mask = 23 AND B23._ipNet = A._ip >> 09
LEFT JOIN t_pbl_list AS B22 ON B22._mask = 22 AND B22._ipNet = A._ip >> 10
LEFT JOIN t_pbl_list AS B21 ON B21._mask = 21 AND B21._ipNet = A._ip >> 11
LEFT JOIN t_pbl_list AS B20 ON B20._mask = 20 AND B20._ipNet = A._ip >> 12
LEFT JOIN t_pbl_list AS B19 ON B19._mask = 19 AND B19._ipNet = A._ip >> 13
LEFT JOIN t_pbl_list AS B18 ON B18._mask = 18 AND B18._ipNet = A._ip >> 14
LEFT JOIN t_pbl_list AS B17 ON B17._mask = 17 AND B17._ipNet = A._ip >> 15
LEFT JOIN t_pbl_list AS B16 ON B16._mask = 16 AND B16._ipNet = A._ip >> 16
LEFT JOIN t_pbl_list AS B15 ON B15._mask = 15 AND B15._ipNet = A._ip >> 17
LEFT JOIN t_pbl_list AS B14 ON B14._mask = 14 AND B14._ipNet = A._ip >> 18
LEFT JOIN t_pbl_list AS B13 ON B13._mask = 13 AND B13._ipNet = A._ip >> 19
LEFT JOIN t_pbl_list AS B12 ON B12._mask = 12 AND B12._ipNet = A._ip >> 20
LEFT JOIN t_pbl_list AS B11 ON B11._mask = 11 AND B11._ipNet = A._ip >> 21
LEFT JOIN t_pbl_list AS B10 ON B10._mask = 10 AND B10._ipNet = A._ip >> 22
LEFT JOIN t_pbl_list AS B09 ON B09._mask = 09 AND B09._ipNet = A._ip >> 23
LEFT JOIN t_pbl_list AS B08 ON B08._mask = 08 AND B08._ipNet = A._ip >> 24
WHERE 
(        # not in any subnet.
         B32._mask IS NULL AND B31._mask IS NULL AND B30._mask IS NULL AND B29._mask IS NULL AND B28._mask IS NULL AND B27._mask IS NULL
     AND B26._mask IS NULL AND B25._mask IS NULL AND B24._mask IS NULL AND B23._mask IS NULL AND B22._mask IS NULL AND B21._mask IS NULL
     AND B20._mask IS NULL AND B19._mask IS NULL AND B18._mask IS NULL AND B17._mask IS NULL AND B16._mask IS NULL AND B15._mask IS NULL
     AND B14._mask IS NULL AND B13._mask IS NULL AND B12._mask IS NULL AND B11._mask IS NULL AND B10._mask IS NULL AND B09._mask IS NULL
     AND B08._mask IS NULL 
)
OR
(       # any subnet has exception attr.
        B32._attr & 1 <=> 1 OR B31._attr & 1 <=> 1 OR B30._attr & 1 <=> 1 OR B29._attr & 1 <=> 1 OR B28._attr & 1 <=> 1 OR B27._attr & 1 <=> 1 
     OR B26._attr & 1 <=> 1 OR B25._attr & 1 <=> 1 OR B24._attr & 1 <=> 1 OR B23._attr & 1 <=> 1 OR B22._attr & 1 <=> 1 OR B21._attr & 1 <=> 1 
     OR B20._attr & 1 <=> 1 OR B19._attr & 1 <=> 1 OR B18._attr & 1 <=> 1 OR B17._attr & 1 <=> 1 OR B16._attr & 1 <=> 1 OR B15._attr & 1 <=> 1 
     OR B14._attr & 1 <=> 1 OR B13._attr & 1 <=> 1 OR B12._attr & 1 <=> 1 OR B11._attr & 1 <=> 1 OR B10._attr & 1 <=> 1 OR B09._attr & 1 <=> 1 
     OR B08._attr & 1 <=> 1 
)
;

在没有条件的情况下尝试查询。只需将_ip,_attr合并到其他表中。也很慢。

尝试使用子查询来代替连接。也很慢。

尝试使用不存在于相反的连接中。也很慢。

尝试删除&gt;&gt;操作功能。也很慢。

我认为内部联接可以更快,但我需要在同一个表中加入所有表。

1 个答案:

答案 0 :(得分:0)

50秒?嗯,需要时间做3.7M * 24 JOINs

t_pbl_list中的ip-ranges是否重叠?如果没有,那么可以在单个测试中执行每个测试的优化,而不是24个测试。仍有3.7M测试。见http://mysql.rjweb.org/doc.php/ipranges