我有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))
来自USERS 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关闭
我只是无法弄清楚为什么它不会在速度更快,功能更强大的服务器上完成。
我在网上发现了一些帖子,但是他们都提到这是一个“索引”问题,但是我无法理解这两台机器之间有什么不同,今天早上我把所有索引都转了一下 - 进口罚款。
非常感谢任何帮助!
使用说明代码进行更新
服务器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 - 结果仍然相同!
答案 0 :(得分: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
未经测试,请原谅任何错别字