是什么导致我的Laravel应用程序变慢?

时间:2016-06-23 23:34:52

标签: php sql-server laravel pdo eloquent

我有一个使用Laravel 5.2和Eloquent ORM开发的应用程序。应用程序连接到MS SQL Server以读取/写入数据。

我遇到性能问题,应用程序某个时候减慢“页面需要5秒以上才能响应”

我安装了“发条”以试图查看需要很长时间才能执行的内容。在深入研究多个请求之后,我注意到一些需要很长时间才能完成的查询。

以下是一个大约需要1600毫秒才能完成查询的示例。 (1600ms包括犯罪和网络延迟。)

INSERT into [items] ([control_id], [created_at], [interview_id], [is_default], [item_id], [item_title], [item_type], [reporting_value], [updated_at])
values ('328', '2016-06-23 23:12:02', '6278', 'No', '1105', '1 (Poor)', 'Answer', '1', '2016-06-23 23:12:02'), 
('328', '2016-06-23 23:12:02', '6278', 'No', '1106', '2', 'Answer', '2', '2016-06-23 23:12:02'), 
('328', '2016-06-23 23:12:02', '6278', 'No', '1107', '3', 'Answer', '3', '2016-06-23 23:12:02'), 
('328', '2016-06-23 23:12:02', '6278', 'No', '1108', '4', 'Answer', '4', '2016-06-23 23:12:02'), 
('328', '2016-06-23 23:12:02', '6278', 'No', '1109', '5', 'Answer', '5', '2016-06-23 23:12:02'), 
('328', '2016-06-23 23:12:02', '6278', 'No', '1110', '6', 'Answer', '6', '2016-06-23 23:12:02'), 
('328', '2016-06-23 23:12:02', '6278', 'No', '1111', '7', 'Answer', '7', '2016-06-23 23:12:02'), 
('328', '2016-06-23 23:12:02', '6278', 'No', '1112', '8', 'Answer', '8', '2016-06-23 23:12:02'), 
('328', '2016-06-23 23:12:02', '6278', 'No', '1113', '9', 'Answer', '9', '2016-06-23 23:12:02'), 
('328', '2016-06-23 23:12:02', '6278', 'No', '1114', '10 (Excellent)', 'Answer', '10', '2016-06-23 23:12:02'), 
('328', '2016-06-23 23:12:02', '6278', 'No', '1608', 'Don''t Know', 'Others', '98', '2016-06-23 23:12:02'), 
('328', '2016-06-23 23:12:02', '6278', 'No', '1708', 'Does not have item/department', 'Answer', '99', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1205', '1 (Poor)', 'Answer', '1', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1206', '2', 'Answer', '2', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1207', '3', 'Answer', '3', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1208', '4', 'Answer', '4', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1209', '5', 'Answer', '5', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1210', '6', 'Answer', '6', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1211', '7', 'Answer', '7', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1212', '8', 'Answer', '8', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1213', '9', 'Answer', '9', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1214', '10 (Excellent)', 'Answer', '10', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1618', 'Don''t Know', 'Others', '98', '2016-06-23 23:12:02'), 
('338', '2016-06-23 23:12:02', '6278', 'No', '1721', 'Does not have item/department', 'Answer', '99', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1472', '1 (Poor)', 'Answer', '1', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1473', '2', 'Answer', '2', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1474', '3', 'Answer', '3', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1475', '4', 'Answer', '4', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1476', '5', 'Answer', '5', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1477', '6', 'Answer', '6', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1478', '7', 'Answer', '7', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1479', '8', 'Answer', '8', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1480', '9', 'Answer', '9', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1481', '10 (Excellent)', 'Answer', '10', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1648', 'Don''t Know', 'Others', '98', '2016-06-23 23:12:02'), 
('408', '2016-06-23 23:12:02', '6278', 'No', '1733', 'Does not have item/department', 'Answer', '99', '2016-06-23 23:12:02')

当我使用MS SQL Server Management Studio直接执行此查询时,它在00:00:00结束,这告诉我数据库服务器运行正常并且数据服务器端没有延迟。

由于插入了大量数据,我认为PDO需要花费很长时间来准备语句,这导致了一个简单事实的延迟,即这里有36行。但是,问题也可能是PDO需要一段时间来连接数据库,这就是为什么我看到延迟是一个会延迟整个应用程序的查询。

这是另一个简单的查询,在MS SQL Management Studio中执行它时,需要超过1300毫秒才能返回在00:00:01完成的1行。

SELECT * FROM [stext] WHERE [interview_id] = '6278' and [control_id] in ('306') ORDER BY [id] ASC

此查询准备工作不多,但仍需要1300多天才能完成。

Web服务器和数据服务器都在同一网络和主机上。两者都位于同一个实体房间,所以不应该有任何网络问题。我甚至试图ping服务器,ping看起来很正常。

如何判断导致我的应用程序变慢的原因?它是PDO性能问题还是雄辩需要很长时间来准备声明?

我该如何解决这个问题?

对于上面的查询,这是我使用的雄辩代码

$records = array( array(...), array(...));
Item::insert( $records );

1 个答案:

答案 0 :(得分:0)

根据我的经验,最好不要将PDO用于具有多余记录的批量插入。慢慢地有大量准备好的数据。 最好为此目的创建简单查询。