当前位置: 首页>>代码示例>>PHP>>正文


PHP DbUtil::multipleInsert方法代码示例

本文整理汇总了PHP中DbUtil::multipleInsert方法的典型用法代码示例。如果您正苦于以下问题:PHP DbUtil::multipleInsert方法的具体用法?PHP DbUtil::multipleInsert怎么用?PHP DbUtil::multipleInsert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DbUtil的用法示例。


在下文中一共展示了DbUtil::multipleInsert方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: processCountryCitySummary

function processCountryCitySummary($db, $dbNames, $startTime, $endTime, $hoursElapsedSinceLastEvenYear, $urlIdPageIdMap)
{
    $geoipProvider = CityIPSpatialDatabase::getInstance('geoip');
    $countryWiseLoadTimes = $cityWiseLoadTimes = $ipCountryCityMap = $countryCodeCountryIdMap = $cityNameCityIdMap = array();
    foreach ($urlIdPageIdMap as $urlId => $pageId) {
        $sql = 'SELECT ip_address, load_time.done AS loadTime
            FROM ' . $dbNames['main'] . '.main, ' . $dbNames['main'] . '.url, ' . $dbNames['main'] . '.url_static, ' . $dbNames['main'] . '.load_time
            WHERE main.url_id = url.url_id
            AND url.url_static_id = url_static.url_id
            AND main.main_id = load_time.main_id
            AND main.log_time >= :start_time AND main.log_time <= :end_time
            AND url_static.url_id = :url_id';
        $st = $db->prepare($sql);
        $st->bindValue(':start_time', $startTime, PDO::PARAM_STR);
        $st->bindValue(':end_time', $endTime, PDO::PARAM_STR);
        $st->bindValue(':url_id', $urlId, PDO::PARAM_INT);
        $st->execute();
        while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
            $ipAddress = long2ip($row['ip_address']);
            if (!isset($ipCountryCityMap[$ipAddress])) {
                $geoipDetails = $geoipProvider->getLocationDetailsByIP($ipAddress);
                $ipCountryCityMap[$ipAddress] = array('country_code' => $geoipDetails['country_code'], 'city_name' => $geoipDetails['city_name']);
            }
            $countryCode = $ipCountryCityMap[$ipAddress]['country_code'];
            $cityName = $ipCountryCityMap[$ipAddress]['city_name'];
            if (!isset($countryCodeCountryIdMap[$countryCode])) {
                $sqlCountry = 'SELECT country_id FROM newmonk_common.country WHERE code = :code';
                $stCountry = $db->prepare($sqlCountry);
                $stCountry->bindValue(':code', $countryCode ? $countryCode : '', PDO::PARAM_STR);
                $stCountry->execute();
                $rowCountry = $stCountry->fetch(PDO::FETCH_ASSOC);
                $countryCodeCountryIdMap[$countryCode] = $rowCountry['country_id'];
                $stCountry->closeCursor();
            }
            $countryId = $countryCodeCountryIdMap[$countryCode];
            if (!isset($cityNameCityIdMap[$countryId][$cityName])) {
                $sqlCity = 'SELECT city_id
                    FROM newmonk_common.city
                    WHERE country_id = :country_id
                    AND name = :name';
                $stCity = $db->prepare($sqlCity);
                $stCity->bindValue(':country_id', $countryId, PDO::PARAM_INT);
                $stCity->bindValue(':name', $cityName ? $cityName : '', PDO::PARAM_STR);
                $stCity->execute();
                $rowCity = $stCity->fetch(PDO::FETCH_ASSOC);
                $cityNameCityIdMap[$countryId][$cityName] = $rowCity['city_id'];
                $stCity->closeCursor();
            }
            $cityId = $cityNameCityIdMap[$countryId][$cityName];
            $countryWiseLoadTimes[$urlId][$countryId][] = $row['loadTime'];
            $cityWiseLoadTimes[$urlId][$cityId][] = $row['loadTime'];
        }
        $st->closeCursor();
    }
    $countryWisePageViewsAndLoadTimes = $cityWisePageViewsAndLoadTimes = array();
    foreach ($urlIdPageIdMap as $urlId => $pageId) {
        foreach ($countryWiseLoadTimes[$urlId] as $countryId => $countryWiseLoadTime) {
            $countryWisePageViewsAndLoadTimes[$urlId][] = array('countryId' => $countryId, 'pageViews' => count($countryWiseLoadTime), 'loadTime' => round(array_sum($countryWiseLoadTime) / count($countryWiseLoadTime), 2));
        }
        foreach ($cityWiseLoadTimes[$urlId] as $cityId => $cityWiseLoadTime) {
            $cityWisePageViewsAndLoadTimes[$urlId][] = array('cityId' => $cityId, 'pageViews' => count($cityWiseLoadTime), 'loadTime' => round(array_sum($cityWiseLoadTime) / count($cityWiseLoadTime), 2));
        }
    }
    $numValuesToInsert = 0;
    $valuesToInsert = array();
    $queryPrefix = 'INSERT INTO ' . $dbNames['summary'] . '.country_summary(page_id, hours_elapsed_since_last_even_year, country_id, page_views, avg_load_time) VALUES';
    foreach ($urlIdPageIdMap as $urlId => $pageId) {
        foreach ($countryWisePageViewsAndLoadTimes[$urlId] as $countryWisePageViewsAndLoadTime) {
            if ($numValuesToInsert >= 5000) {
                DbUtil::multipleInsert($db, $queryPrefix, $valuesToInsert, 5);
                $numValuesToInsert = 0;
                $valuesToInsert = array();
            }
            ++$numValuesToInsert;
            $valuesToInsert[] = $pageId;
            $valuesToInsert[] = $hoursElapsedSinceLastEvenYear;
            $valuesToInsert[] = $countryWisePageViewsAndLoadTime['countryId'];
            $valuesToInsert[] = $countryWisePageViewsAndLoadTime['pageViews'];
            $valuesToInsert[] = $countryWisePageViewsAndLoadTime['loadTime'];
        }
    }
    DbUtil::multipleInsert($db, $queryPrefix, $valuesToInsert, 5);
    $numValuesToInsert = 0;
    $valuesToInsert = array();
    $queryPrefix = 'INSERT INTO ' . $dbNames['summary'] . '.city_summary(page_id, hours_elapsed_since_last_even_year, city_id, page_views, avg_load_time) VALUES';
    foreach ($urlIdPageIdMap as $urlId => $pageId) {
        foreach ($cityWisePageViewsAndLoadTimes[$urlId] as $cityWisePageViewsAndLoadTime) {
            if ($numValuesToInsert >= 5000) {
                DbUtil::multipleInsert($db, $queryPrefix, $valuesToInsert, 5);
                $numValuesToInsert = 0;
                $valuesToInsert = array();
            }
            ++$numValuesToInsert;
            $valuesToInsert[] = $pageId;
            $valuesToInsert[] = $hoursElapsedSinceLastEvenYear;
            $valuesToInsert[] = $cityWisePageViewsAndLoadTime['cityId'];
            $valuesToInsert[] = $cityWisePageViewsAndLoadTime['pageViews'];
            $valuesToInsert[] = $cityWisePageViewsAndLoadTime['loadTime'];
        }
    }
//.........这里部分代码省略.........
开发者ID:naukri-engineering,项目名称:NewMonk,代码行数:101,代码来源:boomSummarizeLogs.php


注:本文中的DbUtil::multipleInsert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。