Query is taking lot of time with not in clause
Beloq mentioned query is taking huge amount of time with not in clause.Without this it is taking significantly low time.How should i improve on this.
set @clinicid:=3;
set @location := (select entity_id from hlclocations where locsiteid=@clinicid and loctypelviid=5);
select
s.sitedesc,
hlcitems.itemcode,
hlcitems.itemdesc,
0 TranIn,
0 TranOut,
0 Sales,
0 Consumption,
0 Adjustment
,hlcinventory.invtotalqty closing
,hlcinventory.invtotalqty opening
from hlcinventory
inner join ( select hlcinventory.invitemid invitemid,
Max(hlcinventory.invcreatedat) MaxDate from hlcinventory
where (hlcinventory.invlocid = @location and invdcnid = @clinicid and
(hlcinventory.invcreatedat < '2009-07-01'))
group by hlcinventory.invitemid ) Q1 on (hlcinventory.invcreatedat=Q1.MaxDate and hlcinventory.invitemid=Q1.invitemid)
inner join hlcitems on (hlcinventory.invitemid = hlcitems.entity_id)
inner join hlcsites s on s.entity_id=@clinicid
where hlcitems.entity_id not in (select group_concat(invitemid) from hlcinventory where
invcreatedat between '2009-07-01 00:00:00' and '2009-07-05 23:59:59'
and invlocid = @location and invdcnid = @clinicid)
and invlocid = @location and invdcnid = @clinicid
group by itemcode
order by itemcode;