gpt4 book ai didi

mysql - MySQL 中的默认排序顺序(ALTER TABLE ... ORDER BY ...;)

转载 作者:IT老高 更新时间:2023-10-28 23:57:31 31 4
gpt4 key购买 nike

假设通过执行以下操作更改了 MySQL 表 (ISAM) 的默认顺序:

ALTER TABLE tablename ORDER BY columnname ASC;

从现在开始,假设我的查询中没有指定“ORDER BY”(即“SELECT * FROM tablename”),我是否保证按照“列名 ASC”的顺序从表中检索记录哪里...限制 10;")?

有什么我应该注意的极端情况吗?

更新#1:非常感谢 Quassnoi,他正确地指出了 INSERT 和 DELETE 会打乱顺序。这导致我提出以下额外问题:

  • 更新怎么样?假设没有对表进行 INSERT 或 DELETE,而只有更新 - 排序顺序是否完整?
  • 假设进行了 INSERT 和 DELETE——我如何再次“重建”排序,比如每天一次(在这种特定情况下,表格仅每天更改,因此在更改完成后每天重建它应该仍然没问题! ). REPAIR TABLE 是否修复它,或者必须再次添加 do ALTER TABLE ... ORDER BY?

最佳答案

来自 documentation :

Note that the table does not remain in this order after inserts and deletes

实际上,如果您向此表发出 SELECT ... ORDER BY,则 ALTER TABLE 的选项不会让您免于 filesort,而是使 filesort 更快。​​

对已经排序的集合进行排序相当于浏览这个集合以确保一切正常。

What about UPDATEs? Assume that no INSERTs or DELETEs are made to the table, but only updates - will the sort order be intact?

如果您的表不包含任何动态字段(如 VARCHAR 或 'BLOB'),那么 很可能 MyISAM 不会移动它更新时。

不过,如果我正在 build 一座核电站或我得到报酬的东西,我不会依赖这种行为。

Assume that INSERTs and DELETEs are made - how do I "rebuild" the sorting again, say once a day (in this specific case the table only changes daily, so rebuilding it daily after the changes are done should still be OK!). Does REPAIR TABLE fix it, or must add do ALTER TABLE ... ORDER BY again?

您需要执行 ALTER TABLE ... ORDER BY

REPAIR 只是修复损坏表的物理结构。

关于mysql - MySQL 中的默认排序顺序(ALTER TABLE ... ORDER BY ...;),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/725717/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com