简体   繁体   English

Oracle中查询的优化

[英]Optimization of query in Oracle

Update table from statement. 从语句更新表。 Using subquery. 使用子查询。 I need write an update statement that used same table. 我需要编写一个使用同一表的更新语句。
I used subquery for update multiple columns. 我用子查询来更新多列。 My example. 我的例子。

UPDATE  USER_BANCU.REGISTRU_21052016_AE NEW 
        SET  (
             NEW.LIST_COND,
             NEW.LISTA_FOND,
             NEW.GEN_ACT_NE_LIC,
             NEW.GEN_ACT_LIC
             )
           =

         (
    SELECT 
        OLD.LIST_COND,
        OLD.LISTA_FOND,
        OLD.GEN_ACT_NE_LIC,
        OLD.GEN_ACT_LIC
        FROM (
    SELECT 
      VB.IDNO IDNO ,
      trim_vb(VB.LIST_COND)                LIST_COND,
      trim_vb(VB.LISTA_FOND)               LISTA_FOND,
      REPLACE(VB.GEN_ACT_NE_LIC, ' ','' )  GEN_ACT_NE_LIC,  
      REPLACE(VB.GEN_ACT_LIC, ' ','' )     GEN_ACT_LIC    
      FROM   USER_BANCU.REGISTRU_21052016_AE VB
       ) OLD

        WHERE 
            OLD.IDNO=NEW.IDNO 
      )

         WHERE EXISTS (

         SELECT *    
        FROM (
    SELECT 

      VB.IDNO IDNO ,
      trim_vb(VB.LIST_COND)                LIST_COND,
      trim_vb(VB.LISTA_FOND)               LISTA_FOND,
      REPLACE(VB.GEN_ACT_NE_LIC, ' ','' )  GEN_ACT_NE_LIC,  
      REPLACE(VB.GEN_ACT_LIC, ' ','' )     GEN_ACT_LIC

      FROM   USER_BANCU.REGISTRU_21052016_AE VB
       ) OLD


        WHERE 
         OLD.IDNO=NEW.IDNO 
         )

Update table from statement. 从语句更新表。 Using subbquery. 使用subbquery。 Ability to optimize? 优化能力? Is it possible to create a procedure or a cursor in this case? 在这种情况下是否可以创建过程或游标?

I have an error when you run a query --ORA-01427: single-row subquery returns more than one row. 运行查询--ORA-01427时出现错误:单行子查询返回多个行。

It looks like you've massively overcomplicated the update. 您似乎已使该更新过于复杂。 Since you're updating every single row in the table with values from the same table, I think you're just trying to do: 由于您要使用同一张表中的值更新表中的每一行,因此我认为您只是在尝试这样做:

update user_bancu.registru_21052016_ae
set    list_cond = trim_vb(vb.list_cond),
       lista_fond = trim_vb(vb.lista_fond),
       gen_act_ne_lic = replace(vb.gen_act_ne_lic, ' '),
       gen_act_lic = replace(vb.gen_act_lic, ' ');

NB I removed the '' from the replace parameters because in Oracle, there isn't such a thing as an empty string - it's treated the same as null. 注意,我从replace参数中删除了'' ,因为在Oracle中,没有空字符串之类的东西-它被视为null。 And as the default value of the string-to-replace-with parameter is null, you can just remove the parameter altogether. 并且由于string-to-replace-with参数的默认值为null,因此您可以完全删除该参数。

Also, replacing the above statement with a procedure involving looping round a cursor is likely to be slower. 同样,用涉及循环游标的过程替换上面的语句可能会更慢。 If you have to have a procedure, just use the update statement directly in the procedure. 如果必须要有一个过程,只需直接在该过程中使用update语句即可。

If you need to speed things up even further, than the above update statement, I suggest you take a look at the trim_vb function calls - if you can move the logic directly into the update statement, then that should speed things up even more (certainly in pre-12c, user defined function calls in DML statements involve context switching between the SQL and PL/SQL engines, which slows things down.). 如果您需要进一步加快速度,而不是上面的update语句,建议您看一下trim_vb函数调用-如果您可以将逻辑直接移到update语句中,那么那应该可以进一步加快速度(当然在12c之前的版本中,DML语句中的用户定义函数调用涉及SQL和PL / SQL引擎之间的上下文切换,这会减慢速度。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM