Databases: Making a Log of actions, how to handle various references?

后端 未结 4 1060
离开以前
离开以前 2021-02-06 11:44

hope you all had a happy new year.

So, my question is, what\'s the best way to make a log of actions. Let me explain it with a example, suppose we have these entities:

4条回答
  •  时光说笑
    2021-02-06 12:31

    1. Keep it simple and extendible
    2. Don't let translation overhead affect performance, translation only needs to be done for output purposes.

    Suggestion:

     LogId       DateTime   Action   Role  Entity
    
     e.g.
    
     30303     1/1/10    43        Sender   John
     30303     1/1/10    43        Receiver Sam
     30304     1/1/10    44        Game      game43
     30304     1/1/10    44        Player    Sue
     30304     1/1/10    44        Player    Mike
    

    (In the above table, "Message", ,"Sender", "John", "game43" etc. would not be text but would be foreign keys in either the action, role, or entity table. I've written the keys for "Action" but not for "Role" or "Entity" but they would be keys as well.

    Now, instead of text action, Role, Entity you might have keys in there, and store them in a separate table. This can be used for output, e.g.

     Action Table
    
     Id    ActionKey   Text      Language
     1     43          JoinGame  English
     2     43          Jeu       French
     3     44          Message   English
     ...
     ...
    
     Role Table
     Id   RoleKey  Text  Language
     1    1        Sender   English
     2    1        Sendeur  French        (I don't know french :)
     ....
    
     Entity Table
     EntityKey   Text
     1           Sam
     2           game43
     3           Sam
    

    Note that in the entity table, 2 or more entries might have the same text representation, as there could be more than 1 user named Sam. If you want to represent different, orthogonal information about each entity, then you can include the EntityKey in the correspondig table, e.g.

    Person Table
    Id    EntityKey   FirstName  LastName ....
    1      1          Sam         Johnson
    
    Game Instance Table
    Id    EntityKey   GameType  
    1     2           444
    
    Game Table
    Id   Name   MaxPlayers ...
    444  Quake    10
    

    Basically we are mapping a predicate (in the first-order predicate logic sense, a set of tuples) to binary relation form (by creating an artificial entity, the action key). Thus, the Entity table basically contains various columns/arguments of the relation, and so it can be practically anything which could be an argument of the relation. The advantage of this representation is it is infinitely extendible to new relations that you may wish to log without changing the schema. Everything in the ActionTable should be a key or foreign key, I just haven't put that in there because it may be harder to read.

提交回复
热议问题