gpt4 book ai didi

sql - 有没有办法减少我的 SQL 查询的大小?

转载 作者:行者123 更新时间:2023-11-29 12:15:15 26 4
gpt4 key购买 nike

我试图在 1 个 SQL 查询中从 Stores、Offers、Products 和 Jobs 表中获取我需要的所有详细信息。

数据库关系:

  • 提供 belongs_to 商店
  • 提供 has_many 产品
  • 提供 has_many 工作

问题是:我想对不同的工作状态进行工作计数。

下面的查询有效,但我不喜欢它有多大,并且这部分有多个副本:WHERE "jobs"."offer_id"= "offers"“编号”。有没有办法使这个查询优化并且可能更小?

        SELECT
"offers".*,
"stores"."name" AS store_name,
(
SELECT COUNT(*)
FROM "products"
WHERE "products"."offer_id" = "offers"."id"
) AS products_count,
(
SELECT COUNT(*)
FROM "jobs"
WHERE "jobs"."offer_id" = "offers"."id"
) AS jobs_count,
(
SELECT COUNT(*)
FROM "jobs"
WHERE "jobs"."offer_id" = "offers"."id" AND "jobs"."status" = 1
) AS jobs_in_progress_count,
(
SELECT COUNT(*)
FROM "jobs"
WHERE "jobs"."offer_id" = "offers"."id" AND "jobs"."status" = 2
) AS jobs_done_count,
(
SELECT COUNT(*)
FROM "jobs"
WHERE "jobs"."offer_id" = "offers"."id" AND "jobs"."status" = 3
) AS jobs_error_count
FROM "offers"
LEFT JOIN "stores" ON "stores"."id" = "offers"."store_id"
GROUP BY "offers"."id", "stores"."name"
ORDER BY "offers"."created_at" DESC
        SELECT
"offers".*,
"stores"."name" AS store_name,
products.products_count,
jobs.jobs_count,
jobs.jobs_in_progress_count,
jobs.jobs_done_count,
jobs.jobs_error_count
FROM "offers"
LEFT JOIN "stores" ON "stores"."id" = "offers"."store_id"
LEFT JOIN (
SELECT p.offer_id,
COUNT(*) AS products_count
FROM "products" p
GROUP BY p.offer_id
) "products" ON "products"."offer_id" = "offers"."id"
LEFT JOIN (
SELECT j.offer_id,
COUNT(*) AS jobs_count,
COUNT(*) FILTER (WHERE j.status = 1) AS jobs_in_progress_count,
COUNT(*) FILTER (WHERE j.status = 2) AS jobs_done_count,
COUNT(*) FILTER (WHERE j.status = 3) AS jobs_error_count
FROM "jobs" j
GROUP BY j.offer_id
) "jobs" ON "jobs"."offer_id" = "offers"."id"
GROUP BY "offers"."id", "stores"."name", "products"."products_count", "jobs"."jobs_count", "jobs"."jobs_in_progress_count", "jobs"."jobs_done_count", "jobs"."jobs_error_count"
ORDER BY "offers"."created_at" DESC

最佳答案

您可以使用条件聚合来计算不同的作业:

SELECT "offers".*,
"stores"."name" AS store_name,
(
SELECT COUNT(*)
FROM "products"
WHERE "products"."offer_id" = "offers"."id"
) AS products_count,
count(jobs.offer_id) as jobs_count,
count(jobs.offer_id) filter (where jobs.status = 1) as jobs_in_progress_count,
count(jobs.offer_id) filter (where jobs.status = 2) as jobs_done_count,
count(jobs.offer_id) filter (where jobs.status = 3) as jobs_error_count
FROM "offers"
LEFT JOIN "stores" ON "stores"."id" = "offers"."store_id"
LEFT JOIN jobs ON jobs.offer_id = offers.id
GROUP BY "offers"."id", "stores"."name"
ORDER BY "offers"."created_at" DESC;

先聚合,然后加入其结果可能会更快:

SELECT "offers".*,
"stores"."name" AS store_name,
(
SELECT COUNT(*)
FROM "products"
WHERE "products"."offer_id" = "offers"."id"
) AS products_count,
jobs.jobs_count,
jobs.jobs_in_progress_count,
jobs.jobs_done_count,
jobs.jobs_error_count
FROM "offers"
LEFT JOIN "stores" ON "stores"."id" = "offers"."store_id"
LEFT JOIN (
SELECT j.offer_id,
count(*) as jobs_count,
count(*) filter (where j.status = 1) as jobs_in_progress_count,
count(*) filter (where j.status = 2) as jobs_done_count,
count(*) filter (where j.status = 3) as jobs_error_count
FROM jobs j
group by j.offer_id
) jobs ON jobs.offer_id = offers.id
ORDER BY "offers"."created_at" DESC;

这也可以用于计算产品:

SELECT "offers".*,
"stores"."name" AS store_name,
prod.products_count,
jobs.jobs_count,
jobs.jobs_in_progress_count,
jobs.jobs_done_count,
jobs.jobs_error_count
FROM "offers"
LEFT JOIN "stores" ON "stores"."id" = "offers"."store_id"
LEFT JOIN (
SELECT j.offer_id,
count(*) as jobs_count,
count(*) filter (where j.status = 1) as jobs_in_progress_count,
count(*) filter (where j.status = 2) as jobs_done_count,
count(*) filter (where j.status = 3) as jobs_error_count
FROM jobs j
group by j.offer_id
) jobs ON jobs.offer_id = offers.id
LEFT JOIN (
select p.offer_id, count(*)
from products p
group by p.offer_id
) prod on prod.offer_id = offers.id
ORDER BY "offers"."created_at" DESC;

关于sql - 有没有办法减少我的 SQL 查询的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57902077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com