成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

深入解析 MongoDB 中的 renameCollection 操作:不同版本中的風險與影響

數據庫 MongoDB
在進行 renameCollection 操作時,請務必謹慎操作。這個操作可能會對線上系統產生重大影響,因此建議在非高峰時段進行,并且提前備份數據以防萬一。

在 MongoDB 中,renameCollection 操作用于將一個集合重命名為另一個名稱。雖然這看起來是一個簡單的操作,但在不同版本的 MongoDB 中,renameCollection 有一些重大的變化和注意事項。本文將深入探討在不同版本的 MongoDB 中執行 renameCollection 操作時需要考慮的方面。

MongoDB 3.4 版本

renameCollection is suitable for production environments; however:

  • renameCollection blocks all database activity for the duration of the operation.
  • renameCollection is not compatible with sharded collections.
  • renameCollection fails if is the name of an existing collection and you do not specify .targetdropTarget: true
  • You cannot rename a collection from a replicated database to the database, which is not replicated.local
  • You cannot rename a collection from the database, which is not replicated, to a replicated database.local

renameCollection 適合生產 環境:

  • renameCollection操作會阻塞整個數據庫的活動,直到操作完成為止。
  • renameCollection不兼容分片集合(sharded collections)
  • 如果要重命名的集合名字已經存在,并且沒有指定.targetdropTarget: true,renameCollection操作會失敗。
  • 不能將一個來自有副本的數據庫(replicated database)的集合重命名到沒有副本的數據庫(non-replicated database)。
  • 不能將一個來自沒有副本的數據庫的集合重命名到有副本的數據庫。

注意事項:

  1. 相當于是一個copy的操作,如果集合數據量過大,非常耗時,線上慎用;
  2. 重名過程會阻塞整個數據庫的活動,直到操作完成為止;
  3. 分片集合不支持重命名操作;

MongoDB 3.6 版本

Sharded Collections

renameCollection is not compatible with sharded collections.

Existing Target Collection

renameCollection fails if is the name of an existing collection and you do not specify .targetdropTarget: true

Performance

Changed in version 3.6.

renameCollection has different performance implications depending on the target namespace.

If the target database is the same as the source database, renameCollection simply changes the namespace. This is a quick operation.

If the target database differs from the source database, renameCollection copies all documents from the source collection to the target collection. Depending on the size of the collection, this may take longer to complete. Other operations which require exclusive access to the affected databases will be blocked until the rename completes. See What locks are taken by some common client operations? for operations which require exclusive access to all databases.

  1. 分片集合不兼容:renameCollection 操作不兼容于分片集合。這意味著,如果你的集合是分片的,你不能使用 renameCollection 來重命名這個集合。在對分片集合進行操作時,需要使用其他方式來管理集合的名稱或結構。
  2. 現有目標集合:如果目標集合已經存在,并且你沒有明確指定 .targetdrop 選項為 true,那么 renameCollection 操作將失敗。在執行 renameCollection 操作時,如果目標集合已經存在,你必須明確指定是否要刪除目標集合并覆蓋它。
  3. 性能影響:在 MongoDB 3.6 版本中,renameCollection 操作的性能取決于目標命名空間的情況:

  • 如果目標數據庫與源數據庫相同,那么 renameCollection 操作只是簡單地更改命名空間。這是一個快速的操作,因為它只涉及更改命名空間的元數據信息。
  • 如果目標數據庫與源數據庫不同,那么 renameCollection 操作將會復制源集合中的所有文檔到目標集合中。這可能會導致操作需要花費更多的時間來完成,特別是對于大型集合來說。在執行復制操作期間,其他需要對受影響的數據庫進行排他性訪問的操作將被阻塞,直到重命名操作完成。

注意事項:

db.adminCommand( { renameCollection: "db1.orders", to: "db1.orders2014" } )  

  1. renameCollection 操作不兼容于分片集合;
  2. 只更改命名空間,速度極快;

