0

我有 2 台 mysql 服务器

服务器 A 有 mysql 5.0.51 - 8GB RAM 单四核 服务器 B 有 mysql 5.1.66 - 64GB RAM - 2x 四核

运行以下查询

 select FULLNAME ,(select COUNT(*) FROM ORDERS S, ACCOUNTS T WHERE S.CREATED BETWEEN  '2013-04-21 00:00' AND '2013-04-27 23:59'  AND S.ACCOUNT=T.ACCOUNT AND T.USERNAME=U.USERNAME AND T.CUSTOMERSTATUS = 'Donation'
     and T.TIMEST=
      (SELECT TC.TIMEST FROM  DETAILS A, ACCOUNTS TC WHERE S.ACCOUNT=A.ACCOUNT AND A.ACCOUNT = TC.ACCOUNT AND T.USERNAME=U.USERNAME AND T.CUSTOMERSTATUS = 'Donation' AND A.ANAL16 <> 'Cheque' order by TIMEST DESC LIMIT 1))

来自用户 U

在服务器 A 上,它在 27 秒内完成

在服务器 B 上它永远不会完成 - 我只是在发送数据 400 秒后终止它。

这是来自服务器 A 的配置变量

join_buffer_size 131072 key_buffer_size
16777216
myisam_sort_buffer_size 8388608
net_buffer_length 16384
preload_buffer_size 32768
read_buffer_size 131072
read_rnd_buffer_size 262144
sort_buffer_size 2097144

和服务器 B 一样

join_buffer_size 131072 key_buffer_size
16777216
myisam_sort_buffer_size 8388608
net_buffer_length 16384
preload_buffer_size 32768
read_buffer_size 131072
read_rnd_buffer_size 262144
sort_buffer_size 2097144
sql_buffer_result OFF

我只是想不通为什么它不能在更快、更强大的服务器上完成。

我在网上找到了一些帖子,但他们都提到这是一个“索引”问题,但我无法理解两台机器之间有什么不同,今天早上我把所有索引都转储了,所有索引都重新导入了.

任何帮助将非常感激!

用解释代码更新

服务器 A

  1  PRIMARY             U      ALL   NULL                           NULL        NULL     NULL                                      57  Using where; Using temporary; Using filesort  
  3  DEPENDENT SUBQUERY  S      ALL   PRIMARY,ACCSTO0472             NULL        NULL     NULL                                    3948  Using where; Using temporary                  
  3  DEPENDENT SUBQUERY  T      ref   PRIMARY,TELCOM0473             TELCOM0473  9        func                                       1  Using where                                   
  4  DEPENDENT SUBQUERY  TC     ref   PRIMARY,TELCOM0472             PRIMARY     98       tms42_gg.S.ACCOUNT                      2273  Using where; Using temporary; Using filesort  
  4  DEPENDENT SUBQUERY  A      ref   PRIMARY,RCMANL0472,RCMANL0473  RCMANL0473  98       tms42_gg.S.ACCOUNT                         1  Using where; Using index                      
  2  DEPENDENT SUBQUERY  R      ALL   PRIMARY                        NULL        NULL     NULL                                     636  Using where; Using temporary                  
  2  DEPENDENT SUBQUERY  T      ref   PRIMARY,ACCSTO0122             ACCSTO0122  250      tms42_gg.R.ACCOUNT,tms42_gg.U.USERNAME     1  Using where   

服务器 B

|  1 | PRIMARY            | U     | ALL   | NULL                                      | NULL       | NULL    | NULL                                   |   57 | Using where; Using temporary; Using filesort |
|  3 | DEPENDENT SUBQUERY | S     | ALL   | PRIMARY,ACCSTO0472                        | NULL       | NULL    | NULL                                   | 3948 | Using where; Using temporary                 |
|  3 | DEPENDENT SUBQUERY | T     | ref   | PRIMARY,TELCOM0473,TELCOM047J,TELCOM047JR | TELCOM0473 | 9       | func                                   |    1 | Using where                                  |
|  4 | DEPENDENT SUBQUERY | TC    | index | PRIMARY,TELCOM0472,TELCOM047J,TELCOM047JR | TELCOM0473 | 9       | NULL                                   |    1 | Using where; Using temporary                 |
|  4 | DEPENDENT SUBQUERY | A     | ref   | PRIMARY,RCMANL0472,RCMANL0473             | RCMANL0473 | 98      | tms42_gg.S.ACCOUNT                     |    1 | Using where; Using index                     |
|  2 | DEPENDENT SUBQUERY | R     | ALL   | PRIMARY                                   | NULL       | NULL    | NULL                                   |  636 | Using where; Using temporary                 |
|  2 | DEPENDENT SUBQUERY | T     | ref   | PRIMARY,ACCSTO0122                        | ACCSTO0122 | 250     | tms42_gg.R.ACCOUNT,tms42_gg.U.USERNAME |    1 | Using where    

在两个地方运行解释之前,我设置了 SESSION SQL_BUFFER_RESULT= ON - 结果仍然相同!

4

1 回答 1

0

对于嵌套的相关查询,该 SQL 看起来效率很低。

不确定我是否完全理解您要执行的操作,但请尝试将其重新编码为:-

SELECT U.FULLNAME , Sub2.RecCount
FROM USERS U
LEFT OUTER JOIN (select T.USERNAME, COUNT(*) AS RecCount
FROM ORDERS S
INNER JOIN ACCOUNTS T ON S.ACCOUNT = T.ACCOUNT 
INNER JOIN (SELECT A.ACCOUNT, MAX(TC.TIMEST) AS MaxTimeSt
      FROM  DETAILS A
      INNER JOIN ACCOUNTS TC ON A.ACCOUNT = TC.ACCOUNT
      WHERE A.ANAL16 != 'Cheque' 
      GROUP BY A.ACCOUNT) Sub1 ON S.ACCOUNT = Sub1.ACCOUNT AND T.TIMEST = Sub1.MaxTimeSt
WHERE S.CREATED BETWEEN  '2013-04-21 00:00' AND '2013-04-27 23:59'  
AND T.USERNAME = U.USERNAME 
AND T.CUSTOMERSTATUS = 'Donation' 
GROUP BY T.USERNAME) Sub2
ON Sub2.USERNAME = U.USERNAME 

未测试所以请原谅任何错别字

于 2013-04-29T15:51:02.120 回答