SELECT
*
FROM
TCS_ORDER_ASSET_TRAN_RESPONSION oatr
WHERE
oatr.ORDER_ASSET_TRAN_ID = (
SELECT
oat.id
FROM
TCS_ORDER_ASSET_TRAN oat
WHERE
oat.ORDER_ID = ‘xxxx’
)
AND oatr.id = (
SELECT
max(t0.id)
FROM
TCS_ORDER_ASSET_TRAN_RESPONSION t0
WHERE
t0.ORDER_ASSET_TRAN_ID = oatr.ORDER_ASSET_TRAN_ID
);
请问这个 sql 是需要 TCS_ORDER_ASSET_TRAN_RESPONSION 表中 ORDER_ASSET_TRAN_ID 分组后的 最大 id 吗? 能否使用 ROW_NUMBER() OVER (PARTITION 的方式改写试试?
类似如下sql,可以在测试环境先试下:
SELECT * FROM
(SELECT ORDER_ASSET_TRAN_ID, id, ROW_NUMBER() OVER (PARTITION BY ORDER_ASSET_TRAN_ID ORDER BY id DESC) as row_index )
WHERE(row_index = 1) FROM TCS_ORDER_ASSET_TRAN_RESPONSION
您好,这里的问题应该是优化器的问题 ,导致有些条件没有下推。已经有 PR https://github.com/pingcap/tidb/issues/24853 可以关注下 PR ,多谢。
2 目前可以改写 sql 吗?
EXPLAIN SELECT*
FROM (
SELECT*
FROM TCS_ORDER_ASSET_TRAN_RESPONSION oatr
WHERE oatr.ORDER_ASSET_TRAN_ID = (
SELECT oat.id
FROM TCS_ORDER_ASSET_TRAN oat
WHERE oat.ORDER_ID =‘xxxx’
)) oatr,
(
SELECT MAX(t0.id) as id
FROM TCS_ORDER_ASSET_TRAN_RESPONSION t0
WHERE t0.ORDER_ASSET_TRAN_ID = (
SELECT oat.id
FROM TCS_ORDER_ASSET_TRAN oat
WHERE oat.ORDER_ID =‘xxxx’
)) t0
WHERE t0.id = oatr.id;