PolarDB这个问题怎么解决? max没有排序语义 sql语义变了,改成这种窗口函数实现 可以走列存 也非常慢 直接把ap节点cpu拉满了,SELECT ep.product_id, p.products_name AS products_name_m, p.products_url_name, ep.enterprise_id, ent.enterprise_name_en AS enterprise_name
, ent.url_name AS enterprise_url_name, ep.specification, DATE_FORMAT(ep.price_date, '%Y-%m-%d') AS price_date
, ep.price_unit, ep.price AS current_price
, (
SELECT tmp.price
FROM (
SELECT ep2.price, ROW_NUMBER() OVER (PARTITION BY ep2.product_id ORDER BY ep2.price_date DESC) AS rank_no
FROM echemi_db_v0_3.t_zy_enterprise_price ep2
LEFT JOIN t_zy_enterprise_price ep
ON ep2.product_id = ep.product_id
AND ep2.enterprise_id = ep.enterprise_id
WHERE IFNULL(ep2.delflag, '') <> '1'
AND ep2.price_date < ep.price_date
AND IFNULL(ep2.price_date, '') != ''
AND IFNULL(ep2.price, '') != ''
AND IFNULL(ep2.delflag, '') != '1'
) tmp
WHERE tmp.rank_no = 1
) AS previous_price, 0 AS subscribe_flag
FROM t_zy_enterprise_price ep
INNER JOIN t_e_products p
ON p.products_id = ep.product_id
AND p.information_flag = '1'
AND IFNULL(p.delflag, '0') != '1'
INNER JOIN t_zy_enterprise ent
ON ent.id = ep.enterprise_id
AND IFNULL(ent.delflag, '0') != '1'
INNER JOIN (
SELECT ep3.product_id, ep3.enterprise_id, MAX(ep3.price_date) AS max_price_date
FROM t_zy_enterprise_price ep3
GROUP BY ep3.product_id, ep3.enterprise_id
) pimax
ON pimax.product_id = ep.product_id
AND pimax.enterprise_id = ep.enterprise_id
AND pimax.max_price_date = ep.price_date
WHERE ep.delflag = '0'
ORDER BY subscribe_flag DESC, ep.price_date DESC, ep.update_date DESC, ep.id
LIMIT 5
那得用window改,SELECT
tmp1.*,
(
SELECT
tmp.price
FROM
(
SELECT
ep2.price,
ROW_NUMBER() OVER (
PARTITION BY ep2.product_id
ORDER BY
ep2.price_date DESC
) AS rank_no
FROM
echemi_db_v0_3.t_zy_enterprise_price ep2
LEFT JOIN t_zy_enterprise_price ep ON ep2.product_id = ep.product_id
AND ep2.enterprise_id = ep.enterprise_id
WHERE
IFNULL(ep2.delflag, '') <> '1'
AND ep2.price_date < ep.price_date
AND IFNULL(ep2.price_date, '') != ''
AND IFNULL(ep2.price, '') != ''
AND IFNULL(ep2.delflag, '') != '1'
) tmp
WHERE
tmp.rank_no = 1
) AS previous_price,
0 AS subscribe_flag
FROM
(
SELECT
ep.product_id,
p.products_name AS products_name_m,
p.products_url_name,
ep.enterprise_id,
ent.enterprise_name_en AS enterprise_name,
ent.url_name AS enterprise_url_name,
ep.specification,
DATE_FORMAT(ep.price_date, '%Y-%m-%d') AS price_date,
ep.price_unit,
ep.price AS current_price,
ep.price_date as pd,
ep.update_date as ud,
ep.id as id
FROM
t_zy_enterprise_price ep
INNER JOIN t_e_products p ON p.products_id = ep.product_id
AND p.information_flag = '1'
AND IFNULL(p.delflag, '0') != '1'
INNER JOIN t_zy_enterprise ent ON ent.id = ep.enterprise_id
AND IFNULL(ent.delflag, '0') != '1'
INNER JOIN (
SELECT
ep3.product_id,
ep3.enterprise_id,
MAX(ep3.price_date) AS max_price_date
FROM
t_zy_enterprise_price ep3
GROUP BY
ep3.product_id,
ep3.enterprise_id
) pimax ON pimax.product_id = ep.product_id
AND pimax.enterprise_id = ep.enterprise_id
AND pimax.max_price_date = ep.price_date
WHERE
ep.delflag = '0'
ORDER BY
subscribe_flag DESC,
ep.price_date DESC,
ep.update_date DESC,
ep.id
LIMIT
5
) tmp1
ORDER BY
subscribe_flag DESC,
tmp1.pd DESC,
tmp1.ud DESC,
tmp1.id
LIMIT
5
试试这个,先做LIMIT 5减少下window的计算量,也可以试试把
SELECT ep2.price, ep2.price_date FROM t_zy_enterprise_price ep2 ORDER BY ep2.price_date DESC, ep2.price desc limit 1;
改为
select max(ep2.price) FROM t_zy_enterprise_price ep2 where ep2.price_date = (SELECT max(price_date) from t_zy_enterprise_price);此回答整理自钉群“PolarDB专家面对面 - HTAP(列存索引)”
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
PolarDB 分布式版 (PolarDB for Xscale,简称“PolarDB-X”) 是阿里云自主设计研发的高性能云原生分布式数据库产品,为用户提供高吞吐、大存储、低延时、易扩展和超高可用的云时代数据库服务。