如何在php中实现多线程查询循环中的分片表?

如何在php中实现多线程查询循环中的分片表?,php,mysql,multithreading,parallel-processing,sharding,Php,Mysql,Multithreading,Parallel Processing,Sharding,//查询以在一个进程中从一个表中获取所有用户,并且不应该花费超过30秒的时间。我还可以在循环内部查询以从不同的表中获取用户的附加数据 for($shard_id = $start_index; $shard_id <= $end_index; $shard_id++) { list($db, $sharded_table) = DbConfig::getInstance() ->getConnectionByShardId($s

//查询以在一个进程中从一个表中获取所有用户,并且不应该花费超过30秒的时间。我还可以在循环内部查询以从不同的表中获取用户的附加数据

for($shard_id = $start_index; $shard_id <= $end_index; $shard_id++) {
            list($db, $sharded_table) = DbConfig::getInstance()
                ->getConnectionByShardId($shard_id, $shard_table);
            $query = "SELECT user_id,login_id,
                  first_name, middle_name, last_name, gender,
                  title,profile_image_url,
                  registered_user_type,properties
               FROM $sharded_table WHERE $where_clause";
            $st = $db->prepare($query);
            $ret = $st->execute();
            $data = $st->fetchAll(PDO::FETCH_ASSOC);
            foreach($data as $d){
                $rawData[] = $d;
            }
        }


// now i want to iterate each user to get additional properties from different tables containing user Ids
foreach($dataSet as $user){
    $temp = array();
    $properties = json_decode($user['properties'], true);
    $temp['first_name'] = CommonUtil::fetch($user,'first_name','');
    $temp['middle_name'] = CommonUtil::fetch($user, 'middle_name', '');
    $temp['last_name'] = CommonUtil::fetch($user, 'last_name', '');

    $temp['login_id'] = CommonUtil::fetch($user,'login_id','');
    $temp['user_id'] = $user['user_id'];
    $temp['enroll_grade'] = isset($properties['enroll_grade']) && !empty($properties['enroll_grade']) ? $properties['enroll_grade'] : "-";
    $temp['session'] = isset`enter code here`($properties['session']) && !empty($properties['session']) ? $properties['session'] : "2012-2013";
    $temp['admission_session'] = isset($properties['admission_session']) && !empty($properties['admission_session']) ? $properties['admission_session'] : "2012-2013";
    $userRelationShip =getUserRelationships($user['user_id']);
    if(!empty($userRelationShip)){
        $userRelationShips[]=$user['user_id'];
    }
    $temp['user_relationship']=json_encode($userRelationShip);
    $userProperties=getUserProperties($user['user_id']);
    $temp['user_relationship']=json_encode($userProperties);
    $userTemp[]=$temp;
}

PHP不支持多线程,所以您将无法实现您想要做的事情(除非您想尝试,但我不推荐)


不过,为了加快处理速度,可以插入缓存层,以避免直接从数据库查询数据。这似乎是提高性能的最可靠和可扩展的方法。

