mysql 表如何设置阈值或限制? [英] How do you make a threshold or limit mysql table?
问题描述
我有一个表,我只想要 20 个最近的条目,因为该表每 0.50 秒添加一行.
I have a table which i would like only to have the 20 most recent entries, because the table is adding a row every .50 seconds.
我将如何在查询数据库(mysql)时完成此操作.
How would I accomplish this in querying to the database(mysql).
每当我想推入一个新值时,我是否应该添加一个新行并删除最旧的行?
Should i add a new row and delete the oldest whenever i want to push a new value in?
推荐答案
您可以创建一个 查看.为了保证按最近排序,请引入一个带有时间戳的列作为排序依据(或者在使用顺序编号时使用主键,例如 AUTO_INCREMENT).
You could create a view. To guarantee ordering by recentness, introduce a column with a timestamp to order by (or use the primary key when using sequential numbering, such as AUTO_INCREMENT).
CREATE VIEW latest_entries AS
SELECT ... FROM TABLE foo ORDER BY created_time LIMIT 20;
另外,不要忘记不时清理"底层表.
Also, don't forget to 'clean' the underlying table from time to time.
这篇关于mysql 表如何设置阈值或限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!