Sybase ORDER BY 慢速请求

Sybase ORDER BY slow request

无法想象在这里做什么。我的查询是:

SELECT
    ACCOUNT.ID,
    ACCOUNT.NAME,
    SUPPLIERACCOUNT.NAME,
    HEADER.CURRENCY,
    HEADER.DDATE,
    HEADER.RECIPIENTACCOUNTNUMBER,
    HEADER.REQUESTID,
    HEADER.PRINCIPALAMOUNT,
    LINEITEM.HEADERID,
    LINEITEM.DATE
FROM
    ACCOUNT
         JOIN HEADER ON ACCOUNT.ID = HEADER.ISSUERACCOUNTNUMBER
         JOIN ACCOUNT AS SUPPLIERACCOUNT ON HEADER.RECIPIENTACCOUNTNUMBER = SUPPLIERACCOUNT.ID
         JOIN LINEITEM ON HEADER.HEADERID = LINEITEM.HEADERID
WHERE ACCOUNT.BPID = 7222222
  AND LINEITEM.LTYPE = 'P'
  AND HEADER.POSTINGDATE >= ACCOUNT.CREATIONDATE
  and ACCOUNT.ID IN (1111111,2222222 ...)
  and HEADER.RECIPIENTACCOUNTNUMBER IN (3333333,6666666,9999999 ...)
  AND HEADER.POSTINGDATE >= '2018-11-17'
  AND HEADER.POSTINGDATE <= '2019-10-17'
ORDER BY LINEITEM.DATE, ACCOUNT.ID, LINEITEM.HEADERID

在 ORDER 中没有 LINEITEM.DATE 和 LINEITEM.HEADERID 我的查询运行了 2 秒,它们大约运行了 1 分钟。 结果大约有 20 万行。 ORDER 中的这些列在表中有索引。

谁能给我一个提示,我错过了什么? 谢谢。

编辑。 Sybase 版本 ASE 16.0。 使用完整的 ORDER 和 set 统计时间,io on 我有这个结果:

[2019-10-20 22:05:17] [01000][3613] Parse and Compile Time 11.
[2019-10-20 22:05:17] Adaptive Server cpu time: 1100 ms.
[2019-10-20 22:05:17] [01000][3613] Parse and Compile Time 2.
[2019-10-20 22:05:17] Adaptive Server cpu time: 200 ms.
[2019-10-20 22:05:18] [01000][3615] Table: Worktable1 scan count 1, logical reads: (regular=18136 apf=0 total=18136), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
[2019-10-20 22:05:18] [01000][3615] Table: ACCOUNT scan count 214, logical reads: (regular=428 apf=0 total=428), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
[2019-10-20 22:05:18] [01000][3615] Table: HEADER scan count 214, logical reads: (regular=14934 apf=0 total=14934), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
[2019-10-20 22:05:18] [01000][3615] Table: LINEITEM scan count 199411, logical reads: (regular=420968 apf=0 total=420968), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
[2019-10-20 22:05:18] [01000][3615] Table: ACCOUNT (SUPPLIERACCOUNT) scan count 501, logical reads: (regular=1002 apf=0 total=1002), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
[2019-10-20 22:05:18] [01000][3631] Total actual I/O cost for this command: 874664.
[2019-10-20 22:05:18] [01000][3614] Total writes for this command: 0

[2019-10-20 22:05:18] [01000][3612] Execution Time 566.
[2019-10-20 22:05:18] Adaptive Server cpu time: 56513 ms.  Adaptive Server elapsed time: 56790 ms.
[2019-10-20 22:05:18] 500 rows retrieved starting from 1 in 58 s 825 ms (execution: 58 s 473 ms, fetching: 352 ms)

只有 ORDER BY ACCOUNT.ID 我有:

[2019-10-20 22:15:22] [01000][3613] Parse and Compile Time 11.
[2019-10-20 22:15:22] Adaptive Server cpu time: 1100 ms.
[2019-10-20 22:15:22] [01000][3613] Parse and Compile Time 2.
[2019-10-20 22:15:22] Adaptive Server cpu time: 200 ms.
[2019-10-20 22:15:22] [01000][3615] Table: Worktable3 scan count 1, logical reads: (regular=2 apf=0 total=2), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
[2019-10-20 22:15:22] [01000][3615] Table: Worktable1 scan count 1, logical reads: (regular=2 apf=0 total=2), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
[2019-10-20 22:15:22] [01000][3615] Table: ACCOUNT scan count 1, logical reads: (regular=205 apf=0 total=205), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
[2019-10-20 22:15:22] [01000][3615] Table: HEADER scan count 2, logical reads: (regular=33 apf=0 total=33), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
[2019-10-20 22:15:22] [01000][3615] Table: LINEITEM scan count 501, logical reads: (regular=1142 apf=0 total=1142), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
[2019-10-20 22:15:22] [01000][3615] Table: ACCOUNT (SUPPLIERACCOUNT) scan count 501, logical reads: (regular=1002 apf=0 total=1002), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
[2019-10-20 22:15:22] [01000][3631] Total actual I/O cost for this command: 4764.
[2019-10-20 22:15:22] [01000][3614] Total writes for this command: 0

[2019-10-20 22:15:22] [01000][3612] Execution Time 1.
[2019-10-20 22:15:22] Adaptive Server cpu time: 105 ms.  Adaptive Server elapsed time: 106 ms.
[2019-10-20 22:15:22] 500 rows retrieved starting from 1 in 2 s 163 ms (execution: 1 s 936 ms, fetching: 227 ms)

只有:

order by account.id

Sybase 可以清楚地选择一个查询计划,它按 id 顺序遍历帐户 table,然后加入其余行并进行过滤。这意味着它可以非常快速地开始返回结果。目前尚不清楚这 2 秒是否是从 第一个 结果到 整个 结果集的时间。

更复杂的order by,Sybase 必须生成整个结果集,然后对其进行排序。当键在 table 之间混合时,它不能使用索引。除非你 运行 戴着手表,否则排序 20 万行应该不会花一分钟。有一些注意事项:

  • 如果行真的很宽,那么排序时间可能比预期的要长。
  • 如果更简单的 order by 在 2 秒后开始 返回结果,但整个结果集花费的时间更长,则比较无效。