Laravel dabatabse facade memory usage2019 Community Moderator ElectionHow to measure actual memory usage of an application or process?Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted (CodeIgniter + XML-RPC)PDO::MYSQL_ATTR_USE_BUFFERED_QUERY and multiple delete statements in prepared statementHow do I discover memory usage of my application in Android?Creating a memory leak with JavaBest practice to iterate over result set in Postgres/PHP/PDO?How MySQL client and PDO receive big result with fetch methodPDO prepared statement vs. query generated from arrayT-SQL in PHP with PDOPHP PDO - incremental for loop that fetches the next array row with each loop

Rules about breaking the rules. How do I do it well?

Schematic conventions for different supply rails

Is Mortgage interest accrued after a December payment tax deductible?

I need to drive a 7/16" nut but am unsure how to use the socket I bought for my screwdriver

Ban on all campaign finance?

How to make healing in an exploration game interesting

What does it mean to make a bootable LiveUSB?

Is it possible that AIC = BIC?

Why are there 40 737 Max planes in flight when they have been grounded as not airworthy?

How could a scammer know the apps on my phone / iTunes account?

Make a transparent 448*448 image

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

Co-worker team leader wants to inject his friend's awful software into our development. What should I say to our common boss?

At what level can a dragon innately cast its spells?

How do I hide Chekhov's Gun?

Is it true that real estate prices mainly go up?

What is a good source for large tables on the properties of water?

Rejected in 4th interview round citing insufficient years of experience

Where is the 1/8 CR apprentice in Volo's Guide to Monsters?

Employee lack of ownership

Good allowance savings plan?

How is the Swiss post e-voting system supposed to work, and how was it wrong?

Why did it take so long to abandon sail after steamships were demonstrated?

Check this translation of Amores 1.3.26



Laravel dabatabse facade memory usage



2019 Community Moderator ElectionHow to measure actual memory usage of an application or process?Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted (CodeIgniter + XML-RPC)PDO::MYSQL_ATTR_USE_BUFFERED_QUERY and multiple delete statements in prepared statementHow do I discover memory usage of my application in Android?Creating a memory leak with JavaBest practice to iterate over result set in Postgres/PHP/PDO?How MySQL client and PDO receive big result with fetch methodPDO prepared statement vs. query generated from arrayT-SQL in PHP with PDOPHP PDO - incremental for loop that fetches the next array row with each loop










2















I've found great example written in php pdo, which helps to iterate huge amount of data without actually allocating memory for whole set of results:



 $sql = 'SELECT * from playlists limit 50000';
$statement = $pdo->prepare($sql);
$statement->execute();

while (($result = $statement->fetch(PDO::FETCH_ASSOC)) !== false)
//do something



I've done an investigation and this approach uses 18mb of memory.



If I fetch all results like this $results = $statement->fetchAll(PDO::FETCH_ASSOC); memory usage upraises to 35mb.



Using laravel's illuminate/database component and very similar approach DB::table('playlists')->limit(50000)->get(); also uses 35mb of memory.



  • How can I achieve first approach using Laravel's eloquent or DB facade?

  • Could you suggest some articles how this difference in memory usage develops?

Thanks










share|improve this question






















  • Are you comparing like with like? The Laravel framework has overheads of its own. Check memory usage of Laravel without doing the query and check how much memory usage increased when doing the query. Compare that to memory usage without Laravel before and after doing the query

    – apokryfos
    Mar 6 at 18:51
















2















I've found great example written in php pdo, which helps to iterate huge amount of data without actually allocating memory for whole set of results:



 $sql = 'SELECT * from playlists limit 50000';
$statement = $pdo->prepare($sql);
$statement->execute();

while (($result = $statement->fetch(PDO::FETCH_ASSOC)) !== false)
//do something



I've done an investigation and this approach uses 18mb of memory.



If I fetch all results like this $results = $statement->fetchAll(PDO::FETCH_ASSOC); memory usage upraises to 35mb.



