【发布时间】:2015-07-04 22:49:12
【问题描述】:
我在 Laravel 4.2 应用程序中使用 Eloquent。我的一个控制器在大约 6000 毫秒后返回一个视图,这太长了。它处理来自名为responses 的表中的数据。几千行它工作得很好,但是当我超过 30000 时它开始变得非常慢。
这是奇怪的部分。
如果我检查我的DB::getQueryLog();,我可以看到这个查询:
[1] => Array
(
[query] => select * from `response` where `survey_id` = ? and `question_id` = ? and `resp_group` in (?, ?)
[bindings] => Array
(
[0] => 48
[1] => 25
[2] => a
[3] => b
)
[time] => 11
)
我认为这会导致这个实际查询:
select * from `response`
where `survey_id` = 48
and `question_id` = 25
and `resp_group` in ('a', 'b');
如您所见,那里的时间相当长。日志告诉我,我在 7 点到 11 点之间运行了大约 40 次这样的查询。
但如果我在 HediSQL 中运行相同的查询,我会在 PHPMyAdmin 中得到 0,000 秒和 0.0010 秒。我必须跑 20 次才能获得 0,015 秒。
解释一下:
select_type: SIMPLE
table: response
possible_keys:
question_id_4,
resp_group_survey_id,
survey_id,
resp_group_question_id_survey_id
key: question_id_4
key_len: 8
ref: const,const
rows: 949
Extra: Using where
所以有索引。为什么相同的查询在 Eloquent 执行时会这么慢,而当我直接在 MySQL 服务器上运行时却快得多?
(我在本地 WAMP 服务器上运行)
【问题讨论】:
标签: php mysql database laravel eloquent