批量插入EAV表示例

时间:2013-06-13 20:41:23

标签: php mysql insert bulkinsert entity-attribute-value

请帮我将数据插入此EAV实施中。

我完全了解EAV表存在的缺点,性能上的缺点以及维护sql查询的难度,但是当前系统已经使用它并且不能更改它。

系统使用EAV表来存储来自各种来源的临时元数据,这些元素“填写”实体的属性和值,当实体完成后,它将被导出到适当的表中。

我很乐意为此使用NoSQL数据库,但正如我已经提到的,更改现有系统不是一种选择:(

但我实际上问的是,如果有任何关于如何将属性插入到EAV表中的示例,我在网上找不到任何内容(我看到的都是有关邪恶EAV是多么的文章,或者说EAV是如果使用得当,或者有关于Magento的文章,请不要恶意。)

我将举一个简单的例子,你可以复制&粘贴我试图如何插入这些表格。

注意:数据大多是相同的字符串,但是对于每个“实体”都有许多不同的组合,它也使用php来实际插入

注2:问题变得有趣,因为要保存的信息量通常非常大,插入必须高效。

准备好要复制的代码:

<?php
try {
    $user = 'root';
    $pass = '000000';
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
    exit('Failed to open database: '.$e->getMessage() );
}

#the entity
$dbh->exec('
CREATE TEMPORARY TABLE IF NOT EXISTS `tbl_entity` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100),
`created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
');

#the value "type" or attribute
$dbh->exec('
CREATE TEMPORARY TABLE IF NOT EXISTS `tbl_attribute` (
`attr_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) UNIQUE,
`type` TINYINT(3),
PRIMARY KEY(`attr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
');

#the table that holds the values, there is a "mostly" limited set of values and storing each one again and again makes no sense.
$dbh->exec('
CREATE TEMPORARY TABLE IF NOT EXISTS `tbl_value` (
`val_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`value` VARCHAR(255) UNIQUE,
PRIMARY KEY(`val_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
');

#the table that holds the references of the values and their attributes
$dbh->exec('
CREATE TEMPORARY TABLE IF NOT EXISTS `tbl_ref` (
`ent_id` INT(11) UNSIGNED NOT NULL,
`attr_id` INT(11) UNSIGNED NOT NULL,
`val_id` INT(11) UNSIGNED NOT NULL,
PRIMARY KEY(`ent_id`,`attr_id`,`val_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
');

$input_rows = array(
    array(
        'item'=>'Item 1',
        'attr'=>'title',
        'val'=>'Item 1 title',
    ),
    array(
        'item'=>'Item 1',
        'attr'=>'code',
        'val'=>'983474',
    ),
    array(
        'item'=>'Item 1',
        'attr'=>'tag',
        'val'=>'Tag A',
    ),
    array(
        'item'=>'Item 1',
        'attr'=>'tag',
        'val'=>'Tag B',
    ),
    array(
        'item'=>'Item 2',
        'attr'=>'tag',
        'val'=>'Tag B',
    ),
    //... and so forth
);

//Build queries for values and attributes
$attr_values = array();
$attr_params = array();
$val_values = array();
$val_params = array();
foreach($input_rows as $row){
    $attr_values[] = '?';
    $attr_params[] = $row['attr'];
    $val_values[] = '?';
    $val_params[] = $row['val'];
}

//Insert the attributes and values, many might already exist in the db
if(!empty($attr_values)){
    $sql = 'INSERT IGNORE INTO `tbl_attribute` (`name`) VALUES ('.implode('),(',$attr_values).')';
    $stmt = $dbh->prepare($sql);
    $stmt->execute($attr_params);
}
if(!empty($val_values)){
    $sql = 'INSERT IGNORE INTO `tbl_value` (`value`) VALUES ('.implode('),(',$val_values).')';
    $stmt = $dbh->prepare($sql);
    $stmt->execute($val_params);
}

//Select the ID's of the values and attributes
$attr_ids = array();
$stmt = $dbh->prepare('SELECT `attr_id`,`name` FROM `tbl_attribute` WHERE `name` IN('.implode(',',$attr_values).')');
$stmt->execute($attr_params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $attr_ids[$row['name']] = $row['attr_id'];
}
$val_ids = array();
$stmt = $dbh->prepare('SELECT `val_id`,`value` FROM `tbl_value` WHERE `value` IN('.implode(',',$val_values).')');
$stmt->execute($val_params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $val_ids[$row['value']] = $row['val_id'];
}

//Insert the entities and references
$dbh->beginTransaction();
foreach($input_rows as $row){
    //One of my gripes with this approach is that I have to "replace into" instead of "insert into" so that the lastInsertId returns an id even if there is some constraint
    $sql = 'REPLACE INTO `tbl_entity` (`name`) VALUES (?)';
    $stmt = $dbh->prepare($sql);
    $stmt->execute(array($row['item']));
    $id = $dbh->lastInsertId();

    $sql = 'INSERT IGNORE INTO `tbl_ref` (`ent_id`,`attr_id`,`val_id`) VALUES (?,?,?)';
    $stmt = $dbh->prepare($sql);
    $val_id = $val_ids[$row['val']];
    $attr_id = $attr_ids[$row['attr']];
    $stmt->execute(array($id,$attr_id,$val_id));
}
$dbh->commit();

//Select data (selecting all columns only for this example)
$data = array();
$stmt = $dbh->prepare('SELECT `id`,`name`,`created` FROM `tbl_entity`');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $data[$row['id']] = $row;
}
$stmt = $dbh->prepare('
SELECT 
x.`ent_id` AS `item_id`,
a.`name` AS `attr`,
v.`value` AS `val`
FROM `tbl_ref` x
INNER JOIN `tbl_attribute` a USING(`attr_id`)
INNER JOIN `tbl_value` v USING(`val_id`)
');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $data[$row['item_id']]['meta'][$row['attr']][] = $row['val'];
}
echo '<plaintext>';
print_r($data);

如果您发现任何其他问题,请尽可能提及。 我也很高兴听到你对如何妥善解决这个问题的看法,即使我现在无法实现它,它可能对我或其他人有用。

P.S。我不确定这是否更适合stackexchange或stackoverflow的代码审查分支,我现在是50%/ 50%所以发布在这里因为有更多的人,如果我错了,那么我该如何移动/迁移这个问题呢? / p>

P.P.S。我花了很多时间研究这个问题,3个小时就写了这个问题,请回答!

1 个答案:

答案 0 :(得分:0)

通常(咳嗽),EAV表通常看起来更像这样。

create table eav (
  entity varchar(100) not null,
  attribute varchar(100) not null,
  value varchar(255) not null,
  primary key (entity, attribute, value)
);

这种结构有助于简单有效地使用MySQL's bulk loader。我希望输入是一个CSV文件。

"Item 1", "title", "Item 1 title"
"Item 1", "code", "983474"
"Item 1", "tag", "Tag A"
"Item 1", "tag", "Tag B"
"Item 2", "tag", "Tag B"

但是使用SQL语句插入数据也同样简单。

insert into eav values ('Item 1', 'title', 'Item 1 title');
insert into eav values ('Item 1', 'code', '983474');
...

如果您要为所有这些值使用代理ID号,那么您的工作就会困难得多,而且效率会低很多。你只有几个选择。

  • 在导入之前将值转换为ID号。
  • 将其导入临时表,确定ID号,然后插入数据。