SQLinfo.ru - Все о MySQL Webew.ru: теория и практика веб-технологий

Форум пользователей MySQL

Задавайте вопросы, мы ответим

Вы не зашли.

#1 12.11.2009 07:49:32

Alexandra
Участник
Зарегистрирован: 12.11.2009
Сообщений: 2

Поодскажите по поводу оптимизации запроса, пжлста...

Доброго времени суток....
Есть запрос на выборку данных...в таблице на данный момент пока только 1500 записей...запрос выглядит так:


SELECT DISTINCT
spr_otdelenie.otdelenie_name,
(select count(*) from  nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-01-01 00:00:00')) and (concat(p_year,'-01-31 23:59:59'))) ) as  january,

(select count(*) from  nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-02-01 00:00:00')) and (concat(p_year,'-02-29 23:59:59'))) ) as  february,

(select count(*) from  nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-03-01 00:00:00')) and (concat(p_year,'-03-31 23:59:59'))) ) as  march,

(select count(*) from nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-04-01 00:00:00')) and (concat(p_year,'-04-30 23:59:59'))) ) as  april,

(select count(*)  from nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-05-01 00:00:00')) and (concat(p_year,'-05-31 23:59:59'))) ) as  may,

(select count(*) from nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-06-01 00:00:00')) and (concat(p_year,'-06-30 23:59:59'))) ) as  june,

(select count(*) from nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-07-01 00:00:00')) and (concat(p_year,'-07-31 23:59:59'))) ) as  july,

(select count(*) from nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-08-01 00:00:00')) and (concat(p_year,'-08-31 23:59:59'))) ) as  august,

(select count(*) from nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-09-01 00:00:00')) and (concat(p_year,'-09-30 23:59:59'))) ) as  september,

(select count(*)  from nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-10-01 00:00:00')) and (concat(p_year,'-10-31 23:59:59'))) ) as  october,

(select count(*)  from nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-11-01 00:00:00')) and (concat(p_year,'-11-30 23:59:59'))) ) as  november,

(select count(*) from nakladnie_comp
where (nakladnie_comp.status=1) and (nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie) and (nakladnie_comp.date_reg_status between  (concat(p_year,'-12-01 00:00:00')) and (concat(p_year,'-12-31 23:59:59'))) ) as  december

from  nakladnie_comp  inner Join spr_otdelenie ON nakladnie_comp.spr_otdelenie_id = spr_otdelenie.id_spr_otdelenie;



выполняется запрос 6 сек.....скажите это нормально...или можно оптимизировать чтобы выполнялся быстрее????

заранее спасибо ))

Неактивен

 

#2 12.11.2009 16:51:37

paulus
Администратор
MySQL Authorized Developer and DBA
Зарегистрирован: 22.01.2007
Сообщений: 6756

Re: Поодскажите по поводу оптимизации запроса, пжлста...

Кажется, это вообще можно урезать до

SELECT o.otdelenie_name, MONTH(n.date_reg_status), COUNT(*)
FROM spr_otdelenie o JOIN nakladnie_comp n ON n.spr_otdelenie_id = o.id_spr_otdelenie
GROUP BY 2

Ну и индексы нужны на n.spr_otdelenie_id и o.id_spr_otdelenie

Неактивен

 

#3 13.11.2009 06:40:28

Alexandra
Участник
Зарегистрирован: 12.11.2009
Сообщений: 2

Re: Поодскажите по поводу оптимизации запроса, пжлста...

Всем спасибо, я уже решила эту проблему ))))

Неактивен

 

Board footer

Работает на PunBB
© Copyright 2002–2008 Rickard Andersson