惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
H
Help Net Security
月光博客
月光博客
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
GbyAI
GbyAI
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - chenzechao

时间复杂度汇总对比表 GROUP BY + 窗口嵌套聚合(单层 SQL,不嵌套子查询) SQL rename 语法 macOS SecureCRT 外接小键盘回车失效完整修复 Windows 11 首次开机引导(OOBE 阶段)跳过登录微软账户,创建本地账户 Claude Code Skill 突然「用不了」 HomeAssistant 安装HACS MacOS系统GPU占用高 edge 抓取 DNS 解析记录 router iStoreOS mysql starrocks json处理 json遍历数组所有元素 git复制指定提交到其他分支 secureCrt标签页固定 mac键盘 vs code settings.json 拉链表匹配筛选日期区间 StarRocks中CTE报错 shell 循环遍历的详细用法 shell变量带默认值配置 cursor自动执行 JDK17生成JRE环境 Alt+Tab切换窗口时不包括网页窗口 mysql变量使用 maven工程改版本号,支持多模块一次性修改 SQL自定义排序 IDEA常用快捷键 Linux环境使用上下方向键无法查看history历史记录问题解决方法 博文阅读密码验证 - 博客园 linux设置http proxy flask框架在本机可访问,而其它机子访问不了
mysql取中位数、p80、p90
chenzechao · 2025-07-14 · via 博客园 - chenzechao
select
     api_path
    ,avg(time ) as time_avg
    ,max(case when rn = cast(cn * 0.5 as UNSIGNED) then time end) as time_p50
    ,max(case when rn = cast(cn * 0.8 as UNSIGNED) then time end) as time_p80
    ,max(case when rn = cast(cn * 0.9 as UNSIGNED) then time end) as time_p90
    ,max(case when rn = cast(cn * 1.0 as UNSIGNED) then time end) as time_p100
from (
    select
         ROW_NUMBER() over(partition by t1.api_path order by time) as rn
        ,count(1) over(partition by t1.api_path )      as cn
        ,t1.biz_dt
        ,t1.api_path
        ,t1.time
    from api_log t1
) t2
group by
    api_path
order by time_p50 desc
;
select 
     t1.api_path
    ,t1.api_name
    ,t1.cnt
    ,t1.duration_avg
    ,t1.duration_min
    ,t1.duration_max
    ,t2.time_p50
    ,t2.time_p80
    ,t2.time_p90
    ,t2.time_p100
    ,row_number() over(order by t2.time_p90 desc) as rn
from (
    SELECT 
         api_path
        ,api_name
        ,count(1) as cnt
        ,avg(duration_prd) as duration_avg
        ,min(duration_prd) as duration_min
        ,max(duration_prd) as duration_max
    FROM api_request_config_performance
    group by 
         api_path
        ,api_name
    order by 
        duration_avg desc
) t1
left join (
    select
         api_path
        ,avg(duration_prd )                                                   as time_avg
        ,max(case when rn = cast(cn * 0.5 as UNSIGNED) then duration_prd end) as time_p50
        ,max(case when rn = cast(cn * 0.8 as UNSIGNED) then duration_prd end) as time_p80
        ,max(case when rn = cast(cn * 0.9 as UNSIGNED) then duration_prd end) as time_p90
        ,max(case when rn = cast(cn * 1.0 as UNSIGNED) then duration_prd end) as time_p100
    from (
        select
             ROW_NUMBER() over(partition by t1.api_path order by duration_prd) as rn
            ,count(1) over(partition by t1.api_path )      as cn
            ,t1.api_path
            ,t1.duration_prd
        from api_request_config_performance t1
    ) t2
    group by
        api_path
) t2
    on t1.api_path = t2.api_path