db.adminCommand( { renameCollection: "db1.orders", to: "db2.orders2014" } )  

  1. 跨庫重命名相當于是一個copy操作,如果集合數據量較大,執行特別耗時,阻塞操作,線上慎用。

MongoDB 4.2 版本

Sharded Collections

renameCollection is not compatible with sharded collections.

Existing Target Collection

renameCollection fails if is the name of an existing collection and you do not specify .targetdropTarget: true

Performance

Changed in version 3.6.

renameCollection has different performance implications depending on the target namespace.

If the target database is the same as the source database, renameCollection simply changes the namespace. This is a quick operation.

If the target database differs from the source database, renameCollection copies all documents from the source collection to the target collection. Depending on the size of the collection, this may take longer to complete.

Resource Locking

Changed in version 4.2.

If renaming a collection within the same database, renameCollection obtains an exclusive lock on the source and target collections for the duration of the operation. All subsequent operations on the collections must wait until renameCollection completes.

Prior to MongoDB 4.2, renaming a collection within the same database with renameCollection required an exclusive database lock.

If renaming a collection between different databases, renameCollection locking behavior depends on the MongoDB version:

For MongoDB 4.2.2 and later, renameCollection obtains an exclusive (W) lock on the target database, an intent shared (r) lock on the source database, and a shared (S) lock on the source collection. Subsequent operations on the target database must wait until renameCollection releases the exclusive database lock.

For MongoDB 4.2.1 and earlier, renameCollection obtains an exclusive (W) global lock. Subsequent operations on the mongod must wait until renameCollection releases the global lock.

For more information on locking in MongoDB, see FAQ: Concurrency.

1、分片集合不兼容:

renameCollection 操作不兼容于分片集合。這意味著無法直接對分片集合使用 renameCollection 來重命名。

2、現有目標集合:

如果要重命名的目標集合名稱已經存在,并且沒有指定 .targetdrop: true 參數,renameCollection 操作將失敗。這是為了避免意外覆蓋已存在的集合。在執行 renameCollection 操作時,需要明確指定是否要刪除已存在的目標集合以便重命名成功。

3、性能:

在 MongoDB 3.6 及更高版本中,renameCollection 操作的性能取決于目標命名空間的不同情況:

  • 如果目標數據庫與源數據庫相同,renameCollection 操作只是簡單地更改命名空間,這是一個快速的操作,因為它只涉及元數據的更改。
  • 如果目標數據庫與源數據庫不同,renameCollection 操作將會復制源集合中的所有文檔到目標集合中。這可能會導致操作耗時更長,特別是對于大型集合而言。

4、資源鎖定:

在 MongoDB 4.2 版本中,renameCollection 操作的資源鎖定行為發生了變化:

  • 如果在同一數據庫中重命名集合,renameCollection 會在操作期間對源集合和目標集合獲取獨占鎖。所有對這些集合的后續操作必須等待 renameCollection 完成。
  • 在不同數據庫之間重命名集合時,renameCollection 的鎖定行為取決于 MongoDB 版本:
  • 對于 MongoDB 4.2.2 及更高版本,renameCollection 會在目標數據庫上獲取獨占鎖(W),在源數據庫上獲取意圖共享鎖(r),在源集合上獲取共享鎖(S)。所有對目標數據庫的后續操作必須等待 renameCollection 釋放獨占數據庫鎖。
  • 對于 MongoDB 4.2.1 及更早版本,renameCollection 會獲取全局獨占鎖(W)。所有對 MongoDB 實例的后續操作必須等待 renameCollection 釋放全局鎖。

注意事項:

db.adminCommand( { renameCollection: "db1.orders", to: "db1.orders2014" } )  

  1. renameCollection 操作不兼容于分片集合;
  2. 只更改命名空間,速度極快;
  3. 對源集合和目標集合獲取獨占鎖,必須等待 renameCollection 完成

db.adminCommand( { renameCollection: "db1.orders", to: "db2.orders2014" } )  

  1.  MongoDB 4.2.2 及更高版本,跨庫重命名相當于是一個copy操作,如果集合數據量較大,執行特別耗時,目標數據庫上獲取獨占鎖(W),目標數據庫的后續操作必須等待 renameCollection 釋放獨占數據庫鎖,線上慎用。
  2. MongoDB 4.2.1 及更早版本,跨庫重命名相當于是一個copy操作,如果集合數據量較大,執行特別耗時,renameCollection 會獲取全局獨占鎖(W)。所有對 MongoDB 實例的后續操作必須等待 renameCollection 釋放全局鎖,線上慎用。

MongoDB 5.0 版本

Sharded Collections

Starting in MongoDB 5.0, you can use the renameCollection command to change the name of a sharded collection. The target database must be the same as the source database.

Unsharded Collections

You can use renameCollection to rename an unsharded collection in a sharded cluster as long as the source and target databases are on the same primary shard.

Existing Target Collection

renameCollection fails if target is the name of an existing collection and you do not specify dropTarget: true.

Performance

Changed in version 3.6.

renameCollection has different performance implications depending on the target namespace.

If the target database is the same as the source database, renameCollection simply changes the namespace. This is a quick operation.

If the target database differs from the source database, renameCollection copies all documents from the source collection to the target collection. Depending on the size of the collection, this may take longer to complete.

Resource Locking in Sharded Clusters

Changed in version 5.0.

When renaming a sharded or unsharded collection in a sharded cluster, the source and target collections are exclusively locked on every shard. Subsequent operations on the source and target collections must wait until the rename operation completes.

For more information on locking in MongoDB, see FAQ: Concurrency.

Resource Locking in Replica Sets

If renaming a collection within the same database, renameCollection obtains an exclusive lock on the source and target collections for the duration of the operation. All subsequent operations on the collections must wait until renameCollection completes.

If renaming a collection between different databases, renameCollection obtains an exclusive (W) lock on the target database, an intent shared (r) lock on the source database, and a shared (S) lock on the source collection. Subsequent operations on the target database must wait until renameCollection releases the exclusive database lock.

For more information on locking in MongoDB, see FAQ: Concurrency.

1、行為

  • 對于分片集合(Sharded Collections):從MongoDB 5.0開始,可以使用renameCollection命令來更改分片集合的名稱。目標數據庫必須與源數據庫相同。
  • 對于非分片集合(Unsharded Collections):在分片集群中,只要源數據庫和目標數據庫在同一個主分片上,就可以使用renameCollection來重命名非分片集合。
  • 對于已存在的目標集合:如果目標集合已經存在,而且沒有設置dropTarget: true選項,則renameCollection操作將失敗。

2、性能:

  • 自MongoDB 3.6版本開始,renameCollection的性能取決于目標命名空間。
  • 如果目標數據庫與源數據庫相同,則renameCollection僅僅修改命名空間,這是一個快速的操作。
  • 如果目標數據庫與源數據庫不同,則renameCollection將所有文檔從源集合復制到目標集合。根據集合的大小,這可能需要更長的時間來完成。

3、資源鎖定:

  • 在分片集群中,當重命名分片集合或非分片集合時,每個分片上的源和目標集合都會被獨占鎖定。在重命名操作完成之前,對源和目標集合的后續操作必須等待。
  • 在副本集中,如果在同一個數據庫中重命名集合,renameCollection會為操作的整個過程獲取源和目標集合的獨占鎖。所有對集合的后續操作必須等待renameCollection完成。
  • 如果在不同的數據庫之間重命名集合,renameCollection會在目標數據庫上獲取獨占鎖(W),在源數據庫上獲取意向共享鎖(r),在源集合上獲取共享鎖(S)。后續對目標數據庫的操作必須等待renameCollection釋放獨占數據庫鎖。

