Home » RDBMS Server » Performance Tuning » Optimize MERGE statement (Oracle 10g)
Optimize MERGE statement [message #525789] Wed, 05 October 2011 06:19 Go to next message
tsteinmaurer
Messages: 12
Registered: October 2008
Junior Member
Hello,

I have two tables in an Oracle 10g database which should be merged on a daily base. Currently, the destination table has ~70 mio. records and the source table has ~5 mio records. The source table is some kind of staging table and gets truncated after the successful merge. The ON clause looks like that:

  ON (
    dst.date_year = src.date_year
    and dst.date_month = src.date_month
    and dst.date_day = src.date_day
    and dst.datalogger_id = src.datalogger_id
    and dst.device_id = src.device_id
    and dst.channel_name = src.channel_name
  )


In both tables, there is an compound UNIQUE constraint on the entire field set used in the ON criteria. The execution plan shows a FULL TABLE SCAN on both sides, the source and the destination table. I guess the source table is OK, because the MERGE statement needs to process the entire source table anyway, but for the destination table, the index might be helpful.

For both tables, statistics have been re-calculated by running ANALYZE TABLE xyz COMPUTE STATISTICS.

Still, a FULL TABLE SCAN on both tables.

Is this expected?

Thanks!

Thomas
Re: Optimize MERGE statement [message #525791 is a reply to message #525789] Wed, 05 October 2011 06:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
For any peformances question, please read http://www.orafaq.com/forum/mv/msg/84315/433888/102589/#msg_433888 and post the required information.

NEVER use ANALYZE TABLE to gather statistics.
Urgently run ANALYZE TABLE ... DELETE STATISTICS and then execute DBMS_STATS.GATHER_TABLE_STATISTICS.

Regards
Michel

[Updated on: Wed, 05 October 2011 06:36]

Report message to a moderator

Re: Optimize MERGE statement [message #525798 is a reply to message #525791] Wed, 05 October 2011 07:24 Go to previous messageGo to next message
muralikri
Messages: 638
Registered: August 2011
Location: chennai
Senior Member

Can you post the MERGE statement.
Re: Optimize MERGE statement [message #525799 is a reply to message #525798] Wed, 05 October 2011 07:30 Go to previous messageGo to next message
tsteinmaurer
Messages: 12
Registered: October 2008
Junior Member
Sure:
  MERGE INTO aggregates_day dst
  USING (
    select
      date_year,
      date_month,
      date_day,
      datalogger_id,
      device_id,
      channel_name,
      value_count,
      value_sum,
      value_avg,
      value_min,
      value_max,
      is_merged
    from
      aggregates_day_stage
  ) src
  ON (
    dst.date_year = src.date_year
    and dst.date_month = src.date_month
    and dst.date_day = src.date_day
    and dst.datalogger_id = src.datalogger_id
    and dst.device_id = src.device_id
    and dst.channel_name = src.channel_name
  )
  WHEN MATCHED THEN
  UPDATE SET
    dst.value_count = src.value_count
    , dst.value_sum = src.value_sum
    , dst.value_avg = src.value_avg
    , dst.value_min = src.value_min
    , dst.value_max = src.value_max
    , dst.updated_timestamp = sysdate
  WHEN NOT MATCHED THEN
  INSERT (
    dst.date_year
    , dst.date_month
    , dst.date_day
    , dst.datalogger_id
    , dst.device_id
    , dst.channel_name
    , dst.value_count
    , dst.value_sum
    , dst.value_avg
    , dst.value_min
    , dst.value_max
    , dst.aggregates_day_id
  ) VALUES (
    src.date_year
    , src.date_month
    , src.date_day
    , src.datalogger_id
    , src.device_id
    , src.channel_name
    , src.value_count
    , src.value_sum
    , src.value_avg
    , src.value_min
    , src.value_max
    , seq_aggregates_day_id.nextval 
  );


Thanks,
Thomas
Re: Optimize MERGE statement [message #525800 is a reply to message #525799] Wed, 05 October 2011 07:48 Go to previous messageGo to next message
muralikri
Messages: 638
Registered: August 2011
Location: chennai
Senior Member

Create the GTT table as like source table with optiont "ON COMMIT DELETE ROWS"
then loop the stg table and insert the records into GTT table then use the merge,,,it will run fast.
Re: Optimize MERGE statement [message #525804 is a reply to message #525800] Wed, 05 October 2011 08:37 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Prove it! Explain the rationale.

Still silly, incomplete or wrong answers about 65 out of 70 of posts!

Regards
Michel
Re: Optimize MERGE statement [message #525816 is a reply to message #525804] Wed, 05 October 2011 10:34 Go to previous messageGo to next message
orsoghu
Messages: 46
Registered: May 2011
Location: Venice, Italy
Member
Michel, why did you say
Quote:
NEVER use ANALYZE TABLE to gather statistics.
Urgently run ANALYZE TABLE ... DELETE STATISTICS and then execute DBMS_STATS.GATHER_TABLE_STATISTICS.
?

I ask this because we inherited a framework that from time to time analyzes the tables using analyze table (oracle 10.2)
Re: Optimize MERGE statement [message #525817 is a reply to message #525816] Wed, 05 October 2011 11:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Because ANALYZE is obsolete since 9i and is incompatible with:
1/ dbms_stats
2/ Current optimizer algorithm

Regards
Michel

[Edit: typo]

[Updated on: Mon, 24 October 2011 08:42]

Report message to a moderator

Re: Optimize MERGE statement [message #528360 is a reply to message #525817] Mon, 24 October 2011 08:21 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
You can actually use both analyze and dbms_stats if you know the difference of how each works. Analyze collects different data from DBMS_STATS. You in fact must use ANALYZE if you want the items that it collects.

that said, I never use analyze. You can read about the differences in the oracle docs.

Kevin
Re: Optimize MERGE statement [message #528365 is a reply to message #528360] Mon, 24 October 2011 08:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
I should say, use ANALYZE to collect what DBMS_STATS does not, that is VALIDATE ... and LIST CHAINED ROWS.

Regards
Michel

Re: Optimize MERGE statement [message #528419 is a reply to message #525799] Mon, 24 October 2011 15:54 Go to previous message
LNossov
Messages: 318
Registered: July 2011
Location: Germany
Senior Member
Could you please send an explain plan for your merge statement.
Previous Topic: PL/SQL help (performance)
Next Topic: Snap IDs not found for awr reports generation
Goto Forum:
  


Current Time: Tue Apr 23 16:42:10 CDT 2024