В моем проекте есть функция, которая заполняет БД следующим образом: для каждого сотрудника создается 31 день (ну, иногда не 31, смотря сколько дней в месяце, который заполняется), дню присваиваются некоторые данные (время начала работы, продолжительность и т.д.).
Опытным путем, я выяснил, что проблема именно в "заполнении" БД "днями". Код функции:
public function actionCreateDays($month) {
for($i = 1; $i <= cal_days_in_month(CAL_GREGORIAN, $month->month, $month->year); $i++) {
$date = $month->year . '-' . sprintf("%02s", $month->month) . '-' . sprintf("%02s", $i);
$day = Calendar::model()->findByPk($date);
$type = $day->day_type;
if ($type == 'Суббота' || $type == 'Воскресенье' || $type == 'Праздник') { // НУЖНО ПОДГРУЖАТЬ ПРАЗДНИКИ!!!! (ВРОДЕ ПОДГРУЖАЮТСЯ)
$type = 'В';
} else {
$type = 'РАБ';
}
$rate_start_time = $date . ' 08:00:00';
$rate_end_time = $date . ' 17:00:00';
$rate_long_time = 8;
$rate_lunch_time = 1;
if ($month->rate == "0,5") {
$rate_start_time = $date . ' 17:15:00';
$rate_end_time = $date . ' 21:15:00';
$rate_long_time = 4;
$rate_lunch_time = 0;
} elseif ($month->rate == "0,25") {
$rate_start_time = $date . ' 21:30:00';
$rate_end_time = $date . ' 23:3.:00';
$rate_long_time = 2;
$rate_lunch_time = 0;
}
$day = new GraphDays();
$day->month_id = $month->id;
$day->day = $i;
$day->type = $type;
$day->start_time = $rate_start_time;
$day->end_time = $rate_end_time;
$day->long_time = $rate_long_time;
$day->lunch_time = $rate_lunch_time;
$day->save(false);
$day = new TimesheetDays();
$day->month_id = $month->id;
$day->day = $i;
$day->type = $type;
$day->start_time = $rate_start_time;
$day->end_time = $rate_end_time;
$day->long_time = $rate_long_time;
$day->lunch_time = $rate_lunch_time;
$day->save(false);
}
}
Принимаются любые советы по оптимизации этого ужаса =(
Дополню ответ. Вот функция создания месяца в БД, которая вызывает функцию выше (создания дней).
public function actionCreateMonth($arg_year, $arg_month) {
$depart = Departs::model()->find(array('condition' => 't.name = "' . Yii::app()->user->getId() . '"'));
$start = microtime(true);
$s = new Soap();
$all_posts = $s->getPosts();
$all_divisions = $s->getDivisions();
$all_rates = $s->getRelatedActualyPersons($depart['uid']);
$rates_time = microtime(true) - $start;
$start = microtime(true);
$cache = array();
foreach ($all_rates as $rate) {
$month = Months::model()->findByAttributes(array(
'person_post' => $all_posts[$rate['post']],
'person_uid' => $rate['uid'],
'type' => $rate['empType'],
'rate' => $rate['rate'],
'month' => $arg_month,
'year' => $arg_year
));
if ($month == null) {
$month = new Months();
$month->depart_uid = $rate['depart'];
$month->depart_name = $all_divisions[$rate['depart']]['name'];
$month->person_uid = $rate['uid'];
$month->person_tab = $rate['tabNum'];
$month->person_fio = $rate['fio'];
$month->person_post = $all_posts[$rate['post']];
$month->rate = $rate['rate'];
$month->type = $rate['empType'];
$month->year = $arg_year;
$month->month = $arg_month;
$month->graph = 8;
$month->categorie = "Служащие";
$month->save(false);
$this->actionCreateDays($month);
// Подсчет времени
if (!array_key_exists($month->rate, $cache)) {
$cache[$month->rate] = array(
"graphInfo" => $this->calculateTotal($month, 'graph'),
"tabelInfo" => $this->calculateTotal($month, 'tabel')
);
}
$time = new TotalTimeGraph();
$time->month_id = $month->id;
$time->days = $cache[$month->rate]['graphInfo']['total_days'];
$time->hours = $cache[$month->rate]['graphInfo']['total_hours'];
$time->night_hours = $cache[$month->rate]['graphInfo']['night_hours'];
$time->normal_hours = $cache[$month->rate]['graphInfo']['normal_hours'];
$time->deviation_hours = $cache[$month->rate]['graphInfo']['deviation_hours'];
$time->save(false);
$time = new TotalTimeTimesheet();
$time->month_id = $month->id;
$time->days = $cache[$month->rate]['tabelInfo']['total_days'];
$time->hours = $cache[$month->rate]['tabelInfo']['total_hours'];
$time->night_hours = $cache[$month->rate]['tabelInfo']['night_hours'];
$time->normal_hours = $cache[$month->rate]['tabelInfo']['normal_hours'];
$time->deviation_hours = $cache[$month->rate]['tabelInfo']['deviation_hours'];
$time->holiday_hours = $cache[$month->rate]['tabelInfo']['holiday_hours'];
$time->days_15 = $cache[$month->rate]['tabelInfo']['days_15'];
$time->hours_15 = $cache[$month->rate]['tabelInfo']['hours_15'];
$time->night_hours_15 = $cache[$month->rate]['tabelInfo']['night_hours_15'];
$time->holiday_hours_15 = $cache[$month->rate]['tabelInfo']['holiday_hours_15'];
$time->save(false);
}
}
$bd_time = microtime(true) - $start;
return array(
'1c_exec_time' => $rates_time,
'bd_exec_time' => $bd_time
);
}
Логика ее работы просто - создаем запись для сотрудника, вызываем функцию создания дней для только что созданной записи.
$day->save(false);
создаётся соединение новое?