[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Помогите понять.
Страницы: 1, 2
Александр Мутылев
Привет всем. Вот столкнулся с проблемой нагрузки на сервер. Написал тест. Нормально работает )). Но вот загвоздка: сервер, на котором сайт, во время тестирования чуть не упал. На сайте было чуть больше 1,5 т. посетителей.
Привожу код запросов:
function getTest()
{
$config = Core::selectCol("SELECT name AS array_key, config FROM configs");
$teacher = $_SESSION['auth']['id'];

date_default_timezone_set('Europe/Kiev');

//echo mktime(20, 25, 0, 10, 9, 2014);

$this->message['time_start'] = Core::formating_date($config['time_start']);
$this->message['time_finish'] = Core::formating_date($config['time_finish']);

if ($config['time_start'] > time()) {
$this->message['time_begin'] = strtotime($this->message['time_start']) - time();
$this->user = Core::selectRow("SELECT t.login, t.password, r.name as region, t.email, t.phone
FROM teachers as t
LEFT JOIN geo_regions as r ON t.region = r.id
WHERE t.id = '
{$teacher}'
"
);
} elseif ($config['time_finish'] < time()) {
$this->getResult($teacher);
} else {
$this->message['time_left'] = strtotime($this->message['time_finish']) - time();
$count_questions = Core::selectRow("SELECT count(id) AS count_id FROM test_questions");

if ($_GET['id'] > $count_questions['count_id'] || !isset($_GET['id']) || $_GET['id'] == NULL)
$_GET['id'] = 1;

$id = $_GET['id'];

$answers = Core::select("SELECT id, questions FROM test_answers WHERE teachers = '{$teacher}' ORDER BY questions");

$missed = array();

if (!empty($answers)) {
foreach ($answers as $answers_val) {
if ($answers_val['questions'] == $id) {
$id++;
}
$missed[] = $answers_val['questions'];
}
}


$this->missed = Core::select("SELECT id FROM test_questions WHERE id < '{$id}'");

if (!empty($this->missed)) {
foreach ($this->missed as $key => $val) {
foreach ($missed as $missed_val) {
if ($val['id'] == $missed_val) unset($this->missed[$key]);
}
}
}


if (count($answers) < $count_questions['count_id']) {
$this->questions = Core::selectRow("SELECT * FROM test_questions WHERE id = '{$id}'");

if (in_array($id, $missed) > 0 || $this->questions['id'] < 1) {
header("Location:?option=teacher&id=1");
}
}
else {
$this->message['time_result'] = "бла-бла " . $this->message['time_finish'];
}
}
}

Сохранение:
function postTeacher()
{
$teachers = (int)$_SESSION['auth']['id'];
$questions = (int)$_GET['id'] - 1;

if ($_POST['answer'] == NULL) $_POST['answer'] = 'skip';
$answers = $_POST['answer'];

unset($_POST);

$answer = Core::selectRow("SELECT id FROM test_answers WHERE teachers = '{$teachers}' AND questions = '{$questions}'");

if ($teachers > 0 && $questions > 0 && $answers != NULL && $answer == NULL) {
Core::query("INSERT INTO test_answers (teachers, questions, answers) VALUES ('$teachers', '$questions', '$answers')");
} else {
$this->alert['info'] = "бла-бла!";
}
}

Функции для работы с БД:
public function select($sql)
{
global $db;
$result = $db->query($sql);
$this->query_error($sql);
if ($result->num_rows > 0) {
$list = '';
while ($row = $result->fetch_assoc()) {
$list[] = $row;
}
return $list;
} else {
return false;
}
}


public function selectRow($sql)
{
global $db;
$result = $db->query($sql);
$this->query_error($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
return $row;
} else {
return false;
}
}


public function selectCol($sql)
{
global $db;
$result = $db->query($sql);
$this->query_error($sql);
if ($result->num_rows > 0) {
$list = '';
while ($row = $result->fetch_array()) {
$list[$row['array_key']] = $row['1'];
}
return $list;
} else {
return false;
}
}


public function query($sql)
{
global $db;
$result = $db->query($sql);
$this->query_error($sql);
if ($result) {
return $result;
} else {
return false;
}
}

Может кто-то поможет оптимизировать запросы, либо скажет в чем проблема может быть? Сервер германский (недешевый). Если нужна дополнительная информация, пишите.
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.