优化sybase SQL查询插入

时间:2014-02-20 09:48:35

标签: sql performance optimization sybase sybase-ase

我想优化我的SQL查询的一部分。我自己尝试了,但它没有变得更好,它变得更糟。我知道这个SQL代码不是最优的,它会占用很多服务器性能。

insert
        into #grid (
                    depotnummer          ,  --varchar(18)
                    lagerstelle          ,
                    bestand_num          ,
                    bestand_neu_ist_num  ,
                    betrag_num           ,
                    kz_versteuerung_jn   ,
                    id
                   )
      select DEPOTNUMMER.PARAMETER_VARCHAR          ,
             LAGERSTELLE_EINBUCHEN.PARAMETER_VARCHAR,
             BESTAND.PARAMETER_NUMERIC              ,
             ANZAHL_EINBUCHEN.PARAMETER_NUMERIC     ,
             0.00                                   ,
             'J'                                    ,
             B.RBM_BUCHUNG_ID
        from RBM_BUCHUNG             B                    ,
             RBM_BUCHUNG_PARAMETER   DEPOTNUMMER          ,
             RBM_BUCHUNG_PARAMETER   LAGERSTELLE_EINBUCHEN,
             RBM_BUCHUNG_PARAMETER   BESTAND              ,
             RBM_BUCHUNG_PARAMETER   ANZAHL_EINBUCHEN     ,
             Kontenstamm             K
       where B.RBM_VORGANG_ID                      = @id
         and DEPOTNUMMER.RBM_BUCHUNG_ID            = B.RBM_BUCHUNG_ID  and convert(char(20),DEPOTNUMMER.PARAMETER_NAME          ) = convert(char(20),'depotnummer'          )
         and LAGERSTELLE_EINBUCHEN.RBM_BUCHUNG_ID  = B.RBM_BUCHUNG_ID  and convert(char(20),LAGERSTELLE_EINBUCHEN.PARAMETER_NAME) in( convert(char(20),'lagerstelle_einbuchen'),'lg_einbuchen')
         and BESTAND.RBM_BUCHUNG_ID                = B.RBM_BUCHUNG_ID  and convert(char(20),BESTAND.PARAMETER_NAME              ) = convert(char(20),'bestand'              )
         and ANZAHL_EINBUCHEN.RBM_BUCHUNG_ID       = B.RBM_BUCHUNG_ID  and convert(char(20),ANZAHL_EINBUCHEN.PARAMETER_NAME     ) = convert(char(20),'anzahl_einbuchen'     )
         and K.Kontonummer                         = DEPOTNUMMER.PARAMETER_VARCHAR
         and K.MANDANTEN_ID                        = isnull(@mandanten_id,K.MANDANTEN_ID)

      update #grid
         set primanoten_id = Primanoten_ID
        from Primanoten     P
       where P.ID          = id
         and P.Quelle      = 'RBM'
    end

那就是Showplan输出:

QUERY PLAN FOR STATEMENT 154 (at line 595).


STEP 1
    The type of query is INSERT.

