简体   繁体   English

SQL Server TPH(每个层次结构的表)基于类型自动增加多个列

[英]SQL Server TPH (Table Per Hierarchy) auto increment multiple columns base on type

We currently use TPT (Table Per Type) in Entity Framework, this is very slow as we have about 20 tables, when they are queried, Entity Framework creates some massive disguising SQL which is very slow. 当前,我们在实体框架中使用TPT(每种类型的表),这非常慢,因为我们有大约20个表,当查询它们时,实体框架会创建一些大规模的伪装SQL,这非常慢。

Each table has an auto increment integer column, this allows each type to have a number that is incremented per type. 每个表都有一个自动递增整数列,这使每种类型都有一个按类型递增的数字。 This is what the clients wanted. 这就是客户想要的。 Now that we are wanting to move to the more performant TPH, we need all these table columns moved to the one table. 现在,我们希望移至性能更高的TPH,我们需要将所有这些表列移至一个表中。

How can we have the auto increment columns based on the type as in the results below? 我们如何根据类型创建自动递增列,如以下结果所示?

eg 例如

Current Job Task 目前的工作任务

| TaskId    | TaskNumber    |
-----------------------------
| 1234      | 1             |
| 2345      | 2             |

Current Work Task 当前工作任务

| TaskId    | TaskNumber    |
-----------------------------
| 3244      | 1             |
| 3245      | 2             |

This is the TPH table structure we want, as you can see, we want the task number to increment based on the Type of task. 如您所见,这就是我们想要的TPH表结构,我们希望任务编号根据任务类型增加。

| TaskId    | Type      | JobTaskNumber    | WorkTaskNumber   |
---------------------------------------------------------------
| 1234      | Job       | 1                | null             |
| 2345      | Job       | 2                | null             |
| 3244      | Work      | null             | 1                |
| 3245      | Work      | null             | 2                |

I am wondering if we use a seeding table, but any solutions greatly appreciated 我想知道我们是否使用播种台,但是任何解决方案都值得赞赏

Many thanks 非常感谢

Andrew 安德鲁

OK so did what I thought would work. 好的,我认为可行。

Not a hugely nice approach as we need about 20 seed tables.Each table has just an identity id defined as a BIGINT in sql server 这不是一个非常好的方法,因为我们需要大约20个种子表。每个表只有一个标识ID,在SQL Server中定义为BIGINT

When we want to add and get a new incremented id we just call this using dapper to get the result. 当我们要添加并获取新的递增ID时,只需使用dapper调用它即可获得结果。

INSERT INTO SeedMyTable DEFAULT VALUES; SELECT CAST(SCOPE_IDENTITY() AS BIGINT)

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

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