在PolarDB有的sql里面有自定义存储函数 还有的子查询里面有limit语句的 这种想走列存有什么优化建议吗?自定义函数例如这种SELECT
a.*,
func_create_url_name(a.title) AS url_name
FROM
(
SELECT
con.contentid,
con.catid,
cat.name AS catname,
cat.abbr AS catabbr,
con.modelid,
con.title,
con.subtitle,
con.url AS info_url,
con.tags AS tags,
con.tags AS original_tags,
CASE
WHEN LOWER(LEFT(con.thumb, 4)) = ‘http’ THEN con.thumb
ELSE CONCAT(‘https://xxx/’, con.thumb)
END AS thumb,
con.published,
con.pv
FROM
cmstop_content con
INNER JOIN cmstop_category cat ON cat.catid = con.catid
AND cat.first_parentid = ‘201’
AND FIND_IN_SET(‘204’, cat.parentids)
INNER JOIN (
SELECT
content_tag.contentid
FROM
cmstop_tag tag
INNER JOIN cmstop_content_tag content_tag ON tag.tagid = content_tag.tagid
WHERE
tag.tag = ‘zyp-pd2202071028’
) content_tag ON con.contentid = content_tag.contentid
WHERE
con.status = 6
AND con.modelid IN (1, 3, 4, 10)
ORDER BY
con.published DESC,
con.contentid DESC
LIMIT
1
) a
还有这种子查询有limit的。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
ep2.price
FROM
t_zy_enterprise_price ep2
WHERE
ep2.product_id = ep.product_id
AND ep2.enterprise_id = ep.enterprise_id
AND ep2.price_date < ep.price_date
AND IFNULL(ep2.price_date, ”) != ”
AND IFNULL(ep2.price, ”) != ”
AND IFNULL(ep2.delflag, ”) != ‘1’
ORDER BY
ep2.price_date DESC
LIMIT
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
CREATE DEFINER=young
@%
FUNCTION func_create_url_name
(p_str
varchar(500) charset utf8mb4 collate utf8mb4_general_ci ) RETURNS varchar(500) CHARSET utf8
SQL SECURITY INVOKER
BEGIN
DECLARE urlName VARCHAR(500);
SET urlName = p_str;
SET urlName = REPLACE(urlName,’β’,”);
SET urlName = REPLACE(urlName,’α’,”);
SET urlName = REPLACE(urlName,’ω’,”);
SET urlName = REPLACE(urlName,’~’, ”);
SET urlName = REPLACE(urlName,’!’, ”);
SET urlName = REPLACE(urlName,’!’, ”);
SET urlName = REPLACE(urlName,’#’, ”);
SET urlName = REPLACE(urlName,’$’, ”);
SET urlName = REPLACE(urlName,’¥’, ”);
SET urlName = REPLACE(urlName,’¥’, ”);
SET urlName = REPLACE(urlName,’^’, ”);
SET urlName = REPLACE(urlName,’*’, ”);
SET urlName = REPLACE(urlName,’%’, ”);
SET urlName = REPLACE(urlName,’;’, ”);
SET urlName = REPLACE(urlName,’;’, ”);
SET urlName = REPLACE(urlName,’.’, ”);
SET urlName = REPLACE(urlName,’。’, ”);
SET urlName = REPLACE(urlName,’·’, ”);
SET urlName = REPLACE(urlName,’+’, ”);
SET urlName = REPLACE(urlName,’=’, ”);
SET urlName = REPLACE(urlName,’:’, ”);
SET urlName = REPLACE(urlName,’:’, ”);
SET urlName = REPLACE(urlName,’>’, ”);
SET urlName = REPLACE(urlName,'<', '');
SET urlName = REPLACE(urlName,’》’, ”);
SET urlName = REPLACE(urlName,’《’, ”);
SET urlName = REPLACE(urlName,'[‘, ”);
SET urlName = REPLACE(urlName,’]’, ”);
SET urlName = REPLACE(urlName,’【’, ”);
SET urlName = REPLACE(urlName,’】’, ”);
SET urlName = REPLACE(urlName,'{‘, ”);
SET urlName = REPLACE(urlName,’}’, ”);
SET urlName = REPLACE(urlName,'(‘, ”);
SET urlName = REPLACE(urlName,’)’, ”);
SET urlName = REPLACE(urlName,’(’, ”);
SET urlName = REPLACE(urlName,’)’, ”);
SET urlName = REPLACE(urlName,’、’, ”);
SET urlName = REPLACE(urlName,’|’, ”);
SET urlName = REPLACE(urlName,’™’, ”);
SET urlName = REPLACE(urlName,’®’, ”);
SET urlName = REPLACE(urlName,’©’, ”);
SET urlName = REPLACE(urlName,’?’, ”);
SET urlName = REPLACE(urlName,’?’, ”);
SET urlName = REPLACE(urlName,’\”, ‘ ‘);
SET urlName = REPLACE(urlName,'”‘, ‘ ‘);
SET urlName = REPLACE(urlName,’&’, ‘ ‘);
SET urlName = REPLACE(urlName,’—’, ‘ ‘);
SET urlName = REPLACE(urlName,’/’, ‘ ‘);
SET urlName = REPLACE(urlName,’\’, ‘ ‘);
SET urlName = REPLACE(urlName,’,’, ‘ ‘);
SET urlName = REPLACE(urlName,’,’, ‘ ‘);
SET urlName = REPLACE(urlName,’ ’, ‘ ‘);
SET urlName = REPLACE(urlName,’ ‘, ‘ ‘);
SET urlName = REPLACE(urlName,’ ‘, ‘ ‘);
SET urlName = REPLACE(urlName,’ ‘, ‘-‘);
SET urlName = REPLACE(urlName,’—-‘, ‘-‘);
SET urlName = REPLACE(urlName,’—‘, ‘-‘);
SET urlName = REPLACE(urlName,’–‘, ‘-‘);
SET urlName = TRIM(urlName);
SET urlName = TRIM(BOTH ‘-‘ FROM urlName);
SET urlName = LOWER(urlName);
return urlName;
END
以下为热心网友提供的参考意见
自定义函数里面有控制语句,还在调研中。SELECT
ep2.price
FROM
t_zy_enterprise_price ep2
ORDER BY
ep2.price_date DESC
LIMIT
1
可以改成
SELECT
MAX(ep2.price)
FROM
t_zy_enterprise_price ep2 最后一个函数可以直接让业务处理a.title。此回答整理自钉群“PolarDB专家面对面 – HTAP(列存索引)”