sql - join two Oracle tables, date to not overlapping date ranges -
i have 2 tables:
- trips: id_trip, id_object, trip_date, delta (8980026 rows)
- ranges: id_range, id_object, date_since, date_until (18490 rows)
i need optimize following select statement
select r.id_range, sum(t.delta) sum_deltas trips t, ranges r t.id_object = r.id_object , t.trip_date between r.date_since , r.date_until group r.id_range
according condition there 1 matching row trip in 'ranges'
- the trips table growing, there no updates or deletions
- table ranges may change time time in way (deletions, updates, inserts), function based index not way :(
- there indexes on id_object (in both tables) , date_since (in trips)
does have idea how speed things up, possible?
it's possible speed things up; may not worth time / effort / money / disk-space / additional overheads etc.
firstly please use explicit join syntax. it's been sql standard few decades , helps avoid lot of potential errors. query become:
select r.id_range, sum(t.delta) sum_deltas trips t join ranges r on t.id_object = r.id_object , t.trip_date between r.date_since , r.date_until group r.id_range
this query imply need 2 indexes - unique if possible. on ranges
should have index on id_object, date_since, date_until
. index on trips
id_object, trip_date
. if trips
smaller might consider adding delta
on end of index never enter table @ index scan. stands you're going have table access index rowid.
having written problem may different. you're going full-scanning both tables query. problem might indexes. if optimizer using indexes it's possible you're doing index unique/range scan each id_object
in trips
or ranges
, then, because of use of columns not in indexes doing table access index rowid. can massively expensive.
try adding hint force full-scan of both tables:
select /*+ full(t) full(r) */ r.id_range, sum(t.delta) sum_deltas trips t join ranges r on t.id_object = r.id_object , t.trip_date between r.date_since , r.date_until group r.id_range
Comments
Post a Comment