在不损失性能的情况下保持发票金额正确?

Keep invoice sum right without losing performance?(在不损失性能的情况下保持发票金额正确?)
本文介绍了在不损失性能的情况下保持发票金额正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子...

CREATE TABLE [dbo].[Invoice](
    [InvoiceID] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
    [TotalSumBeforeTax] [decimal](12, 2) NOT NULL,
    [TotalSumAfterTax] [decimal](12, 2) NOT NULL
)

CREATE TABLE [dbo].[InvoiceItem](
    [InvoiceItemID] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
    [InvoiceID] [uniqueidentifier] NOT NULL,
    [AmountBeforeTax] [decimal](12, 2) NOT NULL,
    [AmountAfterTax] [decimal](12, 2) NOT NULL
) 

发票表保存了所有引用该发票的 InvoiceItems 的计算总和.

The Invoice table holds a calculated sum of all the InvoiceItems that refer to that Invoice.

我需要帮助找出最佳做法,以在不损失性能的情况下保持发票金额正确.

I need help figuring out the best practice to keep the Invoice sum correct without losing performance.

计算列?

如果我对 Invoice 表中的 TotalSumBeforeTaxTotalSumAfterTax 使用计算列,则通过 InvoiceItem 表中的函数获取值,它不会持久化并且会很慢,因为我在 InvoiceItem 表中有数十万行.

If I use a calculated column for TotalSumBeforeTax and TotalSumAfterTax in the Invoice table, that fetches values via a function from the InvoiceItem table, it's not persisted and will be very slow, since I have hundreds of thousands of rows in the InvoiceItem table.

触发?

我还考虑在 InvoiceItem 表上使用触发器,然后更新 Invioce TotalSumBeforeTax TotalAfterBeforeTax 列.

I also thought about using a trigger on the InvoiceItem table and then updating the Invioce TotalSumBeforeTax TotalAfterBeforeTax columns.

怎么办?

在列出 Invoice 表的多行时,有没有办法始终保持 TotalSumBeforeTaxTotalSumAfterTax 列最新,同时对性能的影响最小?

Is there a way to always keeping the TotalSumBeforeTax and TotalSumAfterTax columns up-to-date with minimal impact on performance when listing many rows of the Invoice table?

推荐答案

我认为您根本不应该保留这些列.只需在 InvoiceItem 表上创建一个索引,该索引将具有 InvoiceID 并包含 AmountBeforeTaxAmountAfterTax 列,并且需要时使用简单的 sumgroup by 查询进行计算:

I would argue that you shouldn't keep these columns at all. Just create an index on the InvoiceItem table that will have the InvoiceID and include the AmountBeforeTax and AmountAfterTax columns, and calculate when needed using a simple sum and group by query:

SELECT InvoiceID
    , SUM(AmountBeforeTax) AS TotalSumBeforeTax
    , SUM(AmountAfterTax) AS TotalSumAfterTax
FROM dbo.InvoiceItem
GROUP BY InvoiceID

根据经验,不应该存储容易计算的东西 - 因为计算比同步存储的值更容易.

As a rule of thumb, things that can easily be computed shouldn't be stored - because it's easier to compute than to synchronize the stored values.

顺便说一句,如果涉及税收,不应该是price而不是amount吗?

BTW, if there's a tax involved shouldn't it be price and not amount?

这篇关于在不损失性能的情况下保持发票金额正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)