powered by simpleCommunicator - 2.0.52     © 2025 Programmizd 02
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Оптимизировать запрос
3 сообщений из 3, страница 1 из 1
Оптимизировать запрос
    #39988926
TanyaBlaginina
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
CREATE OR REPLACE FUNCTION getMessages(begindate date,
enddate date) RETURNS table("outsms" bigint, "delivsms" bigint, "outemail" bigint, "delivemail" bigint)
LANGUAGE plpgsql
AS
$body$
begin
RETURN QUERY
select outsms.cnt as "outsms",
delivsms.cnt as "delivsms",
outemail.cnt as "outemail",
delivemail.cnt as "delivemail" from

(select count(a.*) as cnt from NTF_MESSAGE a /*всего отправлено смс*/
join NTF_MSG_TYPES_LINK b on b.ATTRA=a.A_OUID
where b.ATTRb=10322418 and a.a_createdate>=begindate and a.a_createdate<=enddate ) outsms

join

(select count(a.*) as cnt from NTF_MESSAGE a /*всего доставлено смс*/
join NTF_MSG_TYPES_LINK b on b.ATTRA=a.A_OUID
join NTF_MESSAGE_LOG c on c.a_message=a.A_OUID
where b.ATTRb=10322418 and a.a_createdate>=begindate and a.a_createdate<=enddate ) delivsms on 1=1

join

(select count(a.*) as cnt from NTF_MESSAGE a /*всего отправлено email*/
join NTF_MSG_TYPES_LINK b on b.ATTRA=a.A_OUID
where b.ATTRb=10322416 and a.a_createdate>=begindate and a.a_createdate<=enddate )outemail on 1=1

join

(select count(a.*) as cnt from NTF_MESSAGE a /*всего доставлено email*/
join NTF_MSG_TYPES_LINK b on b.ATTRA=a.A_OUID
join NTF_MESSAGE_LOG c on c.a_message=a.A_OUID
where b.ATTRb=10322416 and c.a_error is null and a.a_createdate>=begindate and a.a_createdate<=enddate ) delivemail on 1=1
;

end;
$body$;

Запрос выполняется очень долго, как его оптимизировать?
...
Рейтинг: 0 / 0
Оптимизировать запрос
    #39988967
Фотография Maxim Boguk
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
TanyaBlaginina,

покажите планы запросов... покажите структуру таблиц участвующих...
по виду у вас нам EAV схема которая убогая по производительности и не лечится но может быть что то очевидное получится сделать.

--
Maxim Boguk
лучшая поддержка PostgreSQL: dataegret.ru
...
Рейтинг: 0 / 0
Оптимизировать запрос
    #39989244
Фотография court
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
TanyaBlaginina
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
CREATE OR REPLACE FUNCTION getMessages(begindate date,
  enddate date) RETURNS table("outsms" bigint, "delivsms" bigint, "outemail" bigint, "delivemail" bigint)
LANGUAGE plpgsql
AS
$body$
begin
   RETURN QUERY
   select outsms.cnt as "outsms", 
   delivsms.cnt as "delivsms",
   outemail.cnt as "outemail",
   delivemail.cnt as "delivemail" from 

   (select count(a.*) as cnt from NTF_MESSAGE a /*всего отправлено смс*/
   join NTF_MSG_TYPES_LINK b on b.ATTRA=a.A_OUID
   where b.ATTRb=10322418  and a.a_createdate>=begindate and a.a_createdate<=enddate ) outsms
   
   join

   (select count(a.*) as cnt from NTF_MESSAGE a /*всего доставлено смс*/
   join NTF_MSG_TYPES_LINK b on b.ATTRA=a.A_OUID
   join NTF_MESSAGE_LOG c on c.a_message=a.A_OUID
   where b.ATTRb=10322418  and a.a_createdate>=begindate and a.a_createdate<=enddate ) delivsms on 1=1 
   
   join

   (select count(a.*) as cnt from NTF_MESSAGE a /*всего отправлено email*/
   join NTF_MSG_TYPES_LINK b on b.ATTRA=a.A_OUID
   where b.ATTRb=10322416  and a.a_createdate>=begindate and a.a_createdate<=enddate )outemail on 1=1 
   
   join

   (select count(a.*) as cnt from NTF_MESSAGE a /*всего доставлено email*/
   join NTF_MSG_TYPES_LINK b on b.ATTRA=a.A_OUID
   join NTF_MESSAGE_LOG c on c.a_message=a.A_OUID
   where b.ATTRb=10322416 and c.a_error is null  and a.a_createdate>=begindate and a.a_createdate<=enddate ) delivemail on 1=1
; 
     
end;
$body$;



Запрос выполняется очень долго, как его оптимизировать?

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
select 
   sum(case when b.ATTRb=10322418 then 1 else 0 end) as outsms
  ,sum(case when b.ATTRb=10322418 and c.a_message is not null then 1 else 0 end) as delivsms
  ,sum(case when b.ATTRb=10322416 then 1 else 0 end) as outemail
  ,sum(case when b.ATTRb=10322416 and c.a_message is not null and c.a_error is null then 1 else 0 end) as delivemail
from NTF_MESSAGE a 
join NTF_MSG_TYPES_LINK b on b.ATTRA=a.A_OUID
left join NTF_MESSAGE_LOG c on c.a_message=a.A_OUID
where b.ATTRb in (10322416,10322418) and a.a_createdate>=begindate and a.a_createdate<=enddate
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Оптимизировать запрос
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]