自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

在 MongoDB 中,renameCollection 操作用于將一個集合重命名為另一個名稱。雖然這看起來是一個簡單的操作,但在不同版本的 MongoDB 中,renameCollection 有一些重大的變化和注意事項。本文將深入探討在不同版本的 MongoDB 中執(zhí)行 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 適合生產(chǎn) 環(huán)境:

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

注意事項:

  1. 相當(dāng)于是一個copy的操作,如果集合數(shù)據(jù)量過大,非常耗時,線上慎用;
  2. 重名過程會阻塞整個數(shù)據(jù)庫的活動,直到操作完成為止;
  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 來重命名這個集合。在對分片集合進行操作時,需要使用其他方式來管理集合的名稱或結(jié)構(gòu)。
  2. 現(xiàn)有目標(biāo)集合:如果目標(biāo)集合已經(jīng)存在,并且你沒有明確指定 .targetdrop 選項為 true,那么 renameCollection 操作將失敗。在執(zhí)行 renameCollection 操作時,如果目標(biāo)集合已經(jīng)存在,你必須明確指定是否要刪除目標(biāo)集合并覆蓋它。
  3. 性能影響:在 MongoDB 3.6 版本中,renameCollection 操作的性能取決于目標(biāo)命名空間的情況:

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

注意事項:

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

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

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

  1. 跨庫重命名相當(dāng)于是一個copy操作,如果集合數(shù)據(jù)量較大,執(zhí)行特別耗時,阻塞操作,線上慎用。

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、現(xiàn)有目標(biāo)集合:

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

3、性能:

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

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

4、資源鎖定:

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

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

注意事項:

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

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

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

  1.  MongoDB 4.2.2 及更高版本,跨庫重命名相當(dāng)于是一個copy操作,如果集合數(shù)據(jù)量較大,執(zhí)行特別耗時,目標(biāo)數(shù)據(jù)庫上獲取獨占鎖(W),目標(biāo)數(shù)據(jù)庫的后續(xù)操作必須等待 renameCollection 釋放獨占數(shù)據(jù)庫鎖,線上慎用。
  2. MongoDB 4.2.1 及更早版本,跨庫重命名相當(dāng)于是一個copy操作,如果集合數(shù)據(jù)量較大,執(zhí)行特別耗時,renameCollection 會獲取全局獨占鎖(W)。所有對 MongoDB 實例的后續(xù)操作必須等待 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命令來更改分片集合的名稱。目標(biāo)數(shù)據(jù)庫必須與源數(shù)據(jù)庫相同。
  • 對于非分片集合(Unsharded Collections):在分片集群中,只要源數(shù)據(jù)庫和目標(biāo)數(shù)據(jù)庫在同一個主分片上,就可以使用renameCollection來重命名非分片集合。
  • 對于已存在的目標(biāo)集合:如果目標(biāo)集合已經(jīng)存在,而且沒有設(shè)置dropTarget: true選項,則renameCollection操作將失敗。

2、性能:

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

3、資源鎖定:

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

注意事項:

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

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

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

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

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

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

結(jié)論:

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

責(zé)任編輯:武曉燕 來源: DBA實戰(zhàn)
相關(guān)推薦

2012-11-16 10:14:54

Windows 8

2024-06-17 10:45:07

C++編程操作符

2024-03-11 15:32:50

C++開發(fā)

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操作符開發(fā)

2024-05-06 00:00:00

C#工具代碼

2010-07-13 14:26:11

Perl數(shù)組

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追加操作

2009-02-20 09:50:29

C#方法重載編程

2024-04-30 00:00:00

數(shù)倉維度建模

2024-11-20 15:55:57

線程Java開發(fā)

2024-07-01 08:04:38

2023-05-05 18:33:15

2023-11-29 09:00:55

ReactuseMemo

2023-08-07 08:13:41

點贊
收藏

51CTO技術(shù)棧公眾號