简体   繁体   English

大量数据导入,Wordpress PHP和mysql

[英]Huge Data Import, Wordpress php & mysql

I have 2 databases on 2 different servers. 我在2个不同的服务器上有2个数据库。 The source database is a massive real estate database and the second is a Wordpress database. 源数据库是一个庞大的房地产数据库,第二个是Wordpress数据库。 I have to import the data from the source to the Wordpress database's posts and postmeta tables. 我必须将数据从源导入到Wordpress数据库的发布和postmeta表中。

Currently, I'm pulling everything out of the source db and then looping through those results to insert them into the posts table with another nested query to insert each field as a metavalue using the insert id from the post insert. 当前,我将所有内容从源数据库中拉出,然后遍历这些结果,以使用另一个嵌套查询将它们插入到posts表中,以使用来自post插入的插入ID将每个字段作为元值插入。 In other words, it's a memory hog. 换句话说,这是一个记忆猪。

My question is, can the loop I have below be improved so that there aren't so many individual insert lines? 我的问题是,下面的循环是否可以进行改进,以使单个插入行没有那么多? Or does anyone have any suggestions for making this faster / not as sloppy? 还是有人对提高速度/不那么草率有任何建议?

// SOURCE DB
$query = "select $fields from listings where data_id = 'B'"; 

$result = mysql_query($query);

// WORDPRESS DB
while ($row = mysql_fetch_assoc($result)) {
    $query2 = "insert into wp_posts (post_author, post_content, post_title, post_status, comment_status, ping_status, post_name, post_type) values";
    $query2 .= " ('1', '" . mysql_real_escape_string($row['remarks']) . "', '{$row['mls_acct']}', 'publish', 'closed', 'closed', '{$row['mls_acct']}', 'properties')";

    $result2 = mysql_query($query2);

    $id = mysql_insert_id();

    foreach ($row as $key => $val)
    {
        $query3 = "insert into wp_postmeta (post_id, meta_key, meta_value) values ";

        $query3 .= "('$id', '$key', 'mysql_real_escape_string($val)')";

        $result3 = mysql_query($query3);
    }
}

You could probably do an INSERT ... SELECT FROM ... as a single query rather than the looped version you're doing internally: 您可能可以将INSERT ... SELECT FROM ...作为单个查询而不是内部执行的循环版本进行:

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
    SELECT $id, meta_key, meta_value FROM wp_posts WHERE id = $id

I'm not particularly familiar with WP's internals (and I really really don't ever want to become so...), but if you can tweak this to work you'll save yourself a few dozen separate insert queries inside the loop. 我对WP的内部并不特别熟悉(我真的真的不想成为这样...),但是如果您可以对此进行调整,您将在循环内节省几十个单独的插入查询。

The first thought I have on this is to insert the records in batches rather than one at a time. 我对此的第一个想法是批量插入记录,而不是一次插入。 So your inner insert would end up being something like: 因此,您的内部插入最终将变为:

insert into wp_postmeta (post_id, meta_key, meta_value) values
(1, meta_key_1, meta_value_1),
(1, meta_key_2, meta_value_2),
(1, meta_key_3, meta_value_3),
(1, meta_key_4, meta_value_4),
(1, meta_key_5, meta_value_5),
(1, meta_key_6, meta_value_6),
(1, meta_key_7, meta_value_7),
...
(1, meta_key_100, meta_value_100);

I'm not sure what's the optimum number of records per batch, but I think you get the idea. 我不确定每批的最佳记录数是多少,但是我想您会明白的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM