Subject | Why is SQL slow? |
---|---|
Author | OB1 |
Post date | 2015-07-02T16:22:05Z |
Hello
Could someone give me some pointers to improve the speed of this SQL please. It's taking about 10 minutes to execute on a fast PC using Firebird v1.56.
I am counting the number of messages sent by different methods, for a particular company. A company has a number of contacts and they receive a number of calls during the month. These calls can generate a number of messages to be sent out.
This has been working fine for many years, but it appears to execute VERY slowly when a Company has many contacts ~800
select m.MSG_TYPE, count (distinct(m.Message_ID)) as MsgCount
from MESSAGES m
join MESSAGE_LINK ml
on ml.message_id = m.message_id
join CALLS a
on a.Call_id = ml.call_id
join CONTACTS c
on c.CONTACT_ID = a.CONTACT_ID
where c.company_id=240 AND
(m.TIMESTAMP_ADDED >= '2015.06.01' AND m.TIMESTAMP_ADDED < '2015.06.30' ) AND
m.TX_Code='O' AND
group by m.MSG_TYPE
Here's the plan:
PLAN SORT (JOIN (C INDEX (I_CONTACTS_A),M INDEX (MESSAGES_TIMESTAMP_ADDED_A),ML INDEX (MESSAGE_LINK_MESSAGE_ID_A),A INDEX (PK_CALLS)))
Many thanks
David
Could someone give me some pointers to improve the speed of this SQL please. It's taking about 10 minutes to execute on a fast PC using Firebird v1.56.
I am counting the number of messages sent by different methods, for a particular company. A company has a number of contacts and they receive a number of calls during the month. These calls can generate a number of messages to be sent out.
This has been working fine for many years, but it appears to execute VERY slowly when a Company has many contacts ~800
select m.MSG_TYPE, count (distinct(m.Message_ID)) as MsgCount
from MESSAGES m
join MESSAGE_LINK ml
on ml.message_id = m.message_id
join CALLS a
on a.Call_id = ml.call_id
join CONTACTS c
on c.CONTACT_ID = a.CONTACT_ID
where c.company_id=240 AND
(m.TIMESTAMP_ADDED >= '2015.06.01' AND m.TIMESTAMP_ADDED < '2015.06.30' ) AND
m.TX_Code='O' AND
group by m.MSG_TYPE
Here's the plan:
PLAN SORT (JOIN (C INDEX (I_CONTACTS_A),M INDEX (MESSAGES_TIMESTAMP_ADDED_A),ML INDEX (MESSAGE_LINK_MESSAGE_ID_A),A INDEX (PK_CALLS)))
Many thanks
David