title: oracle insert into very slow
body: <p>I have the following Oracle query, that executes quickly (in a few seconds):</p>
  
  <pre><code>select contract_id_fk as ct,
         max(trip_id) as tid,
         max(consumed_mileage) as cum
  from trip
  where to_date &gt; to_date('20-12-2016','DD-MM-YYYY')
  and contract_id_fk is not null
  and vehicle_id_fk is not null
  and trip_stop_status is null
  group by contract_id_fk
  </code></pre>
  
  <p>the 'trip' table has a large number of row (more than 20 million)
  Now, I want to insert the result of this query in a table, using:</p>
  
  <pre><code>INSERT INTO lst
  select contract_id_fk as ct, 
         max(trip_id) as tid, 
         max(consumed_mileage) as cum 
  from trip
  where to_date &gt; to_date('20-12-2016','DD-MM-YYYY') 
  and contract_id_fk is not null 
  and vehicle_id_fk is not null 
  and trip_stop_status is null 
  group by contract_id_fk
  </code></pre>
  
  <p>This is extremely slow. So slow that the transaction times out (more than 30 seconds in my case). Any idea why this is so slow, and how it could be optimized?</p>