1

我需要在 Magento 数据库中插入 30,000 个属性集。我尝试如下

set_time_limit(0);
function createAttributeSet($setName)
{
    $entityTypeId = Mage::getModel('eav/entity')
    ->setType('catalog_product')
    ->getTypeId(); // 4 - Default

    $newSet = Mage::getModel('eav/entity_attribute_set');
    $newSet->setEntityTypeId($entityTypeId);
    $newSet->setAttributeSetName($setName);
    $newSet->save();

    $newSet->initFromSkeleton($entityTypeId);
    $newSet->save();
}


for($i=1; $i<=30000; $i++)
{
    $setName = 'rug_'.$i;
    createAttributeSet($setName);

}

上面的脚本工作正常但是很慢,插入一个属性集需要 12 秒,这意味着插入 30k 个属性集需要 6000 分钟,我使用的是 Corei7。

编辑:

我决定使用直接查询,而不是使用之前脚本中使用的数据模型。为此,我为属性集挖掘了整个数据库结构。通过对 Magento 数据模型进行逆向工程,我创建了一个包含直接查询的新脚本。这个脚本提供了惊​​人的性能。

所有 30,000 个属性集都已插入到 eav_attribute_se 中,但 eav_attribute_group 表上的记录限制为 65535 条,因此没有针对 id 高于 8198 的属性集插入任何组。因此,一次可以有 8198 个属性集起作用,这里是代码。

for ($i = 1; $i <= 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 > 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;
            }
        }
    }
}
4

0 回答 0