针对数百万个寄存器优化ecto更新

时间:2018-07-03 06:37:44

标签: postgresql phoenix-framework ecto

我需要优化此代码的性能。表中大约有300万个寄存器需要更新。

我将它们以1000个为单位进行批处理,这样我就不会用完内存。

我正在使用postgres。

$queryCount = $conn->prepare('
    SELECT COUNT(*) AS totalAdoptions 
    FROM adoptions_test 
    INNER JOIN pet_type ON adoptions_test.petTypeID = pet_type.petTypeID 
    INNER JOIN breeds ON adoptions_test.breedID = breeds.breedID 
    INNER JOIN users ON adoptions_test.userID = users.userID 
    INNER JOIN cities ON adoptions_test.cityID = cities.cityID 
    WHERE adoptionStatus = \'Open\' 
    AND adoptions_test.cityID = ?');
$queryCount->bindParam(1, $cityID);

if ($queryCount->execute()) {
    $adoptionCount = $queryCount->fetch();
    $response = array();
    if ($adoptionCount != null) {
        $response["error"] = false;
        $response["totalAdoptions"] = $adoptionCount["totalAdoptions"];

        $query = $conn->prepare("SELECT * FROM adoptions_test 
            INNER JOIN pet_type ON adoptions_test.petTypeID = pet_type.petTypeID 
            INNER JOIN breeds ON adoptions_test.breedID = breeds.breedID 
            INNER JOIN users ON adoptions_test.userID = users.userID 
            INNER JOIN cities ON adoptions_test.cityID = cities.cityID 
            WHERE adoptionStatus = \"Open\" 
            AND adoptions_test.cityID = ? 
            LIMIT " . $offsetNumber . ", " . $itemsPerPage);
        $query->bindParam(1, $cityID);

        if ($query->execute()) {
            $response = array();
            $response["error"] = false;
            $response["totalPages"] = ceil($adoptionCount["totalAdoptions"] / $itemsPerPage);
            $response["pageNumber"] = ceil($pageNumber);

            $adoptions = $query->fetchAll();
            if ($adoptions != null) {
                $response["adoptions"] = array();
                $refs = Array();
                foreach ($adoptions as $adoption) {
                    $tmp = array();
                    $tmp["adoptionID"] = $adoption["adoptionID"];
                    $tmp["petTypeID"] = $adoption["petTypeID"];
                    $tmp["petTypeName"] = $adoption["petTypeName"];
                    $tmp["breedID"] = $adoption["breedID"];
                    $tmp["breedName"] = $adoption["breedName"];
                    $tmp["userID"] = $adoption["userID"];
                    $tmp["userName"] = $adoption["userName"];
                    $tmp["cityID"] = $adoption["cityID"];
                    $tmp["cityName"] = $adoption["cityName"];
                    $tmp["adoptionName"] = $adoption["adoptionName"];
                    $tmp["adoptionCoverPhoto"] = $adoption["adoptionCoverPhoto"];
                    $tmp["adoptionDescription"] = $adoption["adoptionDescription"];
                    $tmp["adoptionGender"] = $adoption["adoptionGender"];
                    $tmp["adoptionTimeStamp"] = $adoption["adoptionTimeStamp"];
                    $tmp["adoptionStatus"] = $adoption["adoptionStatus"];
                    $refs[$adoption["adoptionID"]] = &$tmp;
                    array_push($response["adoptions"], $tmp);
                }

                $queryPromotedCount = $conn->prepare('SELECT adoption_promoted.adoptionID,COUNT(*) AS totalPromoted 
                    FROM adoption_promoted 
                    INNER JOIN adoptions_test ON adoption_promoted.adoptionID = adoptions_test.adoptionID 
                    WHERE adoptions_test.cityID = ?
                    AND CURDATE() BETWEEN promotedFrom AND promotedTo
                    GROUP BY adoption_promoted.adoptionID');
                $queryPromotedCount->bindParam(1, $cityID);
                if ($queryPromotedCount->execute()) {
                    $promotedCount = $queryPromotedCount->fetchAll();
                    if ($promotedCount != NULL) {
                        foreach($promotedCount as $promoted)
                        {
                            $adoptionID = $promoted["adoptionID"];
                            $refs[$adoptionID]["totalPromoted"] = $promoted["totalPromoted"];

                            $queryPromoted = $conn->prepare("SELECT * FROM adoption_promoted 
                                INNER JOIN adoptions_test ON adoption_promoted.adoptionID = adoptions_test.adoptionID 
                                INNER JOIN pet_type ON adoptions_test.petTypeID = pet_type.petTypeID 
                                INNER JOIN breeds ON adoptions_test.breedID = breeds.breedID 
                                INNER JOIN users ON adoptions_test.userID = users.userID 
                                INNER JOIN cities ON adoptions_test.cityID = cities.cityID 
                                WHERE adoptions_test.adoptionStatus = \"Open\" 
                                AND adoptions_test.cityID = ? 
                                AND adoption_promoted.adoptionID = ?
                                AND CURDATE() BETWEEN promotedFrom AND promotedTo 
                                LIMIT " . $promotedOffsetNumber . ", " . $promotedItemsPerPage);
                            $queryPromoted->bindParam(1, $cityID);
                            $queryPromoted->bindParam(2, $adoptionID);
                            if ($queryPromoted->execute()) {
                                $response["error"] = false;
                                $refs[$adoptionID]["totalPromotionPages"] = ceil($promoted["totalPromoted"] / $promotedItemsPerPage);
                                $refs[$adoptionID]["promotionPageNumber"] = ceil($pageNumber);

                                $promotionsList = $queryPromoted->fetchAll();
                                if ($promotionsList != NULL) {
                                    $refs[$adoptionID]["promotions"] = array();
                                    foreach ($promotionsList as $promotion) {
                                        $tmp = array();
                                        $tmp["promotedID"] = $promotion["promotedID"];
                                        $tmp["adoptionID"] = $promotion["adoptionID"];
                                        $tmp["optionID"] = $promotion["optionID"];
                                        $tmp["paymentID"] = $promotion["paymentID"];
                                        $tmp["promotedFrom"] = $promotion["promotedFrom"];
                                        $tmp["promotedTo"] = $promotion["promotedTo"];
                                        $tmp["promotedTimestamp"] = $promotion["promotedTimestamp"];
                                        $tmp["petTypeID"] = $promotion["petTypeID"];
                                        $tmp["petTypeName"] = $promotion["petTypeName"];
                                        $tmp["breedID"] = $promotion["breedID"];
                                        $tmp["breedName"] = $promotion["breedName"];
                                        $tmp["userID"] = $promotion["userID"];
                                        $tmp["userName"] = $promotion["userName"];
                                        $tmp["cityID"] = $promotion["cityID"];
                                        $tmp["cityName"] = $promotion["cityName"];
                                        $tmp["adoptionName"] = $promotion["adoptionName"];
                                        $tmp["adoptionCoverPhoto"] = $promotion["adoptionCoverPhoto"];
                                        $tmp["adoptionDescription"] = $promotion["adoptionDescription"];
                                        $tmp["adoptionGender"] = $promotion["adoptionGender"];
                                        $tmp["adoptionTimeStamp"] = $promotion["adoptionTimeStamp"];
                                        $tmp["adoptionStatus"] = $promotion["adoptionStatus"];
                                        array_push($refs[$adoptionID]["promotions"], $tmp);
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                $response["error"] = true;
                $response["message"] = "There are no open Adoptions available. Please try again";
            }

            /** SHOW THE RESULT **/
            $content_type = 'application/json';
            return $result->withStatus(200)
            ->withHeader('Content-type', $content_type)
            ->write(json_encode($response));
        }
    }
}

谢谢!

0 个答案:

没有答案