Using laravel's illuminate/database component and very similar approach DB::table('playlists')->limit(50000)->get(); also uses 35mb of memory.



  • How can I achieve first approach using Laravel's eloquent or DB facade?

  • Could you suggest some articles how this difference in memory usage develops?

Thanks










share|improve this question






















  • Are you comparing like with like? The Laravel framework has overheads of its own. Check memory usage of Laravel without doing the query and check how much memory usage increased when doing the query. Compare that to memory usage without Laravel before and after doing the query

    – apokryfos
    Mar 6 at 18:51














2












2








2








I've found great example written in php pdo, which helps to iterate huge amount of data without actually allocating memory for whole set of results:



 $sql = 'SELECT * from playlists limit 50000';
$statement = $pdo->prepare($sql);
$statement->execute();

while (($result = $statement->fetch(PDO::FETCH_ASSOC)) !== false)
//do something



I've done an investigation and this approach uses 18mb of memory.



If I fetch all results like this $results = $statement->fetchAll(PDO::FETCH_ASSOC); memory usage upraises to 35mb.



Using laravel's illuminate/database component and very similar approach DB::table('playlists')->limit(50000)->get(); also uses 35mb of memory.



  • How can I achieve first approach using Laravel's eloquent or DB facade?

  • Could you suggest some articles how this difference in memory usage develops?

Thanks










share|improve this question














I've found great example written in php pdo, which helps to iterate huge amount of data without actually allocating memory for whole set of results:



 $sql = 'SELECT * from playlists limit 50000';
$statement = $pdo->prepare($sql);
$statement->execute();

while (($result = $statement->fetch(PDO::FETCH_ASSOC)) !== false)
//do something



I've done an investigation and this approach uses 18mb of memory.



If I fetch all results like this $results = $statement->fetchAll(PDO::FETCH_ASSOC); memory usage upraises to 35mb.



Using laravel's illuminate/database component and very similar approach DB::table('playlists')->limit(50000)->get(); also uses 35mb of memory.



  • How can I achieve first approach using Laravel's eloquent or DB facade?

  • Could you suggest some articles how this difference in memory usage develops?

Thanks







php laravel memory pdo






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 6 at 18:41









Evaldas ButkusEvaldas Butkus

3461819




3461819












  • Are you comparing like with like? The Laravel framework has overheads of its own. Check memory usage of Laravel without doing the query and check how much memory usage increased when doing the query. Compare that to memory usage without Laravel before and after doing the query

    – apokryfos
    Mar 6 at 18:51


















  • Are you comparing like with like? The Laravel framework has overheads of its own. Check memory usage of Laravel without doing the query and check how much memory usage increased when doing the query. Compare that to memory usage without Laravel before and after doing the query

    – apokryfos
    Mar 6 at 18:51

















Are you comparing like with like? The Laravel framework has overheads of its own. Check memory usage of Laravel without doing the query and check how much memory usage increased when doing the query. Compare that to memory usage without Laravel before and after doing the query

– apokryfos
Mar 6 at 18:51






Are you comparing like with like? The Laravel framework has overheads of its own. Check memory usage of Laravel without doing the query and check how much memory usage increased when doing the query. Compare that to memory usage without Laravel before and after doing the query

– apokryfos
Mar 6 at 18:51













2 Answers
2






active

oldest

votes


















4














When you execute an SQL query with php (either mysql functions or PDO) all data returned from query loads in to memory as a "result set".



In order to use data in "result set" you have to fetch them in regular php arrays/objects.



PDOStatement::fetch - fetches one row from the result set in to memory.



PDOStatement::fetchAll - fetches all rows from result set to memory thus doubling the memory usage.



Eloquent has ability to chunk result sets. This is equivalent to performing "X times fetch" in PDO.



However, if you are working with very large result sets consider using SQL limits.






share|improve this answer























  • Let's say I chunk by 100. So every chunk execution will allocate memory for 100 rows or just one?

    – Evaldas Butkus
    Mar 6 at 19:10






  • 2





    @EvaldasButkus Every chunk execution loads 100 rows to memory at once. At second chunk, first chunk gets overridden by second chunk. So peak memory usage should be result set + chunk size theoretically.

    – Yarimadam
    Mar 6 at 19:18


















