2017-05-05 60 views
0

//查詢在一個進程中從一個表中獲取所有用戶,並且它不應該花費超過30秒,我還希望在循環內部進行查詢以獲取來自不同表的用戶的額外數據。如何實現多線程查詢循環在PHP中的分片表?

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; 
} 

//整個過程花費大量時間準備整個數據。目的是將其插入裏面的MongoDB,使執行速度快

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' | 
+0

但是你要這樣做,要小心你用'$ userProperties'替換user_relationship數組入口的行。 – YvesLeBorg

+0

備份 - 真正的問題是什麼?有些東西沒有「足夠快」地運行?什麼?可能有一種方法可以加速與多線程或分片無關的事情。 –

+0

桌子有多大?我們來看看SHOW CREATE TABLE。 –

回答

1

PHP不支持多線程,所以你將無法達到你想要做的(除非你想用pthreads嘗試什麼,但我不會推薦)。

爲了加速這個過程,你可以插入一個緩存層來避免直接從數據庫中查詢數據。這似乎是提高性能的最強大和可擴展的方式。

+0

你好。對不起,我沒有明白這一點:-(對我來說這太複雜了,特別是你有一個SELECT語句的for循環,恐怕這是你的主要原因「整個過程花費了很多時間準備好整個數據「,試着執行一次這個查詢,祝你好運! –

+0

實際上我想導入所有數據到mongoDB多數民衆贊成在需要。 – user3647491

+0

但我必須這樣做,以提取所有包含用戶信息的分片表中的所有數據並插入它進入mongoDB。 – user3647491