16 operator(s) under root

   |ROOT:EMIT Operator (VA = 16)
   |
   |   |INSERT Operator (VA = 15)
   |   |  The update mode is direct.
   |   |
   |   |   |MERGE JOIN Operator (Join Type: Inner Join) (VA = 14)
   |   |   | Using Worktable3 for internal storage.
   |   |   |  Key Count: 1
   |   |   |  Key Ordering: ASC
   |   |   |
   |   |   |   |MERGE JOIN Operator (Join Type: Inner Join) (VA = 11)
   |   |   |   | Using Worktable2 for internal storage.
   |   |   |   |  Key Count: 1
   |   |   |   |  Key Ordering: ASC
   |   |   |   |
   |   |   |   |   |N-ARY NESTED LOOP JOIN Operator (VA = 8) has 4 children.
   |   |   |   |   |
   |   |   |   |   |   |SORT Operator (VA = 1)
   |   |   |   |   |   | Average Row width is 6.000000 
   |   |   |   |   |   | Using Worktable1 for internal storage.
   |   |   |   |   |   |
   |   |   |   |   |   |   |SCAN Operator (VA = 0)
   |   |   |   |   |   |   |  FROM TABLE
   |   |   |   |   |   |   |  RBM_BUCHUNG
   |   |   |   |   |   |   |  B
   |   |   |   |   |   |   |  Index : SK1
   |   |   |   |   |   |   |  Forward Scan.
   |   |   |   |   |   |   |  Positioning by key.
   |   |   |   |   |   |   |  Keys are:
   |   |   |   |   |   |   |    RBM_VORGANG_ID ASC
   |   |   |   |   |   |   |  Using I/O Size 2 Kbytes for index leaf pages.
   |   |   |   |   |   |   |  With LRU Buffer Replacement Strategy for index leaf pages.
   |   |   |   |   |   |   |  Using I/O Size 2 Kbytes for data pages.
   |   |   |   |   |   |   |  With LRU Buffer Replacement Strategy for data pages.
   |   |   |   |   |
   |   |   |   |   |   |RESTRICT Operator (VA = 3)(0)(0)(0)(6)(0)
   |   |   |   |   |   |
   |   |   |   |   |   |   |SCAN Operator (VA = 2)
   |   |   |   |   |   |   |  FROM TABLE
   |   |   |   |   |   |   |  RBM_BUCHUNG_PARAMETER
   |   |   |   |   |   |   |  DEPOTNUMMER
   |   |   |   |   |   |   |  Index : SK1
   |   |   |   |   |   |   |  Forward Scan.
   |   |   |   |   |   |   |  Positioning by key.
   |   |   |   |   |   |   |  Keys are:
   |   |   |   |   |   |   |    RBM_BUCHUNG_ID ASC
   |   |   |   |   |   |   |  Using I/O Size 2 Kbytes for index leaf pages.
   |   |   |   |   |   |   |  With LRU Buffer Replacement Strategy for index leaf pages.
   |   |   |   |   |   |   |  Using I/O Size 2 Kbytes for data pages.
   |   |   |   |   |   |   |  With LRU Buffer Replacement Strategy for data pages.
   |   |   |   |   |
   |   |   |   |   |   |RESTRICT Operator (VA = 5)(0)(0)(0)(6)(0)
   |   |   |   |   |   |
   |   |   |   |   |   |   |SCAN Operator (VA = 4)
   |   |   |   |   |   |   |  FROM TABLE
   |   |   |   |   |   |   |  Kontenstamm
   |   |   |   |   |   |   |  K
   |   |   |   |   |   |   |  Index : SK2
   |   |   |   |   |   |   |  Forward Scan.
   |   |   |   |   |   |   |  Positioning by key.
   |   |   |   |   |   |   |  Keys are:
   |   |   |   |   |   |   |    Kontonummer ASC
   |   |   |   |   |   |   |  Using I/O Size 2 Kbytes for index leaf pages.
   |   |   |   |   |   |   |  With LRU Buffer Replacement Strategy for index leaf pages.
   |   |   |   |   |   |   |  Using I/O Size 2 Kbytes for data pages.
   |   |   |   |   |   |   |  With LRU Buffer Replacement Strategy for data pages.
   |   |   |   |   |
   |   |   |   |   |   |RESTRICT Operator (VA = 7)(0)(0)(0)(6)(0)
   |   |   |   |   |   |
   |   |   |   |   |   |   |SCAN Operator (VA = 6)
   |   |   |   |   |   |   |  FROM TABLE
   |   |   |   |   |   |   |  RBM_BUCHUNG_PARAMETER
   |   |   |   |   |   |   |  BESTAND
   |   |   |   |   |   |   |  Index : SK1
   |   |   |   |   |   |   |  Forward Scan.
   |   |   |   |   |   |   |  Positioning by key.
   |   |   |   |   |   |   |  Keys are:
   |   |   |   |   |   |   |    RBM_BUCHUNG_ID ASC
   |   |   |   |   |   |   |  Using I/O Size 2 Kbytes for index leaf pages.
   |   |   |   |   |   |   |  With LRU Buffer Replacement Strategy for index leaf pages.
   |   |   |   |   |   |   |  Using I/O Size 16 Kbytes for data pages.
   |   |   |   |   |   |   |  With LRU Buffer Replacement Strategy for data pages.
   |   |   |   |
   |   |   |   |   |RESTRICT Operator (VA = 10)(0)(0)(0)(6)(0)
   |   |   |   |   |
   |   |   |   |   |   |SCAN Operator (VA = 9)
   |   |   |   |   |   |  FROM TABLE
   |   |   |   |   |   |  RBM_BUCHUNG_PARAMETER
   |   |   |   |   |   |  ANZAHL_EINBUCHEN
   |   |   |   |   |   |  Index : SK1
   |   |   |   |   |   |  Forward Scan.
   |   |   |   |   |   |  Positioning at index start.
   |   |   |   |   |   |  Using I/O Size 16 Kbytes for index leaf pages.
   |   |   |   |   |   |  With LRU Buffer Replacement Strategy for index leaf pages.
   |   |   |   |   |   |  Using I/O Size 16 Kbytes for data pages.
   |   |   |   |   |   |  With LRU Buffer Replacement Strategy for data pages.
   |   |   |
   |   |   |   |RESTRICT Operator (VA = 13)(0)(0)(0)(11)(0)
   |   |   |   |
   |   |   |   |   |SCAN Operator (VA = 12)
   |   |   |   |   |  FROM TABLE
   |   |   |   |   |  RBM_BUCHUNG_PARAMETER
   |   |   |   |   |  LAGERSTELLE_EINBUCHEN
   |   |   |   |   |  Index : SK1
   |   |   |   |   |  Forward Scan.
   |   |   |   |   |  Positioning at index start.
   |   |   |   |   |  Using I/O Size 16 Kbytes for index leaf pages.
   |   |   |   |   |  With LRU Buffer Replacement Strategy for index leaf pages.
   |   |   |   |   |  Using I/O Size 16 Kbytes for data pages.
   |   |   |   |   |  With LRU Buffer Replacement Strategy for data pages.
   |   |
   |   |  TO TABLE
   |   |  #grid
   |   |  Using I/O Size 2 Kbytes for data pages.

1 个答案:

答案 0 :(得分:0)

  

因为所有SP都被写入以搜索该小写字母。那会   be" to be"重写所有的SP&#S; S。所以它的重要性就是这样做。

我不明白"如何转换为char"与"小写"相关但是,您可以为"转换"创建物化计算列。值并为列创建覆盖索引(RBM_BUCHUNG_ID +计算PARAMETER_NAME)。

此案例可以提高查询的性能。