SQL语句优化之二

如下SQL语句执行大约需要10分钟:

update leapusertable set hasposition =1 where id in (select userid from leappositionusermap)

 

改写SQL语句的写法,40秒:

update leapusertable a set a.hasposition =1 where exists (select b.userid from leappositionusermap b  where b.userid=a.id)

 

执行计划对比如下:

QUERY: (OPTIMIZATION TIMESTAMP: 09-08-2020 20:13:51)
------
update leapusertable set hasposition =1 where id in (select userid from leappositionusermap)


Estimated Cost: 114028
Estimated # of Rows Returned: 356170

  1) informix.leapusertable: SEQUENTIAL SCAN

        Filters: informix.leapusertable.id = ANY  

    Subquery:
    ---------
    Estimated Cost: 28028
    Estimated # of Rows Returned: 712277

      1) informix.leappositionusermap: SEQUENTIAL SCAN

    UDRs in query:
    --------------
        UDR id  :    -114
        UDR name:    equal

UDRs in query:
--------------
    UDR id  :    -114
    UDR name:    equal

Query statistics:
-----------------

  Table map :
  ----------------------------
  Internal name     Table name
  ----------------------------
  t1                leapusertable

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t1     712164     356170    712339     08:37.03   114028  

 

############################################################################################

 

QUERY: (OPTIMIZATION TIMESTAMP: 09-08-2020 20:29:21)
------
update leapusertable a set a.hasposition =1 where exists (select b.userid from leappositionusermap b  where b.userid=a.id)


Estimated Cost: 592993
Estimated # of Rows Returned: 712361

  1) informix.a: SEQUENTIAL SCAN

  2) informix.b: INDEX PATH  (First Row)

    (1) Index Name: informix.leappositionusermap_userid_idx
        Index Keys: userid   (Serial, fragments: ALL)
        Lower Index Filter: informix.equal(informix.b.userid ,informix.a.id )
NESTED LOOP JOIN  (Semi Join)

UDRs in query:
--------------
    UDR id  :    -114
    UDR name:    equal
    UDR id  :    -722
    UDR name:    compare

Query statistics:
-----------------

  Table map :
  ----------------------------
  Internal name     Table name
  ----------------------------
  t1                a
  t2                b

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t1     712339     712339    712339     00:05.65   86000   

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t2     0          712277    0          00:21.87   1       

  type     rows_prod  est_rows  time       est_cost
  -------------------------------------------------
  nljoin   712164     712362    00:27.88   592994  
 

不知道结果集是不是一样 *_* 

 

 

 

你可能感兴趣的:(informix技术,数据库)