Search code examples
oracle11gquery-optimization

Update with multiple tables and query optimization


I have this update statement

  UPDATE
  pr
  SET
  pr.ult_prezzo_euro = ROUND(pr.ult_prezzo/fs.cambio,7)
  FROM --error SQL COMMAND NOT PROPERLY ENDED
  fin_prodotto prod INNER JOIN
  fin_prodotto_linea fpl ON prod.prodotto_id=fpl.prodotto_id INNER JOIN
  fin_att_fin faf ON fpl.attivita_fin_id=faf.attivita_fin_id INNER JOIN
  fin_prezzo pr ON pr.attivita_fin_id=faf.attivita_fin_id INNER JOIN
  flx_sec_posizione_dt_upd fs ON pr.attivita_fin_id=fs.attivita_fin_id
  where 
      prod.prodotto_id=43
  and faf.codice_titolo_cad_s is not null
  and pr.ult_prezzo = pr.ult_prezzo_euro
  and faf.divisa_quot_t<>'242'
  and prod.gstd_esist_b='S'
  and fpl.gstd_esist_b='S'
  and faf.gstd_esist_b='S'
  and pr.gstd_esist_b='S'
  and pr.gstd_ult_user_s in ('AGGIORNAMENTO_POSIZIONE')
  and pr.ult_prezzo>0
  and fs.cambio>0;

it give me "SQL COMMAND NOT PROPERLY ENDED" pointing at FROM row,

And also i would optimize this update statement because this should run on about 2 millions records, how is possible?


Solution

  • i can't be sure it will be right answer without seeing your database structure, but first: you have wrong syntax, it should look like this:

    UPDATE fin_prezzo as pr SET pr.ult_prezzo_euro = ...
    

    and then you will have to change chain of JOINS, to adequately join from fin_prezzo table (as the first table mentioned). if it would be problem, you can 'UPDATE' another table in the statement (without changing/adding/removing updated columns)

    Ad optimization part: it will be good to start with SELECT statement (instead of UPDATE) to see, how it is joinning (maybe duplicating rows because of joins) and reduce the duplications, or not necessary joins. secondly is quicker to move 'rules' from WHERE statement to JOIN statement for example:

     INNER JOIN fin_att_fin as faf ON fpl.attivita_fin_id=faf.attivita_fin_id AND faf.gstd_esist_b='S'
    

    another good technique is comparing numbers instead of strings ('S', '242'...), it is simply quicker

    rest is up to trying..

    hope i helped a bit ;)