1 奇怪 update join 2表不同的写法执行时间差很远update aa a , bb b set aa.isHis='Y' where a.aid=b.bid
发现
比起 
update aa a set aa.isHis='Y' where a.aid in (select id from bb)
要慢 下面明显要快2  同样的java代码 +mysql , db链接在a机器(mysql)上跑很快 , db链接在b机器上(mysql)发现很慢。
不知道如何分析原因 ,观察配置也没有发现太大问题 ??

解决方案 »

  1.   

    explain 看一下explain select * from aa a , bb b where a.aid=b.bid
    explain  select * from aa a where a.aid in (select id from bb)
      

  2.   

    恩,应该2比1要快,因为mysql用的nl连接方式,2方式直接用上索引去扫描了,而1会对整个表扫了
      

  3.   

    explain select * from aa a , bb b where a.aid=b.bid
    +----+-------------+-------+------+------------------------+------------------------+---------+-------------------+--------+-------------+
    | id | select_type | table | type | possible_keys          | key                    | key_len | ref               | rows   | Extra       |
    +----+-------------+-------+------+------------------------+------------------------+---------+-------------------+--------+-------------+
    |  1 | SIMPLE      | n     | ALL  | idx_heap_id            | NULL                   | NULL    | NULL              | 220000 |             |
    |  1 | SIMPLE      | m     | ref  | idx_EP_eventId_history | idx_EP_eventId_history | 8       | oddsmatrixdb.n.id |     12 | Using where |
    +----+-------------+-------+------+------------------------+------------------------+---------+-------------------+--------+-------------+
    2 rows in setexplain select * from aa a where a.aid in (select id from bb)
    +----+--------------------+-------+----------------+---------------+-------------+---------+------+---------+-------------+
    | id | select_type        | table | type           | possible_keys | key         | key_len | ref  | rows    | Extra       |
    +----+--------------------+-------+----------------+---------------+-------------+---------+------+---------+-------------+
    |  1 | PRIMARY            | m     | ALL            | NULL          | NULL        | NULL    | NULL | 1333711 | Using where |
    |  2 | DEPENDENT SUBQUERY | n     | index_subquery | idx_heap_id   | idx_heap_id | 5       | func |      21 |             |
    +----+--------------------+-------+----------------+---------------+-------------+---------+------+---------+-------------+
    2 rows in set奇怪后面的速度好快的 。 前面慢得不得了。bb是 memory表 ,后面的能走hash索引导致飞快
      

  4.   

    你的语句和EXPALIN结果并不一致啊。 m, n 表是哪儿来的?
      

  5.   

    select * from aa a , bb b where a.aid=b.bid---------走的是全表
    主要是后者保证走hash索引 避免全表
    根据1年多的mysql调优经验 总结