2














The Laravel approach to processing large data sets like this is to use chunking.



DB::table('playlists')->chunk(1000, function($playlists) use($count) 
foreach($playlists as $playlist)
// do something with this playlist

);


This ensures that no more than the chunk size (in my example, 1000 rows) is loaded into RAM at once. 1k is arbitrary; you could chunk 1, 100, 253, etc.






share|improve this answer























  • If you want to cap it at 50k rows max, you'd need to do a little logic to keep track of iterations within the loop.

    – ceejayoz
    Mar 6 at 18:54










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55030098%2flaravel-dabatabse-facade-memory-usage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














When you execute an SQL query with php (either mysql functions or PDO) all data returned from query loads in to memory as a "result set".



In order to use data in "result set" you have to fetch them in regular php arrays/objects.



PDOStatement::fetch - fetches one row from the result set in to memory.



PDOStatement::fetchAll - fetches all rows from result set to memory thus doubling the memory usage.



Eloquent has ability to chunk result sets. This is equivalent to performing "X times fetch" in PDO.



However, if you are working with very large result sets consider using SQL limits.






share|improve this answer























  • Let's say I chunk by 100. So every chunk execution will allocate memory for 100 rows or just one?

    – Evaldas Butkus
    Mar 6 at 19:10






  • 2





    @EvaldasButkus Every chunk execution loads 100 rows to memory at once. At second chunk, first chunk gets overridden by second chunk. So peak memory usage should be result set + chunk size theoretically.

    – Yarimadam
    Mar 6 at 19:18















4














When you execute an SQL query with php (either mysql functions or PDO) all data returned from query loads in to memory as a "result set".



In order to use data in "result set" you have to fetch them in regular php arrays/objects.



PDOStatement::fetch - fetches one row from the result set in to memory.



PDOStatement::fetchAll - fetches all rows from result set to memory thus doubling the memory usage.



Eloquent has ability to chunk result sets. This is equivalent to performing "X times fetch" in PDO.



However, if you are working with very large result sets consider using SQL limits.






share|improve this answer























  • Let's say I chunk by 100. So every chunk execution will allocate memory for 100 rows or just one?

    – Evaldas Butkus
    Mar 6 at 19:10






  • 2





    @EvaldasButkus Every chunk execution loads 100 rows to memory at once. At second chunk, first chunk gets overridden by second chunk. So peak memory usage should be result set + chunk size theoretically.

    – Yarimadam
    Mar 6 at 19:18













4












4








4







When you execute an SQL query with php (either mysql functions or PDO) all data returned from query loads in to memory as a "result set".



In order to use data in "result set" you have to fetch them in regular php arrays/objects.



PDOStatement::fetch - fetches one row from the result set in to memory.



PDOStatement::fetchAll - fetches all rows from result set to memory thus doubling the memory usage.



Eloquent has ability to chunk result sets. This is equivalent to performing "X times fetch" in PDO.



However, if you are working with very large result sets consider using SQL limits.






share|improve this answer













When you execute an SQL query with php (either mysql functions or PDO) all data returned from query loads in to memory as a "result set".



In order to use data in "result set" you have to fetch them in regular php arrays/objects.



PDOStatement::fetch - fetches one row from the result set in to memory.



PDOStatement::fetchAll - fetches all rows from result set to memory thus doubling the memory usage.



Eloquent has ability to chunk result sets. This is equivalent to performing "X times fetch" in PDO.



However, if you are working with very large result sets consider using SQL limits.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 6 at 19:03









YarimadamYarimadam

753411




