PSQL 请求太慢。如何修复它?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
WITH
是可能的。将所有查询移至WITH
并进行一些修改:将COUNT(cid)
更改为COUNT(DISTINCT cid)
并删除GROUP BY
子句根本没有。it is possible with using
WITH
. Move all queries toWITH
and modify them a little: changeCOUNT(cid)
toCOUNT(DISTINCT cid)
and removeGROUP BY
clause at all.您可以使用类似这样的内容:
此查询不按 cid 分组,但可能您不需要它。
You could use something like this:
this query doesn't grouping by cid, but probably you don't need it.