我正在使用Zend Framework在一个站点上工作,其中一个页面在生产服务器上的加载速度比在开发服务器上慢得多。经过一些挖掘和分析(使用xdebug)后,我发现PDOStatement->执行调用在生产服务器上运行11 134ms,在dev 1运行780ms。
两个服务器上的数据库相同,两个服务器上的MySQL版本相同(5.5.32),两个服务器上的MySQL配置相同(直到my.cnf中的行)。两个服务器也运行Ubuntu 12.04.2 LTS,尽管开发服务器确实有额外的软件与生产服务器相比。我能想到的唯一区别是, dev服务器在SSD上运行,而生产服务器在RAID 0中运行两个常规SATA驱动器。但是,在phpMyAdmin中运行查询会产生大约相同的时间(0.0003-0.0005秒,尽管我不相信phpMyAdmin执行时间报告)。
以下是我运行的查询:
$query = "SET @rank = 0";
$statement = $db->query($query);
$query = "SET @previous_result = -1";
$statement = $db->query($query);
$query = "
SELECT
@group_id := IFNULL(g.id, -1)
FROM
`branch_statistics` bs
JOIN
branch_to_wave btw ON bs.branch_to_wave_id = btw.id
JOIN
branch_to_group btg ON bs.branch_id = btg.branch_id
JOIN
`group` g ON btg.group_id = g.id
WHERE
btw.wave_id = $wave_id AND bs.branch_id = $branch_id";
$statement = $db->query($query);
$query = "DROP TABLE IF EXISTS group_members";
$statement = $db->query($query);
$query = "CREATE TEMPORARY TABLE group_members (id INT PRIMARY KEY)";
$statement = $db->query($query);
$query = "DROP TABLE IF EXISTS group_members2";
$statement = $db->query($query);
$query = "CREATE TEMPORARY TABLE group_members2 (id INT PRIMARY KEY)";
$statement = $db->query($query);
$query = "
INSERT INTO
group_members
SELECT
btw.id
FROM
branch_to_wave btw
JOIN
branch_to_group btg ON btw.branch_id = btg.branch_id
WHERE
btw.wave_id = $wave_id AND
btg.group_id = @group_id
GROUP BY
btw.id";
$statement = $db->query($query);
$query = "INSERT INTO group_members2 SELECT id FROM group_members";
$statement = $db->query($query);
$query = "
SELECT @number_of_results :=
COUNT(result)
FROM
(
SELECT
ROUND(points / maximum_points, 4) as result
FROM
`branch_statistics` bs
JOIN
group_members2 gm2 ON gm2.id = bs.branch_to_wave_id
GROUP BY
result
) results
";
$statement = $db->query($query);
$query = "
EXPLAIN SELECT
*,
@number_of_results as number_of_results
FROM
(
SELECT
*,
IF(@previous_result != result, @rank := @rank + 1, @rank) as rank,
@previous_result := result
FROM
(
SELECT
bs.branch_id,
ROUND(points / maximum_points, 4) as result,
(
SELECT
AVG(ROUND(points / maximum_points, 4)) as average
FROM
`branch_statistics` bs
JOIN
group_members2 gm2 ON gm2.id = bs.branch_to_wave_id
) as average
FROM
`branch_statistics` bs
JOIN
group_members gm ON gm.id = bs.branch_to_wave_id
GROUP BY
bs.branch_id
ORDER BY
result DESC
) complete
) results
WHERE branch_id = $branch_id
";
$statement = $db->query($query);
$statement->setFetchMode(Zend_Db::FETCH_NUM);
$results = $statement->fetchAll();
我不知道服务器规格是否相关,但在任何情况下都是这样:
生产服务器:
Intel(R)Core(TM)2 Duo CPU E7500 @ 2.93GHz,内存为3.4Gb
开发服务器(在以下硬件上运行的虚拟机,以及其他5-6个虚拟机):
AMD FX(tm)-8120具有3Gb RAM的八核处理器
所以我的问题是:如何确定造成这种差异的原因?
编辑#1 (2013-08-16 @ 11:23):对于那些不熟悉Zend Framework的人,这里是调用堆栈导致PDOStatement->执行:
编辑#2 (2013-08-16 @ 13:09):这是两台机器上的分析结果。
Status Duration DEV Duration PROD
starting 0.000032 0.000010
Waiting for query cache lock 0.000010 0.000006
checking query cache for query 0.000183 0.000074
checking permissions 0.000010 0.000007
checking permissions 0.000009 0.000006
checking permissions 0.000009 0.000006
checking permissions 0.000011 0.000007
Opening tables 0.000030 0.000017
System lock 0.000140 0.000063
optimizing 0.000016 0.000010
statistics 0.000035 0.000016
preparing 0.000020 0.000011
Creating tmp table 0.000028 0.000015
executing 0.000009 0.000006
Copying to tmp table 0.000424 0.000133
Sorting result 0.000039 0.000017
Sending data 0.000015 0.000008
optimizing 0.000014 0.000009
statistics 0.000022 0.000012
preparing 0.000019 0.000010
executing 0.000011 0.000007
Sending data 0.000171 0.000110
optimizing 0.000006 0.000007
statistics 0.000006 0.000008
preparing 0.000006 0.000007
executing 0.000004 0.000006
Sending data 0.000042 0.000035
removing tmp table 0.000007 0.000009
Sending data 0.000006 0.000008
init 0.000011 0.000013
optimizing 0.000005 0.000008
statistics 0.000006 0.000008
preparing 0.000006 0.000009
executing 0.000004 0.000006
Sending data 0.000016 0.000019
end 0.000005 0.000007
query end 0.000005 0.000007
closing tables 0.000004 0.000006
removing tmp table 0.000005 0.000007
closing tables 0.000004 0.000006
removing tmp table 0.000006 0.000007
closing tables 0.000006 0.000008
freeing items 0.000013 0.000275
logging slow query 0.000004 0.000007
cleaning up 0.000006 0.000007
我还测试了 mysqlslap 并找到了一些非常有趣的结果,其后跟(时间是平均查询时间)。您可以看到,虽然同时具有更多同时查询确实增加了平均查询时间,但与开发服务器相比,生产服务器上的相同查询仍然慢24倍。
我真的不明白如何在phpMyAdmin中运行查询需要0.0005秒,而使用mysqlslap的相同查询需要0.299秒,但我怀疑这是我问题的核心。
Mysqlslap settings Dev Prod
iterations = 10, concurrency = 50 1.253 14.129
iterations = 10, concurrency = 25 0.513 7.153
iterations = 10, concurrency = 10 0.141 3.133
iterations = 10, concurrency = 1 0.014 0.299
答案 0 :(得分:3)
我今天终于明白了!问题是临时表不是在生产服务器的内存中创建的。我不知道为什么,因为配置文件明确说明它应该,但我通过向所有ENGINE = MEMORY
调用添加CREATE TEMPORARY TABLE
来解决问题。
E.g:
CREATE TEMPORARY TABLE group_members (id INT PRIMARY KEY) ENGINE = MEMORY