title: Bulk insert attribute sets in Magento database
body: <p>I need to insert 30,000 attribute sets in Magento database. I tried it as follows</p>
  
  <pre><code>set_time_limit(0);
  function createAttributeSet($setName)
  {
      $entityTypeId = Mage::getModel('eav/entity')
      -&gt;setType('catalog_product')
      -&gt;getTypeId(); // 4 - Default
  
      $newSet = Mage::getModel('eav/entity_attribute_set');
      $newSet-&gt;setEntityTypeId($entityTypeId);
      $newSet-&gt;setAttributeSetName($setName);
      $newSet-&gt;save();
  
      $newSet-&gt;initFromSkeleton($entityTypeId);
      $newSet-&gt;save();
  }
  
  
  for($i=1; $i&lt;=30000; $i++)
  {
      $setName = 'rug_'.$i;
      createAttributeSet($setName);
  
  }
  </code></pre>
  
  <p>Above script works fine but is very slow, it takes 12 seconds to insert one attribute set, that means 6000 minute needed to insert 30k attribute sets, I am using Corei7.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Instead of using data models that were used in previous script I decided to use direct queries. To do so I dig entire database structure for attribute sets. By reverse engineering the Magento data models I created a new script containing direct queries. This script gives amazing performance.</p>
  
  <p>All 30,000 attribute sets have been inserted to eav_attribute_se but there is a limitation of 65535 records on eav_attribute_group table, so no groups were inserted against attributes sets having id above 8198. Hence 8198 attribute sets can be functional at a time, here is the code.</p>
  
  <pre><code>for ($i = 1; $i &lt;= 30000; $i++)
  {
      $attribute_set_name = 'rug_' . $i;
      $sql_1 = "INSERT INTO `mg_eav_attribute_set` (`entity_type_id`, `attribute_set_name`, `sort_order`) VALUES (4, '$attribute_set_name', $i)";
      $result_1 = mysql_query($sql_1);
      $attribute_set_id = mysql_insert_id();
  
  
      if ($attribute_set_id &gt; 0)
      {
          $attribute_groups = array(
              'Prices',
              'General',
              'Meta Information',
              'Images',
              'Recurring Profile',
              'Design',
              'Gift Options',
              'Selectable Options'
          );
  
          $sql_2 = "INSERT INTO `mg_eav_attribute_group` (`attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`) VALUES
                  ($attribute_set_id, 'Gift Options', 7, 0),
                  ($attribute_set_id, 'Design', 6, 0),
                  ($attribute_set_id, 'Recurring Profile', 5, 0),
                  ($attribute_set_id, 'Images', 4, 0),
                  ($attribute_set_id, 'Meta Information', 3, 0),
                  ($attribute_set_id, 'Prices', 2, 0),
                  ($attribute_set_id, 'General', 1, 1),
                  ($attribute_set_id, 'Selectable Options', 8, 0);";
          $result_2 = mysql_query($sql_2);
  
  
          $sql_3 = "SELECT `attribute_group_id`, `attribute_group_name` FROM `mg_eav_attribute_group` WHERE `attribute_set_id` = $attribute_set_id;";
          $result_3 = mysql_query($sql_3);
          while ($row = mysql_fetch_array($result_3))
          {
  
              $gid = trim($row['attribute_group_id']);
              $game = trim($row['attribute_group_name']);
  
              switch ($game)
              {
                  case 'Prices':
                      $sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES 
                  (4, $attribute_set_id, $gid, 99, 8),
                  (4, $attribute_set_id, $gid, 75, 1),
                  (4, $attribute_set_id, $gid, 76, 3),
                  (4, $attribute_set_id, $gid, 77, 4),
                  (4, $attribute_set_id, $gid, 78, 5),
                  (4, $attribute_set_id, $gid, 79, 6),
                  (4, $attribute_set_id, $gid, 90, 2),
                  (4, $attribute_set_id, $gid, 91, 7),
                  (4, $attribute_set_id, $gid, 118, 8),
                  (4, $attribute_set_id, $gid, 119, 9),
                  (4, $attribute_set_id, $gid, 120, 10),
                  (4, $attribute_set_id, $gid, 121, 11),
                  (4, $attribute_set_id, $gid, 122, 12),
                  (4, $attribute_set_id, $gid, 127, 13)";
                      mysql_query($sql4);
                      break;
  
                  case 'General':
                      $sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES 
                                  (4, $attribute_set_id, $gid, 89, 6),
                                  (4, $attribute_set_id, $gid, 98, 11),
                                  (4, $attribute_set_id, $gid, 108, 13),
                                  (4, $attribute_set_id, $gid, 110, 14),
                                  (4, $attribute_set_id, $gid, 111, 15),
                                  (4, $attribute_set_id, $gid, 112, 16),
                                  (4, $attribute_set_id, $gid, 113, 17),
                                  (4, $attribute_set_id, $gid, 114, 18),
                                  (4, $attribute_set_id, $gid, 115, 19),
                                  (4, $attribute_set_id, $gid, 116, 20),
                                  (4, $attribute_set_id, $gid, 124, 22),
                                  (4, $attribute_set_id, $gid, 125, 23),
                                  (4, $attribute_set_id, $gid, 126, 24),
                                  (4, $attribute_set_id, $gid, 128, 25),
                                  (4, $attribute_set_id, $gid, 129, 26),
                                  (4, $attribute_set_id, $gid, 130, 27),
                                  (4, $attribute_set_id, $gid, 131, 28),
                                  (4, $attribute_set_id, $gid, 132, 29),
                                  (4, $attribute_set_id, $gid, 71, 1),
                                  (4, $attribute_set_id, $gid, 72, 2),
                                  (4, $attribute_set_id, $gid, 73, 3),
                                  (4, $attribute_set_id, $gid, 74, 4),
                                  (4, $attribute_set_id, $gid, 80, 5),
                                  (4, $attribute_set_id, $gid, 93, 6),
                                  (4, $attribute_set_id, $gid, 94, 7),
                                  (4, $attribute_set_id, $gid, 96, 8),
                                  (4, $attribute_set_id, $gid, 97, 9),
                                  (4, $attribute_set_id, $gid, 102, 10),
                                  (4, $attribute_set_id, $gid, 117, 11)";
  
                      mysql_query($sql4);
                      break;
                  case 'Meta Information':
                      $sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES 
                  (4, $attribute_set_id, $gid, 82, 1),
                  (4, $attribute_set_id, $gid, 83, 2),
                  (4, $attribute_set_id, $gid, 84, 3)";
                      mysql_query($sql4);
                      break;
                  case 'Images':
                      $sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES 
                  (4, $attribute_set_id, $gid, 85, 1),
                  (4, $attribute_set_id, $gid, 86, 2),
                  (4, $attribute_set_id, $gid, 87, 3),
                  (4, $attribute_set_id, $gid, 88, 4),
                  (4, $attribute_set_id, $gid, 95, 5)";
                      mysql_query($sql4);
                      break;
                  case 'Recurring Profile':
                      $sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES 
                  (4, $attribute_set_id, $gid, 100, 1),
                  (4, $attribute_set_id, $gid, 101, 2)";
                      mysql_query($sql4);
                      break;
                  case 'Design':
                      $sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES 
                  (4, $attribute_set_id, $gid, 103, 1),
                  (4, $attribute_set_id, $gid, 104, 2),
                  (4, $attribute_set_id, $gid, 105, 3),
                  (4, $attribute_set_id, $gid, 106, 4),
                  (4, $attribute_set_id, $gid, 107, 5),
                  (4, $attribute_set_id, $gid, 109, 6)";
                      mysql_query($sql4);
                      break;
                  case 'Gift Options':
                      $sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES 
                  (4, $attribute_set_id, $gid, 123, 1)";
                      mysql_query($sql4);
                      break;
                  case 'Selectable Options':
                      $sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES 
                  (4, $attribute_set_id, $gid, 134, 1);";
                      mysql_query($sql4);
                      break;
              }
          }
      }
  }
  </code></pre>