dongmieqiao3152 2016-06-28 20:32
浏览 33
已采纳

如何检测表中的冗余行?

I'm following solution #1 of this answer. After a while, there will be some redundant rows in the table. Suppose this table:

+------+------------------+
| user |     cookie       |
+------+------------------+
| 1    | ojer0f934mf2...  |
| 2    | ko4398f43043...  |
| 2    | 34fjkg3j438t...  |
| 3    | 0243hfd348i4...  |
+------+------------------+

when user 1 removes his browser's cookies, still this row exists in the database:

| 1    | ojer0f934mf2...  |

And when he opens my website, he needs to log in again. So here is the table:

+------+------------------+
| user |     cookie       |
+------+------------------+
| 1    | ojer0f934mf2...  | -- now this row is useless anymore
| 2    | ko4398f43043...  |
| 2    | 34fjkg3j438t...  |
| 3    | 0243hfd348i4...  |
| 1    | 0243hfd348i4...  |
+------+------------------+

Surely in future, the number of such useless rows will increase. Well how can I manage it? Actually I can create an EVENT to clean that table up (per day) by removing redundant rows. But here is the problem: How can I detect them? How can I specify a row is useless?


EDIT: I can add a new column and store the timestamp, and then remove all old-timestamp rows by an EVENT (or a cron jobs). But that isn't a perfect solution. I like a cookie be valid until user signs out (so I don't like any limitation (or expire time) for cookies).

Now I'm looking for a solution to determine redundant rows (not old rows).

  • 写回答

4条回答 默认 最新

  • douaipi3965 2016-06-30 09:37
    关注

    When a user logs out, explicitly delete the row in the table that corresponds to their current cookie.

    For security you should be storing a random value within the cookie, at a secure hash of the cookie within the database tables (e.g. SHA-2).

    For dealing with users that no longer use a particular browser, store another field for Last Used Date. Update this date as soon as you detect the cookie for the row being used to access your application.

    Then you will want a scheduled task to run every so often that cleans up sessions that have not been recently used.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?