查询以获取对象图的所有修订
我正在数据库上实现审核日志,因此所有内容都有 CreatedAt 和 RemovedAt 列。 现在我希望能够列出对象图的所有修订,但我能想到的最好方法是使用联合。 我需要获取每个唯一的CreatedAt 和RemovedAt id。
如果我得到一个有省份的国家列表,那么联盟看起来像这样:
SELECT c.CreatedAt AS RevisionId from Countries as c where localId=@Country
UNION
SELECT p.CreatedAt AS RevisionId from Provinces as p
INNER JOIN Countries as c ON p.CountryId=c.LocalId AND c.LocalId = @Country
UNION
SELECT c.RemovedAt AS RevisionId from Countries as c where localId=@Country
UNION
SELECT p.RemovedAt AS RevisionId from Provinces as p
INNER JOIN Countries as c ON p.CountryId=c.LocalId AND c.LocalId = @Country
对于更复杂的查询,这可能会变得非常复杂,并且可能表现得很差,所以我想看看是否有人能想到更好的方法。 这是在 MSSQL Server 中。
我需要将它们全部放在一个列表中,因为这是在 from 子句中使用的,而真正的数据来自于此的连接。
I'm implementing an audit log on a database, so everything has a CreatedAt and a RemovedAt column. Now I want to be able to list all revisions of an object graph but the best way I can think of for this is to use unions. I need to get every unique CreatedAt and RemovedAt id.
If I'm getting a list of countries with provinces the union looks like this:
SELECT c.CreatedAt AS RevisionId from Countries as c where localId=@Country
UNION
SELECT p.CreatedAt AS RevisionId from Provinces as p
INNER JOIN Countries as c ON p.CountryId=c.LocalId AND c.LocalId = @Country
UNION
SELECT c.RemovedAt AS RevisionId from Countries as c where localId=@Country
UNION
SELECT p.RemovedAt AS RevisionId from Provinces as p
INNER JOIN Countries as c ON p.CountryId=c.LocalId AND c.LocalId = @Country
For more complicated queries this could get quite complicated and possibly perform very poorly so I wanted to see if anyone could think of a better approach. This is in MSSQL Server.
I need them all in a single list because this is being used in a from clause and the real data comes from joining on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您很可能已经实施了您的解决方案,但要解决一些问题; 我建议考虑 Aleris 的解决方案或其某些衍生方案。
日志中包含的字段由您决定,但 Aleris 的解决方案是直接的。 我可能会创建一个操作表并将字段类型从 varchar 更改为 int,作为操作表的链接——迫使开发人员执行一些标准化操作。
希望有帮助。
You have most likely already implemented your solution, but to address a few issues; I would suggest considering Aleris's solution, or some derivative thereof.
The fields contained in your log are up to you but Aleris's solution is direct. I may create an action table and change the field type from varchar to int, as a link into the action table -- forcing the developers to some standardized actions.
Hope it helps.
另一种方法是创建一个可能如下所示的审核日志:
其中 EntityName 是表的名称(例如:Contries、Provinces),Action 是审核操作(例如:Created、Removed 等),EntityId 是原始表中修改行的主键。
该表需要在对表执行的每个操作上保持同步。 有几种方法可以做到这一点:
1) 在每个表上创建触发器,将行添加到 AuditTable
2) 每次对相应表进行更改时,从应用程序中在 AuditTable 中添加行
使用此解决方案可以非常简单地获取审核中的日志列表。
如果您需要从原始表中获取列,也可以使用如下连接:
An alternative would be to create an audit log that might look like this:
where EntityName is the name of the table (eg: Contries, Provinces), the Action is the audit action (eg: Created, Removed etc) and the EntityId is the primary key of the modified row in the original table.
The table would need to be kept synchronized on each action performed to the tables. There are a couple of ways to do this:
1) Make triggers on each table that will add rows to AuditTable
2) From your application add rows in AuditTable each time a change is made to the repectivetables
Using this solution is very simple to get a list of logs in audit.
If you need to get columns from original table is also possible using joins like this:
您可能可以通过交叉连接和合并来完成此操作,但从性能的角度来看,联合可能仍然更好。 不过,您可以尝试分别进行测试。
You could probably do it with a cross join and coalesce, but the union is probably still better from a performance standpoint. You can try testing each though.