Sql: Optimizing BETWEEN clause

前端 未结 9 749
孤街浪徒
孤街浪徒 2021-02-06 07:38

I wrote a statement that takes almost an hour to run so I am asking help so I can get to do this faster. So here we go:

I am making an inner join of two tables :

<
9条回答
  •  再見小時候
    2021-02-06 07:58

    There may be a very efficient way of writing this query if the intervals are deterministic because the query could be converted to an equi-join that would be amenable to more efficient hash joining.

    For example if the intervals are all hourly:

    ENTRY_TIME          EXIT_TIME
    2000-01-15 09:00:00 2000-01-15 09:59:59
    2000-01-15 10:00:00 2000-01-15 10:59:59
    2000-01-15 11:00:00 2000-01-15 11:59:59
    2000-01-15 12:00:00 2000-01-15 12:59:59
    ....
    

    Then the join can be written as:

    intervals.entry_time=trunc(measures.time,'HH')
    

    This would reduce the cost of everything up to and including the join pretty much to a full scan of each of the tables.

    However, since you have the ORDER BY operation in there, I think that a sort-merge might still beat it as the query is written right now because the optimiser will sort a smaller data set for the sort-merge than it would for the hash join (because in the latter case it would have to sort more columns of data). you could get round this by structuring the query as:

    select
      measures.measure     as measure,
      measures.time        as time,
      intervals.entry_time as entry_time,
      intervals.exit_time  as exit_time
    from
      intervals inner join  
      (select time, measure from measures order by time) measures
      on  intervals.entry_time=trunc(measures.time,'HH')  
    /
    

    This gives a lower cost estimate than a sort-merge on my 10.2.0.4 test instance but I'd regard it as a little risky.

    So, I'd look for a sort-merge or rewrite it to allow the use of a hash join if possible.

提交回复
热议问题