加快口才查询 [英] Speed up Eloquent query
问题描述
我正在尝试查询大量数据(4万条记录),并计划将来查询更大的数据集.口才似乎要花很长时间才能加载此数据.我想知道是否有更快的方法来处理这些数据.我试图查看数据的有效性,因此检查所有字段是否为空.
I'm trying to query a large amount of data (40K records), and planning to query much larger datasets in the future. Eloquent seems to take a very long time to load this data. I wondered if there is a faster way to process this data. I'm attempting to look at the validity of my data, and hence checking to see if all fields are null.
我经常使用雄辩的电话.我认为对数据进行分块是不合适的,因为我不打算以任何方式修改数据.我在辩论是否经常执行某项工作并称其结果为更好的方法.
I've used regular Eloquent calls. I don't think chunking the data is appropriate as I'm not planning on modifying the data in any way. I debated whether running a job every so often and calling the results of this job might be a better approach.
$journal = Journal::where('issn', $this->issn)->first();
$collection = $journal->outputs;
$collectionUnique = $collection->unique('doi');
$collectionDupes = $collection->diff($collectionUnique);
dd('Total Articles '.$this->getTotal(), 'Total Articles '.count($collection));
推荐答案
只需使用查询生成器!
这是原因:
Query Builder比Eloquent快快:
比较(雄辩的vs查询生成器):
Comparison (Eloquent vs Query Builder ) :
要为一个简单的表插入1000行,Eloquent需要1.2秒,在这种情况下,DB Facade仅需要800毫秒(ms).
To insert 1000 rows for a simple table Eloquent takes 1.2 seconds and in that case DB facades take only 800 mili seconds(ms).
另一个比较:
ORM的平均响应时间
Another comparison :
Eloquent ORM average response time
Joins | Average (ms)
------+-------------
1 | 162,2
3 | 1002,7
4 | 1540,0
Result of select operation average response time for Eloquent ORM
原始SQL平均响应时间
Joins | Average (ms)
------+-------------
1 | 116,4
3 | 130,6
4 | 155,2
Result of select operation average response time for Raw SQL
更多信息: Laravel Eloquent vs查询生成器
已
您的代码应为:
Edited :
Your code should be :
$journal = DB::table('journals')->where('issn', $this->issn)->first();
然后使用Collection(简单方法):
And Then For using Collection ( Simple way ) :
$journal = Collection::make($journal); //For use Collection Methods
$collection = $journal->get("outputs");//Changed
$collectionUnique = $collection->unique('doi');
$collectionDupes = $collection->diff($collectionUnique);
dd('Total Articles '.$this->getTotal(), 'Total Articles '.count($collection));
最佳表现:
使用查询和 Query Builder 代替集合.因为SQL中的运算通常更快.
Use queries and Query Builder instead of collections . Because operations in SQL often is faster .
请比较您的上一个代码和该代码的时间,并在评论中告诉我:)
Please compare time for your last code and this code and please let me know in comments :)
这篇关于加快口才查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!