Wednesday, 21 August 2013

slow sql script optimization

slow sql script optimization

I have a query like:
select row_number() over(order by m_time desc) as row, m.m_id, m_time,
m_log_id
(case when exists(select 1 from t_approved_phonenumber where
phone_number = m.c_phone_number) then 1 else 0 end) as approved,
(case when exists(select 1 from t_log log where log.c_id < m.m_log_id
and log.phone_number = m.phone_number) then 'N' else 'Y' end) as
is_first_time
from t_message m
all the tables t_approved_phonenumber, t_message and t_log have a lot of
records. and all the IDs are primary key, phone_number column has index.
sometimes my query is very slow. Is there any way to speed it up?
thanks,

No comments:

Post a Comment