{"id":353825,"date":"2022-09-29T13:15:55","date_gmt":"2022-09-29T13:15:55","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=353825"},"modified":"2022-09-29T13:16:02","modified_gmt":"2022-09-29T13:16:02","slug":"magento-2-how-to-handle-huge-result-sets","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/","title":{"rendered":"Magento 2: How to handle huge result sets"},"content":{"rendered":"\n<p>Hello friends !!!<\/p>\n\n\n\n<p>We often use collections to fetch and work with data from our database in Magento 2 and mostly these work perfectly fine,<br>but when we talk to fetch and iterate a huge number of records in a single process it becomes extremely difficult because memory usage becomes a problem.<br>we can face most of the problems while dealing with large collections. so we need to look for an alternative solution. In this article, I\u2019m going to talk about how to handle huge result sets in Magento2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Querying and loading a huge number of records\u2026<\/h2>\n\n\n\n<p>(Magento\\Framework\\Model\\ResourceModel\\Iterator) that enables us to iterate huge database result sets by applying callbacks to each row in the result. This is perfect when you have millions of records that you need to process without loading them all into memory.<\/p>\n\n\n\n<p>The purpose here is to only ever load a single row into memory at any time (unless you assign it to a variable outside the scope of the callback function passed to $iterator-&gt;walk()). If we achieve this then it does not matter how many rows we need to process, it should not increase our memory consumption. <\/p>\n\n\n\n<p>Let\u2019s understand  this in an example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nuse Magento\\Framework\\App\\ResourceConnection;\nuse Magento\\Framework\\Model\\ResourceModel\\IteratorFactory;\n\nclass IteratorExample\n{\n    \/** @var ResourceConnection *\/\n    private $resourceConnection;\n    \n    \/** @var IteratorFactory *\/\n    private $iteratorFactory;\n\n    public function __construct(ResourceConnection $resourceConnection, IteratorFactory $iteratorFactory)\n    {\n        $this-&gt;resourceConnection = $resourceConnection;\n        $this-&gt;iteratorFactory = $iteratorFactory;\n    }\n    \n    public function processResults(): void\n    {\n        $connection = $this-&gt;resourceConnection-&gt;getConnection();\n        $iterator = $this-&gt;iteratorFactory-&gt;create();\n        \n        $query = $connection-&gt;select()-&gt;from('table_name');\n\n        $iterator-&gt;walk((string) $query, [function (array $result) {\n            \/\/ do something with $result\n        }], [], $connection);\n    }\n}<\/pre>\n\n\n\n<p>Hope this will be helpful.<br>Thanks<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends !!! We often use collections to fetch and work with data from our database in Magento 2 and mostly these work perfectly fine,but when we talk to fetch and iterate a huge number of records in a single process it becomes extremely difficult because memory usage becomes a problem.we can face most of <a href=\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":443,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121],"tags":[2460,13101],"class_list":["post-353825","post","type-post","status-publish","format-standard","hentry","category-magento-2","tag-magento-2","tag-magento-2-iterator"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>magento-2-how-to-handle-huge-result-sets<\/title>\n<meta name=\"description\" content=\"We often use collections to fetch and work with data from our database in Magento 2 and mostly these work perfectly fine\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"magento-2-how-to-handle-huge-result-sets\" \/>\n<meta property=\"og:description\" content=\"We often use collections to fetch and work with data from our database in Magento 2 and mostly these work perfectly fine\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-29T13:15:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-29T13:16:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Gaurav Singh Nagar Koti\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gaurav Singh Nagar Koti\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/\"},\"author\":{\"name\":\"Gaurav Singh Nagar Koti\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ee1435b74c1fafb2cd2c8b7daeeba0e7\"},\"headline\":\"Magento 2: How to handle huge result sets\",\"datePublished\":\"2022-09-29T13:15:55+00:00\",\"dateModified\":\"2022-09-29T13:16:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/\"},\"wordCount\":217,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"Magento 2\",\"Magento 2 iterator\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/\",\"url\":\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/\",\"name\":\"magento-2-how-to-handle-huge-result-sets\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2022-09-29T13:15:55+00:00\",\"dateModified\":\"2022-09-29T13:16:02+00:00\",\"description\":\"We often use collections to fetch and work with data from our database in Magento 2 and mostly these work perfectly fine\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Magento 2: How to handle huge result sets\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ee1435b74c1fafb2cd2c8b7daeeba0e7\",\"name\":\"Gaurav Singh Nagar Koti\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/596ff17feb17d1fd7f0c94ad8ea6bdd8b3b3c108cb7beb43ae8b958d77366210?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/596ff17feb17d1fd7f0c94ad8ea6bdd8b3b3c108cb7beb43ae8b958d77366210?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Gaurav Singh Nagar Koti\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/gaurav-singh598\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"magento-2-how-to-handle-huge-result-sets","description":"We often use collections to fetch and work with data from our database in Magento 2 and mostly these work perfectly fine","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/","og_locale":"en_US","og_type":"article","og_title":"magento-2-how-to-handle-huge-result-sets","og_description":"We often use collections to fetch and work with data from our database in Magento 2 and mostly these work perfectly fine","og_url":"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2022-09-29T13:15:55+00:00","article_modified_time":"2022-09-29T13:16:02+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Gaurav Singh Nagar Koti","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Gaurav Singh Nagar Koti","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/"},"author":{"name":"Gaurav Singh Nagar Koti","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ee1435b74c1fafb2cd2c8b7daeeba0e7"},"headline":"Magento 2: How to handle huge result sets","datePublished":"2022-09-29T13:15:55+00:00","dateModified":"2022-09-29T13:16:02+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/"},"wordCount":217,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["Magento 2","Magento 2 iterator"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/","url":"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/","name":"magento-2-how-to-handle-huge-result-sets","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2022-09-29T13:15:55+00:00","dateModified":"2022-09-29T13:16:02+00:00","description":"We often use collections to fetch and work with data from our database in Magento 2 and mostly these work perfectly fine","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/magento-2-how-to-handle-huge-result-sets\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Magento 2: How to handle huge result sets"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ee1435b74c1fafb2cd2c8b7daeeba0e7","name":"Gaurav Singh Nagar Koti","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/596ff17feb17d1fd7f0c94ad8ea6bdd8b3b3c108cb7beb43ae8b958d77366210?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/596ff17feb17d1fd7f0c94ad8ea6bdd8b3b3c108cb7beb43ae8b958d77366210?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Gaurav Singh Nagar Koti"},"url":"https:\/\/webkul.com\/blog\/author\/gaurav-singh598\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/353825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/443"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=353825"}],"version-history":[{"count":2,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/353825\/revisions"}],"predecessor-version":[{"id":353857,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/353825\/revisions\/353857"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=353825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=353825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=353825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}