【问题标题】:How to scan a big mysql table in parallel?如何并行扫描一个大的mysql表?
【发布时间】:2014-01-23 02:54:25
【问题描述】:

假设我们有一个大的 mysql 表,它的行数少于 1000 万。

如果我想选择所有结果,显然全表扫描可以正常工作。

select * from table_name; 

但是如何让它并行呢?我发现 Sqoop 中的解决方案是Split

select * from table_name where id >= 1 and id < 10000;
select * from table_name where id >= 10000 and id < 20000;
select * from table_name where id >= 20000 and id < 30000; 
...

问题是如果id size number很大,mysql可能会把它当成Full table scan

Update1:慢查询日志

# Query_time: 600.632844  Lock_time: 0.000071 Rows_sent: 624  Rows_examined: 236584
SELECT `id`, ... FROM `table_name` WHERE ( `id` >= 647121 ) AND ( `id` <= 765101 );

Update2:解释

+----+-------------+------------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table            | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+------------------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | table_name | range | PRIMARY       | PRIMARY | 4       | NULL |    1 | Using where | 

Update3:Mysql 版本

+------------+
| version()  |
+------------+
| 5.1.46-log | 
+------------+

而且如果我们同时发送3个这样的查询,服务器的查询时间那么长,我们无法忍受。

那么,是否可以并行进行拆分查询?如果不是,为什么 Sqoop 会这样做?谢谢。

【问题讨论】:

  • 只需创建到数据库的多个连接并在问题中发出查询。
  • 我假设 id 是主键或有索引。你有没有试过EXPLAIN检查它是否真的在做表扫描?
  • @HongTat 是的id 是主键。我将在mysql中显示慢查询日志。
  • 那么解释是针对where id &gt;= 1 and id &lt; 10000 查询的吗?如果将上限减小到 100 会怎样?
  • @zerkms 似乎不是全表扫描。但是为什么这么慢呢?

标签: mysql sqoop full-table-scan


【解决方案1】:

看起来它没有使用任何键。您使用的是相当旧的 MySQL 版本吗?

EXPLAIN 应该是这样的:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  table_name  range   PRIMARY     PRIMARY     4   NULL    5926    Using index condition

【讨论】:

  • 对不起,我对解释结果有误。已经更新了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多