简体   繁体   English

优化SQL查询,需要建议

[英]Optimize SQL Query, need suggestions

I have a table in SQL Server having 4 columns:我在 SQL 服务器中有一张表,有 4 列:

Invoice No, Date, Amt and ID

I have to find invoices that have same Invoice No, date and Amt but different ID.我必须找到具有相同发票编号、日期和金额但 ID 不同的发票。 I'm populating the results doing self join but seems like it's not the optimized way to fetch results.我正在填充执行自加入的结果,但似乎它不是获取结果的优化方式。

My query:我的查询:

select * from table t1 join 
table t2 on t1.invoice = t2.invoice 
where t1.invoice=t2.invoice and t1.amount=t2.amount and t1.date =t2.date and t1.id!=t2.id

Kindly suggest me an optimized way to fetch the correct result.请建议我一种优化的方法来获取正确的结果。

try this.尝试这个。 using left join and filter those nulls.使用左连接并过滤那些空值。

select * from (
    select t1.invoiceno, t1.date, t1.amt, t1.id, t2.id as t2ID
    from invoices t1
    left join invoices t2 on t2.invoiceno = t1.invoiceno 
        and t2.date = t1.date 
        and t2.amt = t1.amt
        and t2.id != t1.id) t3 
where coalesce(t3.t2ID, 0) != 0

You might use indexes to speed up the retrieving from large databases.您可以使用索引来加快从大型数据库中检索的速度。 Use sub query but don't use a sub query just to show one column.使用子查询,但不要仅使用子查询来显示一列。

I advised to use sub query as new table to use joins.我建议使用子查询作为新表来使用连接。 just like the first answer.就像第一个答案一样。

use not exists使用不存在

select t1.* from table t1 
where not exists( select 1 form 
table t2 where t1.invoice = t2.invoice 
and t1.invoice=t2.invoice and t1.amount=t2.amount 
and t1.date =t2.date and   t1.id=t2.id
having count(*)>1
)

have to find invoices that have same Invoice No, date and Amt but different ID.必须找到具有相同发票编号、日期和金额但 ID 不同的发票。

Use exists :使用exists

select t.*
from t
where exists (select 1
              from t t2
              where t2.Invoice = t.invoice and
                    t2.Date = t.date and
                    t2.amount = t.amount and
                    t2.id <> t.id
             )
order by t.invoiceNo, t.date, t.amount, t.id;

This will show the matching invoices on adjacent rows.这将在相邻行上显示匹配的发票。 For performance, you want an index on (invoice, date, amount, id) .对于性能,您需要(invoice, date, amount, id)上的索引。

If you just want triplets where this occurs, you can use aggregation:如果您只想要发生这种情况的三元组,则可以使用聚合:

select invoice, date, amount, min(id), max(id)
from t
group by invoice, date, amount
having count(distinct id) > 1;

Note: If there are more than two duplicates, this only shows two ids.注意:如果有两个以上的重复,这只会显示两个 id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM