sql - 如何更好的优化SQL查询?

标签 sql postgresql

我写了请求,但我担心它可能非常未优化,我会遇到问题。

任务:查找没有债务且上次付款时间超过 2 年的用户。没有任何订单的不必要的用户

我们有两个表:

 User tbl (Id (int), LastName (string))
 Order tbl (Id (int), UserId (int), IsPaid (bool), DatePaid (Date, not null))

I wrote a Sql request, but I'm afraid I'm down with 20k of users and lots of orders

I have found all those who have no debts and the last payment is for a period of two years. Now I want to remove them from the general list and so users who need me remain.
It seems bad idea

SELECT u."Id"
FROM "User" AS u
LEFT JOIN
   (SELECT *
    FROM "Order"
    WHERE "UserId" IN
       (SELECT "Id"
        FROM "User"
        WHERE "Id" NOT IN
           (SELECT DISTINCT "UserId"
            FROM "Order"
            WHERE "IsPaid" IS FALSE )
       )
    AND "DatePaid" > '2016-10-10'
   ) AS p
   ON p."UserId" = u."Id";

最佳答案

我想你只是想要这样的东西:

select o.userid
from orders o
group by o.userid
having sum(case when o.isPaid then 1 else 0 end) = count(*) and -- all are paid
       max(o.paymentdate) < current_date - interval '2 year';

请注意,众所周知,日期函数是特定于数据库的。以上使用 ANSI/ISO 标准语法。

如果您想要有关用户的完整信息,则可以使用不同的结构:

select u.*
from users u
where not exists (select 1
                  from orders o
                  where o.userid = u.id and not u.ispaid
                 ) and
      not exists (select 1
                  from orders o
                  where o.userid = u.id and
                        u.paymentdate > current_date - interval '2 year'
                 );

关于sql - 如何更好的优化SQL查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52700010/

相关文章:

mysql - 具有低基数第二列的复合索引是否足以影响性能以至于应该使用它?

mysql - 返回距当前日期一年多以前的日期的 SQL 查询

sql - 查看 SQL Server 执行日志,非常详细以用于调试

php - 我可以写入 SQL,但读取时一无所获

sql - 将 postgreSQL 中的多行合并为一行?

postgresql - pg_ctl : error while loading shared libraries: libpq. so.5

ruby-on-rails - 在 jsonb postgres 中按日期排序

postgresql - Amazon RDS Postgres -> 授予 pg_catalog 权限

postgresql - 将 json 字符串从 spring 应用程序插入到 postgres jsonb

c# - 显示来自 SQL 的数据时出错