如何从相关表中返回尽可能少的行

发布于 2024-09-11 21:58:37 字数 1149 浏览 3 评论 0原文

我是 ADO.net 新手,遇到这个问题:

假设我在 SQL Server 2005 数据库中有这两个表,其中包含以下列:

[Orders]

  • OrderID
  • OrderDate
  • ShopID
  • TotalAmount
  • TotalTaxAmount
  • 等...

[OrdersDetails]

  • OrderID
  • ShopID
  • ItemID
  • Quantity
  • Amount
  • TaxAmount

我已经启动了一个 WinForms 应用程序来开始。 在此表单中,用户可以选择商店列表并选择日期范围以查看该商店的所有订单。

我从 Visual Studio 添加了一个数据源,选择 Orders 和 OrdersDetails 表,并将 Orders 和相关 OrdersDetails 表拖放到 DataGridViews 表单中。

当我从 Orders DataGridView 中选择一行时,我确实在第二个 DataGridView 中看到了我想要的相应订单详细信息。我在这个数据库中建立了关系,ADO.net 捕获了它们并将它们反映在数据集中。

然后,我向键入的数据集添加了一个方法,以按 OrderDate 和 ShopID 列获取数据。 由于 OrdersDetails 表没有 OrderDate 列,因此我只能按 ShopID 对其进行过滤。

问题是从 OrdersDetails 获取记录非常耗时,因为它将检索到 OrdersDetails 数据表中超出所需的行数。 问题是我只能按 ShopID 过滤 OrderDetails 表中的行,这会从数据库返回太多记录。

显然,ADO.net 能够通过使用 OrderID 关系在客户端适当地过滤它们,但仅从 OrdersDetails 中检索我实际需要的行会更有意义。

我已经修改了从第二个表获取数据的查询,以使用联接添加 OrderDate,因此当我从数据库检索数据时,我可以按日期进行过滤...但是,当我尝试更新我的更改时,它会导致问题到这个外国专栏...

我相信一定有一个简单的方法来解决这个问题,不是吗?

预先非常感谢。

I am new to ADO.net and have this problem:

Let's assume I have these two tables in a SQL Server 2005 database, with these columns:

[Orders]

  • OrderID
  • OrderDate
  • ShopID
  • TotalAmount
  • TotalTaxAmount
  • etc...

[OrdersDetails]

  • OrderID
  • ShopID
  • ItemID
  • Quantity
  • Amount
  • TaxAmount
  • etc

I have started a WinForms application to get myself started.
In this form, the user can select a list of Shops and select a date range to see all orders from this shop.

I have added a data source from Visual Studio, select both Orders and OrdersDetails table and dragged and dropped the Orders and related OrdersDetails tables into the form as DataGridViews.

When I select a row from the Orders DataGridView, I indeed see the corresponding Orders Details in the second DataGridView as I wanted. I had relationships inside this database and ADO.net caught them up and reflected them in the dataset.

I have then added a method to my typed dataset to get data by the OrderDate, and ShopID column.
As the OrdersDetails table does not have an OrderDate column, I could only filter it by ShopID.

The issue is that it is time consuming to get the records from the OrdersDetails as it will retrieve more rows than needed into the DataTable for the OrdersDetails.
The problem is that I can only filter the rows from the OrderDetails table by ShopID, which returns way too many records from the database.

Obviously, ADO.net is able to filter them appropriately on the client-side by using the OrderID relationship but it would make much more sense to retrieve only the rows from the OrdersDetails that I actually need.

I have modified my queries getting the data from the second table to add the OrderDate using a join, so I can filter by date when I retrieve the data from the database... however, it causes problems when I try to update my changes due to this foreign column...

I believe there must be an easy way around this, isn't there?

Thanks a lot in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫性小仙女 2024-09-18 21:58:37

你想做这样的事情

SELECT *
FROM OrderDetails
WHERE
    ShopID IN ( @listOfShopIds )
    AND
    OrderID IN (
        SELECT OrderID
        FROM Orders
        WHERE
            OrderDate BETWEEN @dateFrom AND @dateTo
    )

You want to do something like this

SELECT *
FROM OrderDetails
WHERE
    ShopID IN ( @listOfShopIds )
    AND
    OrderID IN (
        SELECT OrderID
        FROM Orders
        WHERE
            OrderDate BETWEEN @dateFrom AND @dateTo
    )
夕色琉璃 2024-09-18 21:58:37

@David,最好编写表达您的意图并反映性能更高的算法的代码,而不是依赖引擎的实现细节来执行优化。

@David it's probably better to write code which expresses your intent and reflects the more performant algorithm, rather than relying on implementation details of the engine to perform the optimization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文