我可以为MySQL表的每一行定义一个生存期吗?

我可以为MySQL表的每一行定义一个生存期吗?,mysql,sql,database,Mysql,Sql,Database,我有一张这样的桌子: // banned +----+---------+---------------+ | id | user_id | unix_time | +----+---------+---------------+ | 1 | 32534 | 1467066745524 | | 2 | 43535 | 1467066745541 | | 3 | 24352 | 1467066745618 | | 4 | 88734 | 1467066746093

我有一张这样的桌子:

// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 |
| 2  | 43535   | 1467066745541 |
| 3  | 24352   | 1467066745618 |
| 4  | 88734   | 1467066746093 |
+----+---------+---------------+
// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 | -- removing this row automatically in 10 min
| 2  | 43535   | 1467066745541 | -- removing this row automatically in 1 hour
| 3  | 24352   | 1467066745618 | -- removing this row automatically 2 day
| 4  | 88734   | 1467066746093 | -- removing this row automatically 8 hours min
+----+---------+---------------+
create view v_table as
    select (case when unix_time < UNIX_TIMESTAMP() then id end) as id,
           (case when unix_time < UNIX_TIMESTAMP() then user_id end) as user_id
    from banned;
实际上,当我插入每一行时,我需要为每一行定义一个过期时间。这在MySQL中可能吗?我听说这在Redis中是可能的,那么MySQL呢

所以我想要这样的东西:

// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 |
| 2  | 43535   | 1467066745541 |
| 3  | 24352   | 1467066745618 |
| 4  | 88734   | 1467066746093 |
+----+---------+---------------+
// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 | -- removing this row automatically in 10 min
| 2  | 43535   | 1467066745541 | -- removing this row automatically in 1 hour
| 3  | 24352   | 1467066745618 | -- removing this row automatically 2 day
| 4  | 88734   | 1467066746093 | -- removing this row automatically 8 hours min
+----+---------+---------------+
create view v_table as
    select (case when unix_time < UNIX_TIMESTAMP() then id end) as id,
           (case when unix_time < UNIX_TIMESTAMP() then user_id end) as user_id
    from banned;

如您所见,每一行都有一个任意的生存期。

您可以创建一个每分钟运行一次的事件,并删除以下旧记录:

// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 |
| 2  | 43535   | 1467066745541 |
| 3  | 24352   | 1467066745618 |
| 4  | 88734   | 1467066746093 |
+----+---------+---------------+
// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 | -- removing this row automatically in 10 min
| 2  | 43535   | 1467066745541 | -- removing this row automatically in 1 hour
| 3  | 24352   | 1467066745618 | -- removing this row automatically 2 day
| 4  | 88734   | 1467066746093 | -- removing this row automatically 8 hours min
+----+---------+---------------+
create view v_table as
    select (case when unix_time < UNIX_TIMESTAMP() then id end) as id,
           (case when unix_time < UNIX_TIMESTAMP() then user_id end) as user_id
    from banned;
启用调度程序

创建每分钟运行一次的事件

样品


您可以创建一个每分钟运行一次的事件并删除旧记录,如下所示:

// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 |
| 2  | 43535   | 1467066745541 |
| 3  | 24352   | 1467066745618 |
| 4  | 88734   | 1467066746093 |
+----+---------+---------------+
// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 | -- removing this row automatically in 10 min
| 2  | 43535   | 1467066745541 | -- removing this row automatically in 1 hour
| 3  | 24352   | 1467066745618 | -- removing this row automatically 2 day
| 4  | 88734   | 1467066746093 | -- removing this row automatically 8 hours min
+----+---------+---------------+
create view v_table as
    select (case when unix_time < UNIX_TIMESTAMP() then id end) as id,
           (case when unix_time < UNIX_TIMESTAMP() then user_id end) as user_id
    from banned;
启用调度程序

创建每分钟运行一次的事件

样品

您可以创建一个视图,如下所示:

// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 |
| 2  | 43535   | 1467066745541 |
| 3  | 24352   | 1467066745618 |
| 4  | 88734   | 1467066746093 |
+----+---------+---------------+
// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 | -- removing this row automatically in 10 min
| 2  | 43535   | 1467066745541 | -- removing this row automatically in 1 hour
| 3  | 24352   | 1467066745618 | -- removing this row automatically 2 day
| 4  | 88734   | 1467066746093 | -- removing this row automatically 8 hours min
+----+---------+---------------+
create view v_table as
    select (case when unix_time < UNIX_TIMESTAMP() then id end) as id,
           (case when unix_time < UNIX_TIMESTAMP() then user_id end) as user_id
    from banned;
或:

这不会在时间戳之后返回任何关于用户的信息。然后,您可以安排一个事件,定期删除包含过期信息的行。

您可以创建一个视图,如下所示:

// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 |
| 2  | 43535   | 1467066745541 |
| 3  | 24352   | 1467066745618 |
| 4  | 88734   | 1467066746093 |
+----+---------+---------------+
// banned
+----+---------+---------------+
| id | user_id |   unix_time   |
+----+---------+---------------+
| 1  | 32534   | 1467066745524 | -- removing this row automatically in 10 min
| 2  | 43535   | 1467066745541 | -- removing this row automatically in 1 hour
| 3  | 24352   | 1467066745618 | -- removing this row automatically 2 day
| 4  | 88734   | 1467066746093 | -- removing this row automatically 8 hours min
+----+---------+---------------+
create view v_table as
    select (case when unix_time < UNIX_TIMESTAMP() then id end) as id,
           (case when unix_time < UNIX_TIMESTAMP() then user_id end) as user_id
    from banned;
或:



这不会在时间戳之后返回任何关于用户的信息。然后,您可以安排一个事件来定期删除包含过期信息的行。

如果每个用户的过期时间是随机的,那么我认为您必须自己插入该数据。只需将过期日期设置为当前日期+允许用户存在的秒/分钟/天/等。至于删除,我不认为有任何方法可以强迫mysql删除记录。如果日期过期,您必须创建一个作业来删除记录。为什么过期很重要?例如,如果您不希望用户登录系统超过其过期日期,请在身份验证期间检查该日期。如果每个用户的过期时间是随机的,则此链接可能会帮助您,我认为您必须自己插入该数据。只需将过期日期设置为当前日期+允许用户存在的秒/分钟/天/等。至于删除,我不认为有任何方法可以强迫mysql删除记录。如果日期过期,您必须创建一个作业来删除记录。为什么过期很重要?例如,如果您不希望用户登录到超过其到期日期的系统,请在身份验证期间检查该日期。此链接可能有助于您始终在查询中选择所有被禁止表的行。因为对于所有行,您的条件始终为true unix\u timeunix\u TIMESTAMP-life\u time。无论如何,谢谢你。。upvote@stack . . . 我提出了另一个选择。事实上,我总是害怕使用视图。这是因为每次我向视图发送查询时,首先会执行该视图的逻辑,然后运行查询。因此,我认为使用视图会产生开销,这实际上是对数据库资源的浪费。但是每天通过一个活动清洁桌子需要的过程要少得多。清洁=移除过期的information@stack . . . 视图实际上不是这样工作的。尤其是在这种情况下,视图应该是非常有效的。在查询中总是选择所有被禁止表的行。因为对于所有行,您的条件始终为true unix\u timeunix\u TIMESTAMP-life\u time。无论如何,谢谢你。。upvote@stack . . . 我提出了另一个选择。事实上,我总是害怕使用视图。这是因为每次我向视图发送查询时,首先会执行该视图的逻辑,然后运行查询。因此,我认为使用视图会产生开销,这实际上是对数据库资源的浪费。但是每天通过一个活动清洁桌子需要的过程要少得多。清洁=移除过期的information@stack . . . 视图实际上不是这样工作的。尤其是在这种情况下,视图应该是非常有效的。unix_timeunix\u时间戳;为防止垃圾收集每隔n分钟读取一个事件的无效项和2。unix_time具有非常高的粒度,2在可能不需要的情况下,它会导致运行事件进行清理的开销。运行如此频繁的事件确实是对数据库资源的浪费。@Gordon Linoff-是的,正确-更好的解决方案是在两种方式上都这样做。1从..中选择*。。其中expire\u unix\u time>unix\u时间戳;为防止垃圾回收每隔n分钟读取一个无效项和2个事件。