diff --git a/ip2country.admin.inc b/ip2country.admin.inc
index 4c60d97..a8d161c 100644
--- a/ip2country.admin.inc
+++ b/ip2country.admin.inc
@@ -195,6 +195,14 @@ function ip2country_admin_settings() {
     $message = t('Database is empty. You may fill the database by pressing the @update button.', array('@update' => t('Update')));
   }
 
+  $form['ip2country_database_update']['ip2country_updatebatch'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('Database update batch size'),
+    '#default_value' => variable_get('ip2country_updatebatch', 200),
+    '#description'   => t('The number of insert to stack before executing them.'),
+  );
+
+
   // Form to initiate manual updating of the IP-Country database.
   $form['ip2country_database_update']['ip2country_update_database'] = array(
     '#type'        => 'button',
diff --git a/ip2country.inc b/ip2country.inc
index ab1f8df..c44dea9 100644
--- a/ip2country.inc
+++ b/ip2country.inc
@@ -113,6 +113,9 @@ function ip2country_update_database($registry = 'arin') {
     unset($txt);  // Free up memory.
 
     // Loop over records.
+    //Create query
+    $insert = db_insert('ip2country_temp')->fields(array('ip_range_first', 'ip_range_last', 'ip_range_length', 'country', 'registry'));
+
     $summary_not_found = TRUE;
     foreach ($lines as $line) {
       // Trim each line for security.
@@ -150,16 +153,21 @@ function ip2country_update_database($registry = 'arin') {
       $range_length = (int) $parts[4];
 
       // Insert range into the temporary table.
-      db_insert('ip2country_temp')->fields(array(
-                'ip_range_first'  => min($ip_start, $ip_end),
-                'ip_range_last'   => max($ip_start, $ip_end),
-                'ip_range_length' => $range_length,
-                'country'         => $country_code,
-                'registry'        => $owner_registry,
+      $insert->values(array(
+          'ip_range_first'  => min($ip_start, $ip_end),
+          'ip_range_last'   => max($ip_start, $ip_end),
+          'ip_range_length' => $range_length,
+          'country'         => $country_code,
+          'registry'        => $owner_registry
         )
-      )->execute();
+      );
+
+      if($entries > 0 && $entries % variable_get('ip2country_updatebatch', 200) == 0){
+        $insert->execute();
+      }
       $entries++;
     }
+    $insert->execute();
     unset($lines);  // Free up memory.
   }

