Subject | can samebody help me to improve a query? |
---|---|
Author | ndirondello marco |
Post date | 2011-06-08T09:11:28Z |
hi, i'm not very good with sql and i have a problem with a query.
i'm using fb 2.5 superclassic on win xp with default configuration.
i have this 2 tables
source
(
ID Bigint NOT NULL, PK
SOURCE_ID Bigint NOT NULL
SOURCE_TYPE Smallint NOT NULL
COUNT Integer NOT NULL
)
data
(
ID Bigint NOT NULL, PK
START_TIME Timestamp NOT NULL
SOURCE_ID Bigint NOT NULL,
SOURCE_TYPE Smallint NOT NULL,
NAME varchar NOTNULL,
VALUE Bigint NOT NULL
)
with the index on data START_TIME DESC and unique index on data (SOURCE_ID SOURCE_TYPE, NAME)
for every row in souce i need to check the last source.COUNT rows of data (joined on source_id and source_type) ordered by data.START_TIME desc,
if the value of this COUNT rows are all > 0 i need to select the first row daa and return it.
this is the pseudo-sql that i'm using (and that will return what i want)
ValidSource (rows that exceed the check) =
select s.SOURCE_ID, s.SOURCE_TYPE
from source s
join data d
on s.SOURCE_ID = d.SOURCE_ID
and s.SOURCE_TYPE = d.SOURCE_TYPE
where d.VALUE > 0
and d.Id in
(
select d2.Id
from data d2
where s.SOURCE_ID = d2.SOURCE_ID
and s.SOURCE_TYPE = d2.SOURCE_TYPE
order by d2.START_TIME desc
rows s.COUNT
)
group by s.SOURCE_ID, s.SOURCE_TYPE
having count(s.SOURCE_ID) = s.COUNT
LastValidValues =
for each row in ValidSource vs
select d.*
from data d
where d.SOURCE_ID = vs.SOURCE_ID
and d.SOURCE_TYPE = vs.SOURCE_TYPE
order by d.START_TIME desc
rows 1
return LastValidValues
my problem is that data table contains a lot of data so the query to get ValidSource take a lot of time (2/3 minutes) becouse there's
this constraint
and d.Id in
(
select d2.Id
from data d2
where s.SOURCE_ID = d2.SOURCE_ID
and s.SOURCE_TYPE = d2.SOURCE_TYPE
order by d2.START_TIME desc
rows s.COUNT
)
if i use
and d.ID in (same static value) the query is immediate.
i want know if samebody can help me to rewrite the first query to improve the performance.
another improvement can be to select directly the first rows in the first query to read data only once for a single couple (source_id, source_type).
thanks
i'm using fb 2.5 superclassic on win xp with default configuration.
i have this 2 tables
source
(
ID Bigint NOT NULL, PK
SOURCE_ID Bigint NOT NULL
SOURCE_TYPE Smallint NOT NULL
COUNT Integer NOT NULL
)
data
(
ID Bigint NOT NULL, PK
START_TIME Timestamp NOT NULL
SOURCE_ID Bigint NOT NULL,
SOURCE_TYPE Smallint NOT NULL,
NAME varchar NOTNULL,
VALUE Bigint NOT NULL
)
with the index on data START_TIME DESC and unique index on data (SOURCE_ID SOURCE_TYPE, NAME)
for every row in souce i need to check the last source.COUNT rows of data (joined on source_id and source_type) ordered by data.START_TIME desc,
if the value of this COUNT rows are all > 0 i need to select the first row daa and return it.
this is the pseudo-sql that i'm using (and that will return what i want)
ValidSource (rows that exceed the check) =
select s.SOURCE_ID, s.SOURCE_TYPE
from source s
join data d
on s.SOURCE_ID = d.SOURCE_ID
and s.SOURCE_TYPE = d.SOURCE_TYPE
where d.VALUE > 0
and d.Id in
(
select d2.Id
from data d2
where s.SOURCE_ID = d2.SOURCE_ID
and s.SOURCE_TYPE = d2.SOURCE_TYPE
order by d2.START_TIME desc
rows s.COUNT
)
group by s.SOURCE_ID, s.SOURCE_TYPE
having count(s.SOURCE_ID) = s.COUNT
LastValidValues =
for each row in ValidSource vs
select d.*
from data d
where d.SOURCE_ID = vs.SOURCE_ID
and d.SOURCE_TYPE = vs.SOURCE_TYPE
order by d.START_TIME desc
rows 1
return LastValidValues
my problem is that data table contains a lot of data so the query to get ValidSource take a lot of time (2/3 minutes) becouse there's
this constraint
and d.Id in
(
select d2.Id
from data d2
where s.SOURCE_ID = d2.SOURCE_ID
and s.SOURCE_TYPE = d2.SOURCE_TYPE
order by d2.START_TIME desc
rows s.COUNT
)
if i use
and d.ID in (same static value) the query is immediate.
i want know if samebody can help me to rewrite the first query to improve the performance.
another improvement can be to select directly the first rows in the first query to read data only once for a single couple (source_id, source_type).
thanks