753411












  • Let's say I chunk by 100. So every chunk execution will allocate memory for 100 rows or just one?

    – Evaldas Butkus
    Mar 6 at 19:10






  • 2





    @EvaldasButkus Every chunk execution loads 100 rows to memory at once. At second chunk, first chunk gets overridden by second chunk. So peak memory usage should be result set + chunk size theoretically.

    – Yarimadam
    Mar 6 at 19:18

















  • Let's say I chunk by 100. So every chunk execution will allocate memory for 100 rows or just one?

    – Evaldas Butkus
    Mar 6 at 19:10






  • 2





    @EvaldasButkus Every chunk execution loads 100 rows to memory at once. At second chunk, first chunk gets overridden by second chunk. So peak memory usage should be result set + chunk size theoretically.

    – Yarimadam
    Mar 6 at 19:18
















Let's say I chunk by 100. So every chunk execution will allocate memory for 100 rows or just one?

– Evaldas Butkus
Mar 6 at 19:10





Let's say I chunk by 100. So every chunk execution will allocate memory for 100 rows or just one?

– Evaldas Butkus
Mar 6 at 19:10




2




2





@EvaldasButkus Every chunk execution loads 100 rows to memory at once. At second chunk, first chunk gets overridden by second chunk. So peak memory usage should be result set + chunk size theoretically.

– Yarimadam
Mar 6 at 19:18





@EvaldasButkus Every chunk execution loads 100 rows to memory at once. At second chunk, first chunk gets overridden by second chunk. So peak memory usage should be result set + chunk size theoretically.

– Yarimadam
Mar 6 at 19:18













2














The Laravel approach to processing large data sets like this is to use chunking.



DB::table('playlists')->chunk(1000, function($playlists) use($count) 
foreach($playlists as $playlist)
// do something with this playlist

);


This ensures that no more than the chunk size (in my example, 1000 rows) is loaded into RAM at once. 1k is arbitrary; you could chunk 1, 100, 253, etc.






share|improve this answer























  • If you want to cap it at 50k rows max, you'd need to do a little logic to keep track of iterations within the loop.

    – ceejayoz
    Mar 6 at 18:54















2














The Laravel approach to processing large data sets like this is to use chunking.



DB::table('playlists')->chunk(1000, function($playlists) use($count) 
foreach($playlists as $playlist)
// do something with this playlist

);


This ensures that no more than the chunk size (in my example, 1000 rows) is loaded into RAM at once. 1k is arbitrary; you could chunk 1, 100, 253, etc.






share|improve this answer























  • If you want to cap it at 50k rows max, you'd need to do a little logic to keep track of iterations within the loop.

    – ceejayoz
    Mar 6 at 18:54













2












2








2







The Laravel approach to processing large data sets like this is to use chunking.



DB::table('playlists')->chunk(1000, function($playlists) use($count) 
foreach($playlists as $playlist)
// do something with this playlist

);


This ensures that no more than the chunk size (in my example, 1000 rows) is loaded into RAM at once. 1k is arbitrary; you could chunk 1, 100, 253, etc.






share|improve this answer













The Laravel approach to processing large data sets like this is to use chunking.



DB::table('playlists')->chunk(1000, function($playlists) use($count) 
foreach($playlists as $playlist)
// do something with this playlist

);


This ensures that no more than the chunk size (in my example, 1000 rows) is loaded into RAM at once. 1k is arbitrary; you could chunk 1, 100, 253, etc.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 6 at 18:54









ceejayozceejayoz

143k34223306




143k34223306












  • If you want to cap it at 50k rows max, you'd need to do a little logic to keep track of iterations within the loop.

    – ceejayoz
    Mar 6 at 18:54

















  • If you want to cap it at 50k rows max, you'd need to do a little logic to keep track of iterations within the loop.

    – ceejayoz
    Mar 6 at 18:54
















If you want to cap it at 50k rows max, you'd need to do a little logic to keep track of iterations within the loop.

– ceejayoz
Mar 6 at 18:54





If you want to cap it at 50k rows max, you'd need to do a little logic to keep track of iterations within the loop.

– ceejayoz
Mar 6 at 18:54

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55030098%2flaravel-dabatabse-facade-memory-usage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

Алба-Юлія

Захаров Федір Захарович