简体   繁体   English

MySQL查询WHERE条件慢的大表

[英]MySql query WHERE condition slow in large table

I have a table (ip2country) of 1.5 million record and I'm executing the following query which takes more than 4 seconds My table structure 我有一个150万条记录的表(ip2country),并且正在执行以下查询,该查询耗时超过4秒我的表结构

CREATE TABLE `ip2country` (
  `beginIPNum` bigint(20) DEFAULT NULL,
  `endIPNum` bigint(20) DEFAULT NULL,
  `countryId` varchar(4) DEFAULT NULL,
  `countryName` varchar(50) DEFAULT NULL,
  `state` varchar(50) DEFAULT NULL,
  `city` varchar(50) DEFAULT NULL,
  KEY `index1` (`beginIPNum`,`endIPNum`) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=utf8
PARTITION BY RANGE  COLUMNS(beginIPNum,endIPNum)
(PARTITION p0 VALUES LESS THAN (16777216,16777216) ENGINE = MyISAM,
 PARTITION p1 VALUES LESS THAN (251658240,251658240) ENGINE = MyISAM,
 PARTITION p2 VALUES LESS THAN (503316480,503316480) ENGINE = MyISAM,
 PARTITION p3 VALUES LESS THAN (754974720,754974720) ENGINE = MyISAM,
 PARTITION p4 VALUES LESS THAN (1006632960,1006632960) ENGINE = MyISAM,
 PARTITION p5 VALUES LESS THAN (1258291200,1258291200) ENGINE = MyISAM,
 PARTITION p6 VALUES LESS THAN (1509949440,1509949440) ENGINE = MyISAM,
 PARTITION p7 VALUES LESS THAN (1761607680,1761607680) ENGINE = MyISAM,
 PARTITION p8 VALUES LESS THAN (2013265920,2013265920) ENGINE = MyISAM,
 PARTITION p9 VALUES LESS THAN (2264924160,2264924160) ENGINE = MyISAM,
 PARTITION p10 VALUES LESS THAN (2516582400,2516582400) ENGINE = MyISAM,
 PARTITION p11 VALUES LESS THAN (2768240640,2768240640) ENGINE = MyISAM,
 PARTITION p12 VALUES LESS THAN (3019898880,3019898880) ENGINE = MyISAM,
 PARTITION p13 VALUES LESS THAN (3271557120,3271557120) ENGINE = MyISAM,
 PARTITION p14 VALUES LESS THAN (3523215360,3523215360) ENGINE = MyISAM,
 PARTITION p15 VALUES LESS THAN (3774873600,3774873600) ENGINE = MyISAM,
 PARTITION p16 VALUES LESS THAN (4294967295,4294967295) ENGINE = MyISAM)

SELECT beginIPNum,endIPNum,countryId,countryName 
FROM sdportallog.ip2country 
WHERE 2130706433 BETWEEN beginIPNum AND endIPNum

Can anyone help me please? 谁能帮我吗?

Try 尝试

SELECT beginIPNum,endIPNum,countryId,countryName 
FROM sdportallog.ip2country            
WHERE beginIPNum  <= 2130706433 AND endIPNum >= 2130706433 

The query optimization is to create a couple of indexes. 查询优化是创建几个索引。 The following script will create two indexes on the table, one on each field. 以下脚本将在表上创建两个索引,每个字段一个。

-- Create index on IPFrom
CREATE INDEX index_beginIPNum
    ON sdportallog.ip2country(beginIPNum)

-- Create index on IPTo
CREATE INDEX index_endIPNum
    ON sdportallog.ip2country(endIPNum)

If you are fine with indexes. 如果索引很好。 You can try the following approach: 您可以尝试以下方法:

  1. Add partitions 添加分区
  2. Add another column to your table 在表格中添加另一列
  3. Change query 变更查询

Partitions : Try to add partitions (you can test RANGE and HASH). 分区 :尝试添加分区(可以测试RANGE和HASH)。 Right strategy you can choose by your own simply re-creating table and re-executing query. 您可以通过自己简单的重新创建表和重新执行查询来选择正确的策略。 For HASH you can start for example with 10 partitions. 对于HASH,您可以从10个分区开始。 For RANGE you can: 对于RANGE,您可以:

  • inet_aton(1.0.0.0) ... inet_aton(255.0.0.0). inet_aton(1.0.0.0)... inet_aton(255.0.0.0)。 No need to create 255 partitions. 无需创建255个分区。
  • if for any group too many countries exists per group X.0.0.0 you can try to narrow partitions and divide by XY0.0 如果对于任何组X.0.0.0,存在太多国家,则可以尝试缩小分区并除以XY0.0

Tables changes : I suppose it is good practice to add partitioning parameter as column and index it. 表的变化 :我认为将分区参数添加为列并为其编制索引是一种好习惯。 But feel free this step. 但是请放心这一步。

Changes related you query : you should take about adding filtering on your hash/range values. 与查询有关的更改 :您应该考虑在哈希/范围值上添加过滤。 For example, if you are partitioning by X.0.0.0 you should add something like where your_added_column = inet_aton(X.0.0.0) and your_base_condition 例如,如果要按X.0.0.0进行分区,则应添加以下内容: where your_added_column = inet_aton(X.0.0.0) and your_base_condition

Try to play with different partition types, amount of partitions. 尝试使用不同的分区类型,分区数量。

Hope it helps. 希望能帮助到你。

从sdportallog.ip2country中选择beginIPNum,endIPNum,countryId,countryName WHERE 2130706433 beginIPNum和endIPNum 限制1之间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM