dongpao5658 2018-04-28 07:42
浏览 14

在数据库中存储用于用户活动日志的IP

I'm building a sort of login system for my website and I want to log user's activity in the database. The problem is with storing the IPs of the user each time.

I have the users table with all the user's details, such as username, pass etc. But if I'll add another column with the IPs, the only way to store it(that I am aware) is JSON kind of format(the TEXT one). So it would be something like:

username: theuser password: thepassword etc... IPs: { '27/04/2018 13:05': '127.0.0.1', '28/04/2018 16:28': '127.0.0.2', etc }

But...

1) Is it efficient to store IPs like that?

2) If yes, then how can I update each time the date and IP, so it won't erase the previous ones?

3) If instead of using the same table, I will create a new one, say "users_IPs", and then will store the user-id there, and then will use the same TEXT(JSON-like format) to store the IPs, would that be more efficient?

If you think there's a better way of dealing with this kind of thing, you're most welcome to share you thoughts.

  • 写回答

1条回答 默认 最新

  • douju8113 2018-04-28 16:55
    关注

    Storing logs as JSON is neither efficient nor practical.

    List of IPs as JSON string will take more disk space. At the same time such list is harder to search - at some point you may want use these logs to search users by IP or display logins-per-day statistics. This will be much harder if you will need to operate on JSON string than separate table with proper schema.

    This also will slow down updates. For each new IP you need to update the whole JSON, so if you have a 100 saved IPs, and want to add a new one, you need to save in database JSON with 101 records. Inserting single record in separate table should be much more efficient (and easier to prune old data).

    So unless:
    a) you want to keep this simple and you don't want to store a large amount of data in this way nor update it often,
    b) you need to access user logs often and select performance is much more important than logs updating,
    I would recommend creating a separate table for login logs, where each row represents single login event. It is more safe solution - scales better and it is more pragmatic.

    评论

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染