0

我需要创建一个InnoDB表,用于向其中添加数据并不断获取最近添加的 10 行。为了避免必须对每个ORDER BY查询执行一次以获得最后 10 行,我希望表本身按顺序排序,以便我可以完全跳过并只执行 a ,这应该会自动拉出最近的 10 行添加到表中。 SELECTPrimary KeyDESCORDER BYSELECT ... LIMIT 10

我怎样才能做到这一点?是否像添加ORDER BY [PRIMARYKEY] DESCCREATE TABLE查询一样简单?即使在ing 新行之后,表格还会继续按顺序排序吗?DESCINSERT

4

1 回答 1

1

A RDBMS never provides any guarantees on the order of the rows in any of the tables it manages. The only way to get a specific order is to ask for one. For the case of MySQL, the rows happens to be sorted by the primary key in ascending order often time when that key is in auto increment mode, but it's not a guaranteed property.

Use ORDER BY on your queries to get the desired result.

On the other hand, the ordering will be faster if the primary key type is BTREE (which is the default on most engine).

The sorting direction isn't yet used on MySQL 5.5.

于 2013-04-16T17:30:10.313 回答