简化 H2 数据库中的查询 - TOP X PERCENT 的替代方案

发布于 2024-12-04 17:37:46 字数 1139 浏览 0 评论 0原文

我的查询遇到性能问题,想知道如何简化它。

我有一个“评估”表(样本、类别、评审团、价值) 并创建了一些自定义函数来获取每个样本的一些平均值,所以我有这样的观点:

CREATE VIEW Results AS 
SELECT  Sample, 
        Category,
        IFNULL(COUNT_VALID(Value),0) || ' / ' || COUNT(Value) AS Valid,
        CUSTOM_MEAN(Value) AS Mean,
        CUSTOM_MEDIAN(Value) AS Median
FROM Evaluations GROUP BY Sample, Category;

然后我想要另一个字段告诉我每个样本是否在其类别中最有价值样本的 30% 之内。使用 TOP(X) PERCENT 是完美的,但似乎 H2 不支持它,所以我做了第二个视图,计算类别中的位置乘以 100,除以类别中的总计数并与 30 进行比较:

CREATE VIEW Res AS 
SELECT  R1.*,

        CASE 
            WHEN ( 
                ((SELECT COUNT(*) FROM Results R2 
                    WHERE R2.Category = R1.Category 
                    AND (R2.Mean > R1.Mean OR (R2.Mean = R1.Mean AND R2.Median > R1.Median))) + 1) * 100 
                / 
                (SELECT COUNT(*) FROM Results R2 WHERE R2.Category = R1.Category) ) 
                > 30
        THEN 'over 30%'
        ELSE 'within 30%'
        END as 30PERCENT

FROM Results R1 ORDER BY Mean DESC, Median DESC;

这有效正确,但只有 500 条记录,检索结果需要一些时间。 有人可以告诉我构建此查询的更有效方法吗?

谢谢和问候!

I'm having performance issues with a query and was wondering how to simplify it.

I have a table "Evaluations" (Sample, Category, Jury, Value)
And created some custom functions to get some average values for each sample, so I have this view:

CREATE VIEW Results AS 
SELECT  Sample, 
        Category,
        IFNULL(COUNT_VALID(Value),0) || ' / ' || COUNT(Value) AS Valid,
        CUSTOM_MEAN(Value) AS Mean,
        CUSTOM_MEDIAN(Value) AS Median
FROM Evaluations GROUP BY Sample, Category;

Then I want to have another field telling me if each sample is within the 30% of best valued samples of its category. It would be perfect to use TOP(X) PERCENT but it seems H2 doesn't support it so I made a second view that calculates the position in category multiplied by 100, divided by the total count in category and compared to 30:

CREATE VIEW Res AS 
SELECT  R1.*,

        CASE 
            WHEN ( 
                ((SELECT COUNT(*) FROM Results R2 
                    WHERE R2.Category = R1.Category 
                    AND (R2.Mean > R1.Mean OR (R2.Mean = R1.Mean AND R2.Median > R1.Median))) + 1) * 100 
                / 
                (SELECT COUNT(*) FROM Results R2 WHERE R2.Category = R1.Category) ) 
                > 30
        THEN 'over 30%'
        ELSE 'within 30%'
        END as 30PERCENT

FROM Results R1 ORDER BY Mean DESC, Median DESC;

This works properly but with just 500 records it takes some time to retrieve the results.
Could someone tell me a more efficient way of constructing this query?

Thanks and regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文