注意事項:

  1. 不能使用 renameCollection 重命名時間序列集合
  2. 支持分片集合renameCollection操作,不能跨庫操作;
  3. 非分片集合renameCollection操作,在分片集群中,源數據庫和目標數據庫在同一個主分片上;

db.adminCommand( { renameCollection: "db1.orders", to: "db1.orders2014" } )  

  1. 同庫重命名,只更改命名空間,速度極快;
  2. 在分片集群中,當重命名分片集合或非分片集合時,在重命名操作完成之前,源和目標集合都會被獨占鎖定,必須等待;
  3. 在副本集中,如果在同一個數據庫中重命名集合,在重命名操作完成之前,源和目標集合都會被獨占鎖定,必須等待;

db.adminCommand( { renameCollection: "db1.orders", to: "db2.orders2014" } )  

跨庫重命名相當于是一個copy操作,如果集合數據量較大,執行特別耗時。目標數據庫上獲取獨占鎖(W),在源數據庫上獲取意向共享鎖(r),在源集合上獲取共享鎖(S)。后續對目標數據庫的操作必須等待renameCollection釋放獨占數據庫鎖阻塞操作,線上慎用。

MongoDB 6.0 、7.0版本版本renameCollection未做變更

結論:

在進行 renameCollection 操作時,請務必謹慎操作。這個操作可能會對線上系統產生重大影響,因此建議在非高峰時段進行,并且提前備份數據以防萬一。謹慎的操作是確保系統穩定性和數據完整性的重要步驟,我們始終致力于提供最佳的服務和解決方案,以確保您的業務順利運行。再次感謝您對我們的信任和支持。

責任編輯:武曉燕 來源: DBA實戰
相關推薦

2012-11-16 10:14:54

Windows 8

2024-06-17 10:45:07

C++編程操作符

2024-03-11 15:32:50

C++開發

2020-11-05 08:56:19

Python

2023-11-26 18:02:00

ReactDOM

2025-01-07 13:48:57

2020-12-26 16:51:12

Python操作符開發

2024-05-06 00:00:00

C#工具代碼

2010-07-13 14:26:11

Perl數組

2024-10-15 16:53:07

2010-09-26 10:13:25

2024-12-06 10:43:27

2023-10-23 12:35:36

Golang追加操作

2023-08-07 08:13:41

2024-04-30 00:00:00

數倉維度建模

2009-02-20 09:50:29

C#方法重載編程

2024-11-20 15:55:57

線程Java開發

2024-07-01 08:04:38

2023-05-05 18:33:15

2023-11-29 09:00:55

ReactuseMemo
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲精品888 | 国产激情一区二区三区 | 欧美日韩视频在线第一区 | 免费视频一区 | 国产精品久久久久久影视 | 久久夜视频 | 精品福利在线 | 91免费在线播放 | 国产日韩欧美在线观看 | 99视频在线免费观看 | 激情欧美一区二区三区中文字幕 | 97精品超碰一区二区三区 | 99精品久久久久久中文字幕 | 三级成人片 | 国产亚洲成av人在线观看导航 | 国产一区二区精品在线 | 久久久久久亚洲精品 | a国产视频| 亚洲精品久久嫩草网站秘色 | 澳门永久av免费网站 | 欧美久久久久 | 国产精品福利视频 | 精品自拍视频在线观看 | 国产精品久久久久久久久久久久久 | 高清久久 | 日本精品一区 | 国产成人综合在线 | 一区二区三区四区av | 中文字幕动漫成人 | 久久久www成人免费精品 | 日韩三级电影在线看 | 欧美精品在线免费 | 九九伊人sl水蜜桃色推荐 | 九九热精品在线 | 黄视频网址 | 亚洲成人精品久久久 | 中文字幕在线视频网站 | 中文一区二区 | 亚洲欧美精品 | 国产日韩欧美 | 国产免费一区二区 |