{"id":122,"date":"2026-07-17T12:45:43","date_gmt":"2026-07-17T10:45:43","guid":{"rendered":"https:\/\/www.lukaswojcik.com\/blog\/?p=122"},"modified":"2026-07-05T23:30:50","modified_gmt":"2026-07-05T21:30:50","slug":"extracting-specific-tables-from-massive-sql-dumps-using-php","status":"publish","type":"post","link":"https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/","title":{"rendered":"Extracting Specific Tables from Massive SQL Dumps using PHP"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Have you ever needed to restore just one database or a specific set of tables from a multi-gigabyte (e.g., 5GB) MySQL dump? Opening a file of that size in standard text editors like VS Code or Notepad++ usually leads to extreme lag or a complete system crash.<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of struggling with desktop software or complex command-line tools like <code>awk<\/code> or <code>sed<\/code>, we can use a simple, memory-efficient PHP script to extract exactly what we need.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The Solution: Stream Processing<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The trick to handling massive files in PHP is to avoid loading the entire file into memory at once (which functions like <code>file_get_contents()<\/code> do). By using PHP&#8217;s <code>fgets()<\/code> function, we can stream and process the file line by line.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The script below looks for the standard <code>mysqldump<\/code> comments that indicate the start of our target database or table (e.g., <code>-- Current Database: <\/code>db). When it finds our target, it toggles a flag to start writing those lines to a new file. As soon as it encounters the start of a different database or table, it turns the flag off.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the complete script:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PHP<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nini_set('memory_limit', '2G');\n\n$inputFile = 'dump_5GB.sql';\n$outputFile = 'db_fragment.sql';\n$targetName = 'db';\n\n$in = fopen($inputFile, 'r');\n$out = fopen($outputFile, 'w');\n\n$isExtracting = false;\n\nwhile (($line = fgets($in)) !== false) {\n    if (strpos($line, '-- Current Database: `' . $targetName . '`') === 0 || \n        strpos($line, 'USE `' . $targetName . '`;') === 0) {\n        $isExtracting = true;\n    } elseif (strpos($line, '-- Current Database: `') === 0 || \n              strpos($line, 'USE `') === 0) {\n        $isExtracting = false;\n    }\n\n    if (!$isExtracting) {\n        if (strpos($line, '-- Table structure for table `' . $targetName . '`') === 0 ||\n            strpos($line, '-- Table structure for table `' . $targetName . '_') === 0) {\n            $isExtracting = true;\n        } elseif (strpos($line, '-- Table structure for table `') === 0) {\n            $isExtracting = false;\n        }\n    }\n\n    if ($isExtracting) {\n        fwrite($out, $line);\n    }\n}\n\nfclose($in);\nfclose($out);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This method keeps memory usage practically flat\u2014well under a few megabytes\u2014and processes gigabytes of SQL data in just a few seconds.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever needed to restore just one database or a specific set of tables from a multi-gigabyte (e.g., 5GB) MySQL dump? Opening a file of that size in standard text editors like VS Code or Notepad++ usually leads to extreme lag or a complete system crash. Instead of struggling with desktop software or complex [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-122","post","type-post","status-publish","format-standard","hentry","category-it-networks"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Extracting Specific Tables from Massive SQL Dumps using PHP - Lukas Wojcik - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Extracting Specific Tables from Massive SQL Dumps using PHP - Lukas Wojcik - Blog\" \/>\n<meta property=\"og:description\" content=\"Have you ever needed to restore just one database or a specific set of tables from a multi-gigabyte (e.g., 5GB) MySQL dump? Opening a file of that size in standard text editors like VS Code or Notepad++ usually leads to extreme lag or a complete system crash. Instead of struggling with desktop software or complex [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Lukas Wojcik - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-17T10:45:43+00:00\" \/>\n<meta name=\"author\" content=\"luky\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"luky\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/it-networks\\\/extracting-specific-tables-from-massive-sql-dumps-using-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/it-networks\\\/extracting-specific-tables-from-massive-sql-dumps-using-php\\\/\"},\"author\":{\"name\":\"luky\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\"},\"headline\":\"Extracting Specific Tables from Massive SQL Dumps using PHP\",\"datePublished\":\"2026-07-17T10:45:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/it-networks\\\/extracting-specific-tables-from-massive-sql-dumps-using-php\\\/\"},\"wordCount\":211,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\"},\"articleSection\":[\"IT &amp; Networks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/it-networks\\\/extracting-specific-tables-from-massive-sql-dumps-using-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/it-networks\\\/extracting-specific-tables-from-massive-sql-dumps-using-php\\\/\",\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/it-networks\\\/extracting-specific-tables-from-massive-sql-dumps-using-php\\\/\",\"name\":\"Extracting Specific Tables from Massive SQL Dumps using PHP - Lukas Wojcik - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#website\"},\"datePublished\":\"2026-07-17T10:45:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/it-networks\\\/extracting-specific-tables-from-massive-sql-dumps-using-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/it-networks\\\/extracting-specific-tables-from-massive-sql-dumps-using-php\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/it-networks\\\/extracting-specific-tables-from-massive-sql-dumps-using-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Extracting Specific Tables from Massive SQL Dumps using PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/\",\"name\":\"Lukas Wojcik - Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\",\"name\":\"luky\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\",\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\",\"contentUrl\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\",\"width\":424,\"height\":636,\"caption\":\"luky\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\"},\"sameAs\":[\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\"],\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/author\\\/luky\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Extracting Specific Tables from Massive SQL Dumps using PHP - Lukas Wojcik - Blog","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:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/","og_locale":"en_US","og_type":"article","og_title":"Extracting Specific Tables from Massive SQL Dumps using PHP - Lukas Wojcik - Blog","og_description":"Have you ever needed to restore just one database or a specific set of tables from a multi-gigabyte (e.g., 5GB) MySQL dump? Opening a file of that size in standard text editors like VS Code or Notepad++ usually leads to extreme lag or a complete system crash. Instead of struggling with desktop software or complex [&hellip;]","og_url":"https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/","og_site_name":"Lukas Wojcik - Blog","article_published_time":"2026-07-17T10:45:43+00:00","author":"luky","twitter_card":"summary_large_image","twitter_misc":{"Written by":"luky","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/#article","isPartOf":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/"},"author":{"name":"luky","@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9"},"headline":"Extracting Specific Tables from Massive SQL Dumps using PHP","datePublished":"2026-07-17T10:45:43+00:00","mainEntityOfPage":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/"},"wordCount":211,"commentCount":0,"publisher":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9"},"articleSection":["IT &amp; Networks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/","url":"https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/","name":"Extracting Specific Tables from Massive SQL Dumps using PHP - Lukas Wojcik - Blog","isPartOf":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/#website"},"datePublished":"2026-07-17T10:45:43+00:00","breadcrumb":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/it-networks\/extracting-specific-tables-from-massive-sql-dumps-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.lukaswojcik.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Extracting Specific Tables from Massive SQL Dumps using PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.lukaswojcik.com\/blog\/#website","url":"https:\/\/www.lukaswojcik.com\/blog\/","name":"Lukas Wojcik - Blog","description":"","publisher":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.lukaswojcik.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9","name":"luky","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg","url":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg","contentUrl":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg","width":424,"height":636,"caption":"luky"},"logo":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg"},"sameAs":["https:\/\/www.lukaswojcik.com\/blog"],"url":"https:\/\/www.lukaswojcik.com\/blog\/author\/luky\/"}]}},"_links":{"self":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":1,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":123,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts\/122\/revisions\/123"}],"wp:attachment":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}