简体   繁体   English

获取重复项真的是一个糟糕的查询还是糟糕的数据库设计?

[英]Get duplicates really is it a bad query or bad database design?

I have read this article 我读过这篇文章

In the second paragrpah says this:在第二段中说:

"The fact that the resultset has duplicates is frequently (though not always) the result of a poor database design, an ineffective query, or both". “结果集有重复的事实经常(尽管并非总是)是数据库设计不佳、查询无效或两者兼而有之的结果”。

Later use as example the Adventures database, that I guess that it has a good design.后来以 Adventures 数据库为例,我猜它有一个很好的设计。

Well, my doubt is this case.好吧,我怀疑是这种情况。 I have to tables, Persons and Orders and I want to get all persons that has at least one order which total >= $200.我必须表,人员和订单,我想得到所有至少有一个订单总额 >= 200 美元的人。 I would use this query:我会使用这个查询:

Select Persons.* from Persons, Orders where
Orders.IDPerson = Persons.IDPerson
and Orders.Total >= 200;

In this case I can get many times the same person because has more than one order which total is 200 or more.在这种情况下,我可以多次获得同一个人,因为有多个订单,总数为 200 或更多。 Really I want each person once in the results, so is this query a bad query because a I can get the same person many times?我真的希望每个人都在结果中出现一次,所以这个查询是一个糟糕的查询,因为我可以多次得到同一个人吗?

Another option is this query:另一种选择是这个查询:

select * from Person where
IDPerson IN(select IDPerson from Orders where total >= 200);

In this case I get only once each person, although this person has more than one order with a total >= 200. But use a subquery to avoid duplicates in the main query is it a good idea?在这种情况下,我每个人只得到一次,尽管这个人有多个订单,总数 >= 200。但是使用子查询来避免主查询中的重复是个好主意吗?

In this case, Persons and orders, I guess that the database design is not bad, because I don't know which other option I have to design this model, and the query I guess is very simple, but I have the doubt if in this case to get duplicates it is a sign of bad query.在这种情况下,Persons和orders,我猜数据库设计还不错,因为我不知道我还有哪个选项来设计这个模型,而且我猜的查询很简单,但我怀疑是否在这种情况下会出现重复项,这是错误查询的标志。

In sumary, get duplicates is a bad query in this case?总之,在这种情况下,获取重复项是一个糟糕的查询吗?

Thanks.谢谢。

I think the first query is bad like this.我认为第一个查询很糟糕。 Seems not useful to get duplicates which you later need to remove by using DISTINCT.获取重复项似乎没有用,以后需要使用 DISTINCT 删除这些重复项。

The seconds query with the sub-query seems more useful in the context (maybe sometimes it makes more sense to use "exists" rather than "in").带有子查询的秒查询在上下文中似乎更有用(也许有时使用“exists”而不是“in”更有意义)。

SQL Server IN vs. EXISTS Performance SQL Server IN 与 EXISTS 性能

Such query would also be possible as well:这样的查询也是可能的:

select * 
from Person
join
(
    select IDPerson
    from Orders 
    where total >= 200  
) PersonsWithMoreThan200Total
on Person.IDPerson = PersonsWithMoreThan200Total.IDPerson

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

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