【问题标题】:Optimization of Sybase 15.5 union querySybase 15.5联合查询优化
【发布时间】:2014-06-01 16:34:44
【问题描述】:

我在尝试优化 Sybase 15.5 上的以下查询时遇到了问题。有谁知道我该如何改进它。那里使用的每个表都有大约 3000 万行。我尽力优化它,但仍然需要很多时间(1.5 小时)。

create table #tmp1( f_id smallint, a_date smalldatetime )
create table #tmp2( f_id smallint, a_date smalldatetime )


insert #tmp1
select f_id, a_date = max( a_date )
    FROM  audit_table
    WHERE  i_date =  @pIDate
group by f_id

insert #tmp2
select f_id , a_date = max( a_date )
    FROM  n_audit_table
    WHERE  i_date =  @pIDate
group by f_id

    create table #tmp(
    t_account varchar(32) not null,
    t_id varchar(32) not null,
    product varchar(64) null
    )

insert into #tmp
select t_account,t_id, product
    FROM  audit_table nt, #tmp1 a
    WHERE  i_date   =  @pIDate
    and nt.a_date = a.a_date
    and nt.f_id = a.f_id
union
select t_account,t_id, product
    FROM  n_audit_table t, #tmp2 a
    WHERE  t.item_date =  @pIDate
    and t.a_date = a.a_date
    and t.f_id = a.f_id

两个表都有 i_date、a_date、f_id 的索引。请在下面的展示计划中找到很长时间的地方。 语句 2 的查询计划(第 24 行)。 使用串行模式进行优化

STEP 1
    The type of query is INSERT.

10 operator(s) under root

   |ROOT:EMIT Operator (VA = 10)
   |
   |   |INSERT Operator (VA = 9)
   |   |  The update mode is direct.
   |   |
   |   |   |HASH UNION Operator (VA = 8)  has 2 children.
   |   |   | Using Worktable1 for internal storage.
   |   |   |  Key Count: 3
   |   |   |
   |   |   |   |NESTED LOOP JOIN Operator (VA = 3) (Join Type: Inner Join)
   |   |   |   |
   |   |   |   |   |SCAN Operator (VA = 0)
   |   |   |   |   |  FROM TABLE
   |   |   |   |   |  #tmp1
   |   |   |   |   |  a
   |   |   |   |   |  Table Scan.
   |   |   |   |   |  Forward Scan.
   |   |   |   |   |  Positioning at start of table.
   |   |   |   |   |  Using I/O Size 2 Kbytes for data pages.
   |   |   |   |   |  With LRU Buffer Replacement Strategy for data pages.
   |   |   |   |
   |   |   |   |   |RESTRICT Operator (VA = 2)(5)(0)(0)(0)(0)
   |   |   |   |   |
   |   |   |   |   |   |SCAN Operator (VA = 1)
   |   |   |   |   |   |  FROM TABLE
   |   |   |   |   |   |  audit_table
   |   |   |   |   |   |  nt
   |   |   |   |   |   |  Index : IX_audit_table
   |   |   |   |   |   |  Forward Scan.
   |   |   |   |   |   |  Positioning by key.
   |   |   |   |   |   |  Keys are:
   |   |   |   |   |   |    i_date ASC
   |   |   |   |   |   |    a_date 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.
   |   |   |
   |   |   |   |NESTED LOOP JOIN Operator (VA = 7) (Join Type: Inner Join)
   |   |   |   |
   |   |   |   |   |SCAN Operator (VA = 4)
   |   |   |   |   |  FROM TABLE
   |   |   |   |   |  #tmp2
   |   |   |   |   |  a
   |   |   |   |   |  Table Scan.
   |   |   |   |   |  Forward Scan.
   |   |   |   |   |  Positioning at start of table.
   |   |   |   |   |  Using I/O Size 2 Kbytes for data pages.
   |   |   |   |   |  With LRU Buffer Replacement Strategy for data pages.
   |   |   |   |
   |   |   |   |   |RESTRICT Operator (VA = 6)(5)(0)(0)(0)(0)
   |   |   |   |   |
   |   |   |   |   |   |SCAN Operator (VA = 5)
   |   |   |   |   |   |  FROM TABLE
   |   |   |   |   |   |  n_audit_table
   |   |   |   |   |   |  t
   |   |   |   |   |   |  Index : IX_n_audit_table
   |   |   |   |   |   |  Forward Scan.
   |   |   |   |   |   |  Positioning by key.
   |   |   |   |   |   |  Keys are:
   |   |   |   |   |   |    i_date ASC
   |   |   |   |   |   |    a_date 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.
   |   |
   |   |  TO TABLE
   |   |  #tmp
   |   |  Using I/O Size 2 Kbytes for data pages.


 Total estimated I/O cost for statement 2 (at line 24): 29322945.

【问题讨论】:

  • 你看过查询计划了吗?成本最高的地方在哪里?
  • 大部分成本发生在联合时。我已经用演出计划更新了我的查询。请看看并提出建议
  • 有人可以帮我解决这个问题吗?

标签: tsql union sybase sap-ase distinct-values


【解决方案1】:

我怀疑这是工会问题。查询更有可能是麻烦制造者。

我想您应该从在临时表上添加索引开始:

create table #tmp1( f_id smallint, a_date smalldatetime ) 
Create clustered index IX1Temp on #tmp1(f_id ) 
Create clustered index IX2Temp on #tmp1(a_date ) 
...

另外,我认为#tmp1、#tmp2 的使用方式没有多大意义。你可以打电话给 CTE。还。我建议您尝试使用 PARTITION BY 而不是 GROUP BY 语句。

【讨论】:

    【解决方案2】:

    根据查询执行计划,问题是临时表上的表扫描。

    请获取以下查询的执行计划:

    insert into #tmp
    select t_account,t_id, product
    FROM
        audit_table nt,
        (
            select f_id, a_date = max(a_date)
            FROM  audit_table
            WHERE  i_date =  @pIDate
            group by f_id
        ) a
    WHERE
        i_date   =  @pIDate
        and nt.a_date = a.a_date
        and nt.f_id = a.f_id
    union
    select t_account,t_id, product
    FROM
        n_audit_table t,
        (
            select f_id , a_date = max( a_date )
            FROM  n_audit_table
            WHERE  i_date =  @pIDate
            group by f_id
        ) a
    WHERE
        t.item_date =  @pIDate
        and t.a_date = a.a_date
        and t.f_id = a.f_id
    

    【讨论】:

      【解决方案3】:

      每个临时表中有多少行? 看起来可以使用 HAVING 替换临时表,我需要对其进行测试,当您的 group by 位于单个列上并且您需要输出中的更多列时,它总是很复杂。

      尝试使用SET STATISTICS PLANCOST ON 和 SET STATISTICS IO ON 运行此语句,因为这样可以很好地了解扫描的页数以及 Sybase 在优化查询时是否出现问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-21
        • 2018-04-07
        • 2020-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多