dongtangu8403 2018-01-20 14:36
浏览 158
已采纳

我想在MySQL表和Delete中设置最大行数

what i actual want that i want to insert data in sql table but i want to limit table with 50 row no more and when i insert new data it delete the old data as new data enter for example there is 50 data already in table i add 10 new data the new data will insert and table show delete 10 rows from beginning. if new data(rows) 10 and already data in db is 45 row so it delete first 5 rows and add new 5 so i need help and suggestion how to put restriction on table and when i new data came it delete from start row some rows if data exceed 50 rows thanks in advance

  • 写回答

1条回答 默认 最新

  • dongmeng2687 2018-01-20 14:41
    关注

    Why? You are just making the inserts take longer.

    Instead, you can insert new rows and use an auto-incrementing primary key. Then you can do something like:

    select t.*
    from t
    order by t.id desc
    fetch first 50 rows only;
    

    This will get you the most recent 50 rows. And the query should perform quite well.

    What advantages does this have?

    • You get to keep all the data, which is quite useful to see what happened in the past.
    • Performance is not affected.
    • You can change "50" to another number on-the-fly.
    • Your inserts are not slowed down by deletes.
    • There is no need to deal with triggers and other complexity.

    Of course, if your table is going to grow to tens of millions of rows, this might not be the optimal solution (the table itself will start to eat up memory for other purposes). But for smallish tables, this is a very viable solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答