在索引列和日期列之间查询分区的性能

Performance of querying partition by between indexed column and date column

问题: 我很困惑如何利用索引列来提高性能或缩短执行时间,特别是在 window 函数中。

情况: 我正在根据订单日期(从旧到新)对交易进行排序。 我的第一直觉是按交易的 issue_date 进行排序(在 Partition by 子句中)。但是 transaction_id 列被索引并且是 table 的主键。查看 table 的属性,Identity Increment 属性 为空白(不知道为什么,但猜测它的自动增量)。所以我假设按 transaction_id 排序会产生相同的输出,但会大大提高执行速度。

我有以下查询:

SELECT 
 transaction_id
 ,CAST(subscription_id as VARCHAR) as subscription_id 
 ,product_id
 ,ROW_NUMBER() OVER (PARTITION BY subscription_id ORDER BY issue_date ASC) AS tx_rank 
FROM table.transactions

我是否应该这样评价 issue_date 到 transaction_id:

ROW_NUMBER() OVER (PARTITION BY subscription_id ORDER BY transaction_id ASC) AS tx_rank

注意:这个查询占用了10mns,我想尽可能提高它的性能。

不太可能产生太大影响。 row_number() 可以利用正在使用的列上的索引 -- (subscription_id, issue_date)(subscription_id, transaction_id).

我不确定索引是否会显着加快速度。您正在选择整个 table,这可能相当大。