0

我正在创建自己的 Wordpress 插件,将配置文件作为帖子导入,并且在脚本工作时,我希望改进插入/更新帖子的查询。

该脚本每 5 分钟运行一次,工作方式如下:

1从本地用户表中的外部源导入/更新所有配置文件。这很简单,工作正常

2 . 在更新之前将所有现有帖子自定义字段状态设置为离线

$offline = "UPDATE $meta SET meta_value='offline' WHERE meta_key='status'";

3 . 从表用户中选择所有在线的个人资料

$new_query = "SELECT * FROM users WHERE status='online' ORDER BY visitors DESC";

4 . Foreach 个人资料,如果帖子存在,则更新,否则创建新帖子

$result = $wpdb->get_results($new_query);
foreach ($result as $post){
    $cat = $post->gender.'s';
    $getterm = get_term_by('name',$cat,'user-category');
    $cat_id = $getterm->term_id;
    $custfields = array (
        'name'          => $post->name,
        'age'           => $post->age,
        'subject'       => $post->subject,
        'status'        => $post->status,
        'fans'          => $post->fans,
        'visitors'      => $post->visitors,
    );
    endif;
    $postarr = array (
        'post_title'    => $post->username,
        'post_name'     => $post->username.'-profile',
        'post_content'  => 'something',
        'post_type'     => 'users-profile',
        'post_category' =>  array($cat_id),
        'post_status'   => 'publish',       
        'meta_input'    => $custfields
    );
    $postcheck = get_page_by_title($post->username,OBJECT, 'users-profile');    

    if($postcheck):
            update_post_meta($postcheck->ID ,'subject', $post->subject );
            update_post_meta($postcheck->ID ,'status', $post->status );
            update_post_meta($postcheck->ID ,'visitors', $post->visitors );
    else :
        wp_set_object_terms(wp_insert_post( $postarr,true),$cat_id,'user-category',true);
    endif;
}
SHOW CREATE TABLE wp_users

wp_users    CREATE TABLE `wp_users` (
  `username` varchar(111) COLLATE utf8mb4_unicode_520_ci NOT NULL,
  `name` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
  `age` int(11) NOT NULL,
  `gender` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
  `subject` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
  `languages` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
  `isnew` tinyint(1) NOT NULL,
  `imagebig` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
  `fans` int(11) NOT NULL,
  `visitors` int(11) NOT NULL,
  `timeonline` int(11) NOT NULL,
  `status` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
  `location` text COLLATE utf8mb4_unicode_520_ci NOT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci  

SHOW CREATE TABLE `wp_postmeta`

wp_postmeta CREATE TABLE `wp_postmeta` (
  `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `post_id` bigint(20) unsigned NOT NULL DEFAULT 0,
  `meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `meta_value` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`meta_id`),
  KEY `post_id` (`post_id`),
  KEY `meta_key` (`meta_key`(191))
) ENGINE=InnoDB AUTO_INCREMENT=405678 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci    

SHOW CREATE TABLE `wp_posts`

wp_posts    CREATE TABLE `wp_posts` (
  `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `post_author` bigint(20) unsigned NOT NULL DEFAULT 0,
  `post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_content` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `post_title` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `post_excerpt` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `post_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'publish',
  `comment_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open',
  `ping_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open',
  `post_password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `post_name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `to_ping` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `pinged` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_content_filtered` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `post_parent` bigint(20) unsigned NOT NULL DEFAULT 0,
  `guid` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `menu_order` int(11) NOT NULL DEFAULT 0,
  `post_type` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'post',
  `post_mime_type` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `comment_count` bigint(20) NOT NULL DEFAULT 0,
  PRIMARY KEY (`ID`),
  KEY `post_name` (`post_name`(191)),
  KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
  KEY `post_parent` (`post_parent`),
  KEY `post_author` (`post_author`)
) ENGINE=InnoDB AUTO_INCREMENT=57991 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci 

我想要做的是,在更新/插入之后移动离线开关并将其应用于不在 SELECT 语句中的所有帖子......也许是进行更新/插入的更好方法。

也许是这样的:

从用户表中选择所有在线个人资料,选择与个人资料匹配的帖子(帖子标题=个人资料用户名),如果帖子存在,则更新否则创建,将表中的其余帖子切换为离线。这可能吗?

谢谢

4

1 回答 1

0

前缀索引 (KEY meta_key( meta_key(191))) 是可憎的。是的,这是由于utf8mb4. 但它正在伤害你。做这个

SELECT MAX(CHAR_LENGTH(meta_key)) from wp_postmeta;

如果返回 191 或更少,则将 meta_key 的大小更改为 191 并(191)在索引中删除。并查看http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta中的其他提示

这些提示可能是您问题的解决方案。

UPDATEing许多行中的标志似乎是处理事物的笨拙方式。有没有办法在不使用离线/在线标志的情况下简单地折叠新数据?(我想我仍然错过了意图。)如果它是一百万行,那么更新可能需要很长时间。

“所有不在 SELECT 中的帖子”——这听起来像是“多表更新”语句。这将涉及一个LEFT JOIN ON ... WHERE ... IS NULL. 你能用重要的列模拟几行吗?向我们展示这些表格中应该包含的内容的“之前”和“之后”。

您也可以将上述操作应用于post_name.

age整数(11)

你每年都在增加它吗?

于 2020-06-03T18:38:08.340 回答