对于没有标识字段(和复合主键)的表,Microsoft同步框架速度较慢



我正在使用Microsoft Sync Framework 2.1从本地数据库到Azure Sql数据库进行单向复制。当同步没有标识字段和复合主键的表时,sync为所有这些表中的每一行运行4条sql语句。

UPDATE [MYTABLE_tracking] SET [update_scope_local_id] = @sync_scope_local_id, [scope_update_peer_key] = @sync_update_peer_key, [scope_update_peer_timestamp] = @sync_update_peer_timestamp, [local_update_peer_key] = 0, [local_update_peer_timestamp] = @nextRowVersion, [sync_row_is_tombstone] = @sync_row_is_tombstone WHERE ([PrimaryCol1] = @P_1 AND [PrimaryCol2] = @P_2) AND (@sync_check_concurrency = 0 or [local_update_peer_timestamp] = @sync_row_timestamp)
SELECT [side].[PrimaryCol1], [side].[PrimaryCol2], ... , [side].[sync_row_is_tombstone], [side].[local_update_peer_timestamp] as sync_row_timestamp, case when ([side].[update_scope_local_id] is null or [side].[update_scope_local_id] <> @sync_scope_local_id) then [side].[local_update_peer_timestamp] else [side].[scope_update_peer_timestamp] end as sync_update_peer_timestamp, case when ([side].[update_scope_local_id] is null or [side].[update_scope_local_id] <> @sync_scope_local_id) then case when ([side].[local_update_peer_key] > @sync_scope_restore_count) then @sync_scope_restore_count else [side].[local_update_peer_key] end else [side].[scope_update_peer_key] end as sync_update_peer_key, case when ([side].[create_scope_local_id] is null or [side].[create_scope_local_id] <> @sync_scope_local_id) then [side].[local_create_peer_timestamp] else [side].[scope_create_peer_timestamp] end as sync_create_peer_timestamp, case when ([side].[create_scope_local_id] is null or [side].[create_scope_local_id] <> @sync_scope_local_id) then case when ([side].[local_create_peer_key] > @sync_scope_restore_count) then @sync_scope_restore_count else [side].[local_create_peer_key] end else [side].[scope_create_peer_key] end as sync_create_peer_key from [MYTABLE] [base] right join [MYTABLE_tracking] [side] on [base].[PrimaryCol1] = [side].[PrimaryCol1] and [base].[PrimaryCol2] = [side].[PrimaryCol2] WHERE [side].[PrimaryCol1] = @P_1 AND [side].[PrimaryCol2] = @P_2
SELECT @was_tombstone = [sync_row_is_tombstone] FROM [MYTABLE_tracking] WHERE ([PrimaryCol1] = @P_1 AND [PrimaryCol2] = @P_2)
IF NOT EXISTS (SELECT * FROM [MYTABLE_tracking] WHERE [PrimaryCol1] = @P_1 AND [PrimaryCol2] = @P_2)

很多这样的表格都是多对多的表格。有些有标识字段,但同步框架不支持标识不是主键的情况,所以我不得不从同步中排除标识列。在我的数据库同步之前,我会收到一百万个数据库查询,这会减慢同步速度,因此同步需要超过24小时,我的Azure账单也在增长。

我可以在配置这些表时以某种方式指示同步框架,以便它可以优化同步吗?

目标数据库是只读的,即只有同步写入它,这样我就可以删除约束等。我创建的数据库没有外键,但如果我创建的目标数据库也没有主键,会有帮助吗?

更新:如果我创建没有索引的目标数据库,我会得到错误

Microsoft.Synchronization.Data.DbSyncException:未能执行表"CNT_ContentNode"的命令"BulkInsertCommand";这个事务已回滚。确保命令语法为正确。--->System.Data.SqlClient.SqlException:没有此版本的SQL Server不支持聚集索引。请创建聚集索引,然后重试。

-Mathias

我最终用组合键从表中删除了所有主键,并用聚集的unqiue索引取而代之。现在它工作正常。

-Mathias

相关内容

最新更新