您好。很抱歉,我没有理解要点:-(这对我来说似乎太复杂了,特别是对于具有SELECT stmt的for循环,恐怕这是导致您“整个过程需要花费大量时间来准备整个数据”的主要原因。请尝试执行此查询一次。祝您好运!实际上,我需要将所有数据导入mongoDB。但我必须这样做,以提取包含用户信息的所有分片表中的所有数据,并将其插入mongoDB。无论如何,请注意用
$userProperties
替换用户关系数组项的行。备份--真正的问题是什么?有些东西运行得不够“快”?什么?可能有一种与多线程或切分无关的加速方法。表有多大?让我们看看
SHOW CREATE table
。这个“切分”的机器有多少台?(或者你正在切分其他一些东西?)我被
$shard\u table
和简单的
用户配置文件
搞糊涂了。这个表似乎没有被切分?哦,也许你误用了“切分”这个词?这只是一个示例碎片表名称计算运行时,让我们假设用户\u配置文件\u 1、用户\u配置文件\u 2、用户\u配置文件\u 3等碎片id取决于碎片键,让我们假设表是否由用户\u id进行碎片,所以算法采用碎片id的模并将数据插入其中。
CREATE TABLE `user_profiles` (
  `user_id` bigint(20) unsigned NOT NULL,
  `first_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL COMMENT 'first name',
  `middle_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'middle name',
  `last_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL COMMENT 'last name',
  `login_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL COMMENT 'globally unique email address or name',
  `password_hash` varchar(255) NOT NULL COMMENT 'encrypted password',
  `gender` enum('male','female') DEFAULT NULL COMMENT 'gender of the user (male or female)',
  `title` enum('mr','ms','mrs','dr') DEFAULT NULL COMMENT 'title of the user (mr, ms, mrs, dr etc)',
  `dob` date DEFAULT NULL COMMENT 'Date of Birth',
  `secret_code` varchar(6) NOT NULL COMMENT 'secret code used to connect users',
  `default_calendar_id` bigint(20) unsigned DEFAULT NULL COMMENT 'default calendar id',
  `default_folder_id` bigint(20) unsigned DEFAULT NULL COMMENT 'default folder id',
  `default_album_id` bigint(20) unsigned DEFAULT NULL COMMENT 'default album id',
  `profile_album_id` bigint(20) unsigned DEFAULT NULL COMMENT 'profile album id',
  `profile_photo_id` bigint(20) unsigned DEFAULT NULL COMMENT 'profile photo id as stored in the profile album',
  `profile_image_url` varchar(1024) DEFAULT NULL COMMENT 'user profile image url',
  `registered_user_type` enum('teacher','parent','student') DEFAULT NULL COMMENT 'registered user type as',
  `referrer_user_id` bigint(20) unsigned DEFAULT NULL COMMENT 'user who referred the current user to BY',
  `notification_preference` varchar(2048) DEFAULT NULL COMMENT 'notification preference, JSON array of various notifications that the current user is configured to be notified',
  `privacy_preference` varchar(2048) DEFAULT NULL COMMENT 'privacy preference, JSON array of various privacy preferences that the current user has configured',
  `properties` varchar(8192) DEFAULT NULL COMMENT 'user specific properties, JSON name-value pairs used to manage user experience etc',
  `app_properties` varchar(8192) DEFAULT NULL COMMENT 'application user properties',
  `phone_info` varchar(2048) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'phone information JSON array of (number, provider, activation_code, verified_ts). The first number is primary contact.',
  `phone_info_updated_ts` timestamp NULL DEFAULT NULL COMMENT 'last phone info updated timestamp',
  `notification_preference_updated_ts` timestamp NULL DEFAULT NULL COMMENT 'last notification preference updated timestamp',
  `privacy_preference_updated_ts` timestamp NULL DEFAULT NULL COMMENT 'last privacy preference updated timestamp',
  `password_updated_ts` timestamp NULL DEFAULT NULL COMMENT 'last password updated timestamp',
  `profile_updated_ts` timestamp NULL DEFAULT NULL COMMENT 'last profile updated timestamp',
  `secret_code_updated_ts` timestamp NULL DEFAULT NULL COMMENT 'last secret code updated timestamp',
  `status` varchar(25) NOT NULL DEFAULT 'pending' COMMENT 'user status - ''pending'',''active'',''deleted''',
  `created_ts` timestamp NULL DEFAULT NULL COMMENT 'created timestamp of the user',
  `approved_ts` timestamp NULL DEFAULT NULL COMMENT 'approved timestamp of the user',
  `updated_ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'row updated timestamp',
  `school_id` bigint(20) unsigned DEFAULT NULL COMMENT 'School Id for the relavent user',
  `organization_id` bigint(20) unsigned DEFAULT NULL COMMENT 'Organization Id for the relavent school',
  PRIMARY KEY (`user_id`),
  KEY `status_index` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='a BY user profile' |