MySQL:根据EXPLAIN计划修改查询

时间:2014-05-30 02:29:26

标签: mysql sql

我有一个长期运行的查询,我想加快速度。查询的结果是一个新表。

这些表都是MYISAM,它运行在一个大型EC2实例上(m2.4xlarge,64GB RAM)。

系统使用情况如下:

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                            
17438 mysql     20   0 32.7g 7.1g 7420 S    2 10.6   1:35.82 mysqld 

我的cnf的相关部分:

key_buffer      = 32768M
max_allowed_packet  = 96M
thread_stack    = 192K
thread_cache_size   = 8
sort_buffer_size    = 2M
read_buffer_size    = 2M
read_rnd_buffer_size = 8M


table_cache             = 512
thread_concurrency      = 8
bulk_insert_buffer_size = 2048M
max_write_lock_count    = 1

# ~1/4 of memory of machine
max_heap_table_size = 16384M 
tmp_table_size      = 16384M

# ~1/4 of memory
myisam_sort_buffer_size = 17179869184

当我运行这个简单的查询时,它需要比我想象的要长得多,并且机器上的内存和CPU使用率很低。

查询解释计划如下所示:

mysql> explain SELECT encounter_id
-> FROM encounters e, sampled_patients sp
-> WHERE e.patient_id = sp.patient_id;
+----+-------------+-------+-------+---------------+------------+---------+--------------------+---------+-------------+
| id | select_type | table | type  | possible_keys | key        | key_len | ref                | rows    | Extra       |
+----+-------------+-------+-------+---------------+------------+---------+--------------------+---------+-------------+
|  1 | SIMPLE      | sp    | index | patient_id    | patient_id | 4       | NULL               | 1537954 | Using index |
|  1 | SIMPLE      | e     | ref   | patient_id    | patient_id | 5       | noah.sp.patient_id |       1 | Using where |
+----+-------------+-------+-------+---------------+------------+---------+--------------------+---------+-------------+
2 rows in set (0.00 sec)

它必须查看~1.5M行,但它已编入索引。我怎样才能加快速度呢?

0 个答案:

没有答案