У меня есть простой, но длинный запрос, который подсчитывает содержание результата, которое занимает около 14 секунд. сам счетчик на главной таблице занимает меньше секунды, но после многократных присоединиться к задержке слишком высока, как следоватьКак оптимизировать запрос со многими объединениями?
Select Count(Distinct visits.id) As Count_id
From visits
Left Join clients_locations ON visits.client_location_id = clients_locations.id
Left Join clients ON clients_locations.client_id = clients.id
Left Join locations ON clients_locations.location_id = locations.id
Left Join users ON visits.user_id = users.id
Left Join potentialities ON clients_locations.potentiality = potentialities.id
Left Join classes ON clients_locations.class = classes.id
Left Join professions ON clients.profession_id = professions.id
Inner Join specialties ON clients.specialty_id = specialties.id
Left Join districts ON locations.district_id = districts.id
Left Join provinces ON districts.province_id = provinces.id
Left Join locations_types ON locations.location_type_id = locations_types.id
Left Join areas ON clients_locations.area_id = areas.id
Left Join calls ON calls.visit_id = visits.id
Выход объяснить,
+---+---+---+---+---+---+---+---+---+---+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +---+---+---+---+---+---+---+---+---+---+ | 1 | SIMPLE | specialties | index | PRIMARY | specialty_name | 52 | NULL | 53 | Using index | | 1 | SIMPLE | clients | ref | PRIMARY,specialty | specialty | 4 | crm_db.specialties.id | 143 | | | 1 | SIMPLE | clients_locations | ref | PRIMARY,client_id | client_id | 4 | crm_db.clients.id | 1 | | | 1 | SIMPLE | locations | eq_ref | PRIMARY | PRIMARY | 4 | crm_db.clients_locations.location_id | 1 | | | 1 | SIMPLE | districts | eq_ref | PRIMARY | PRIMARY | 4 | crm_db.locations.district_id | 1 | Using where | | 1 | SIMPLE | visits | ref | unique_visit,client_location_id | unique_visit | 4 | crm_db.clients_locations.id | 4 | Using index | | 1 | SIMPLE | calls | ref | call_unique,visit_id | call_unique | 4 | crm_db.visits.id | 1 | Using index | +---+---+---+---+---+---+---+---+---+---+
Обновление 1 Этот запрос используется с динамикой, где оператор $sql = $sql . "Where ". $whereFilter
, но я представил его в простой форме. Так не считают ответ только eleminate стыки :)
Update 2 Вот пример динамической фильтрации
$temp = $this->province_id;
if ($temp != null) {
$whereFilter = $whereFilter . " and provinces.id In ($temp) ";
}
Но в случае запуска, который в нашем случае не где оператор
Зачем вам нужно присоединяться, если вы просто подсчитываете уникальный идентификатор во время посещений? – ebyrob
У вас, похоже, нет фильтрации, так зачем вам нужно «count (distinct)». Я имею в виду, что * есть * «внутреннее соединение» или два, посыпанные среди «join', но кажется, что объединения не нужны. –
Я использую позже этот запрос с динамическим, где оператор «Где $ whereFilter ' –