PSQL 请求太慢。如何修复它?

发布于 2024-11-23 15:09:04 字数 632 浏览 3 评论 0原文

SELECT 
    COUNT(a)/COUNT(s)*100 as aratio, 
    COUNT(b)/COUNT(s)*100 as bratio, 
    COUNT(c)/COUNT(s)*100 as cratio, 
    COUNT(a), 
    COUNT(b), 
    COUNT(c), 
    COUNT(s) 
FROM 
    (SELECT COUNT(cid) as a FROM images WHERE width > height AND category_id = 4 GROUP BY cid) as aq, 
    (SELECT COUNT(cid) as b FROM images WHERE width < height AND category_id = 4 GROUP BY cid) as bq, 
    (SELECT COUNT(cid) as c FROM images WHERE width = height AND category_id = 4 GROUP BY cid) as cq, 
    (SELECT COUNT(cid) as s FROM images WHERE category_id = 4 GROUP BY cid) as sq;

我怎样才能使这个请求更有效?

SELECT 
    COUNT(a)/COUNT(s)*100 as aratio, 
    COUNT(b)/COUNT(s)*100 as bratio, 
    COUNT(c)/COUNT(s)*100 as cratio, 
    COUNT(a), 
    COUNT(b), 
    COUNT(c), 
    COUNT(s) 
FROM 
    (SELECT COUNT(cid) as a FROM images WHERE width > height AND category_id = 4 GROUP BY cid) as aq, 
    (SELECT COUNT(cid) as b FROM images WHERE width < height AND category_id = 4 GROUP BY cid) as bq, 
    (SELECT COUNT(cid) as c FROM images WHERE width = height AND category_id = 4 GROUP BY cid) as cq, 
    (SELECT COUNT(cid) as s FROM images WHERE category_id = 4 GROUP BY cid) as sq;

How can i make this request more effective?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攒一口袋星星 2024-11-30 15:09:05

使用 WITH 是可能的。将所有查询移至 WITH 并进行一些修改:将 COUNT(cid) 更改为 COUNT(DISTINCT cid) 并删除 GROUP BY 子句根本没有。

it is possible with using WITH. Move all queries to WITH and modify them a little: change COUNT(cid) to COUNT(DISTINCT cid) and remove GROUP BY clause at all.

揽月 2024-11-30 15:09:05

您可以使用类似这样的内容:

SELECT 
    SUM(CASE (width > height) WHEN true THEN 1 ELSE 0 END)::float8*100/COUNT(*) as aratio, 
    SUM(CASE (width < height) WHEN true THEN 1 ELSE 0 END)::float8*100/COUNT(*) as bratio, 
    SUM(CASE (width = height) WHEN true THEN 1 ELSE 0 END)::float8*100/COUNT(*) as cratio, 
    SUM(CASE (width > height) WHEN true THEN 1 ELSE 0 END), 
    SUM(CASE (width < height) WHEN true THEN 1 ELSE 0 END), 
    SUM(CASE (width = height) WHEN true THEN 1 ELSE 0 END), 
    COUNT(*) 
FROM 
    images WHERE category_id = 4;

此查询不按 cid 分组,但可能您不需要它。

You could use something like this:

SELECT 
    SUM(CASE (width > height) WHEN true THEN 1 ELSE 0 END)::float8*100/COUNT(*) as aratio, 
    SUM(CASE (width < height) WHEN true THEN 1 ELSE 0 END)::float8*100/COUNT(*) as bratio, 
    SUM(CASE (width = height) WHEN true THEN 1 ELSE 0 END)::float8*100/COUNT(*) as cratio, 
    SUM(CASE (width > height) WHEN true THEN 1 ELSE 0 END), 
    SUM(CASE (width < height) WHEN true THEN 1 ELSE 0 END), 
    SUM(CASE (width = height) WHEN true THEN 1 ELSE 0 END), 
    COUNT(*) 
FROM 
    images WHERE category_id = 4;

this query doesn't grouping by cid, but probably you don't need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文