sql – 合并复制数据库中的插入是非常缓慢的

前端之家收集整理的这篇文章主要介绍了sql – 合并复制数据库中的插入是非常缓慢的前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个sql服务器,通过合并复制安装到运行sql CE的800个移动客户端.

服务器具有足够的资源,公司内外的业务流量足够多,客户端与服务器之间的复制一般都很好,但是我们无法追踪到间歇性的错误.

昨天我们需要在我们的主表中插入550条记录,唯一的触发器是标准的合并复制.

这个插件花费了14个小时,因为它与移动设备试图同步不断地陷入僵局.

有没有人有任何建议,我们如何可以避免插入锁和如何加快整个过程?

——更新—–

接下来的一些评论,我已经运行一个剖析器在一个单一的插入,我看到很多这样的事情

insert into dbo.MSmerge_current_partition_mappings with (rowlock) (publication_number,tablenick,rowguid,partition_id)
            select distinct 1,mc.tablenick,mc.rowguid,v.partition_id
            from dbo.MSmerge_contents mc with (rowlock) 
            JOIN dbo.[MSmerge_JEMProjectME_PromotionResource_PARTITION_VIEW] v with (rowlock) 
            ON mc.tablenick = 286358001
            and mc.rowguid = v.[rowguid]
            and mc.marker = @child_marker 
            and v.partition_id in (select partition_id from dbo.MSmerge_current_partition_mappings cpm with (rowlock) JOIN
                dbo.MSmerge_contents mc2 with (rowlock)
                ON cpm.rowguid = mc2.rowguid
                and mc2.marker = @marker)
            where not exists (select * from MSmerge_current_partition_mappings with (readcommitted,rowlock,readpast) where 
                publication_number = 1 and 
                tablenick = 286358001 and
                rowguid = v.[rowguid] and
                partition_id = v.partition_id)

对于许多表,我不是要插入…这可能是一个线索?

解决方法

我们最近在我们的系统中经历了与您的系统相似的行为.原因是msmerge_contents和msmsmerge_current_partition_mappings中的大量数据,我们注意到,通过查看sql Profiler中读取的行数可能是缺少的索引. (49 000 000读取一个简单的插入在一个表中似乎有点多)

解决30分钟前通过添加两个索引:

CREATE NONCLUSTERED INDEX [IX_MSmerge_current_partition_mappings_PERF1] ON [dbo].[MSmerge_current_partition_mappings] 
(
    [partition_id] ASC
)
INCLUDE ( [rowguid]) 


CREATE NONCLUSTERED INDEX [IX_msmerge_contents_PERF1] ON [dbo].[MSmerge_contents] 
(
    [marker] ASC
)
INCLUDE ( [rowguid])

我希望这可以帮助你,它帮助我们将查询时间从5分钟降低到10秒.

– 几个小时后…

我的同事发现另一个指标进一步提高了75%的表现:

CREATE NONCLUSTERED INDEX [IX_MSmerge_current_partition_mappings_PERF2] ON [dbo].[MSmerge_current_partition_mappings] 
(
    [rowguid] ASC,[partition_id] ASC
)

识别缺失的索引
您可以使用以下脚本来识别缺失的索引,按预期的方式排序,最大程度地提高性能(有许多这样的脚本流行,这是从http://www.sherbaz.com/category/sqlserver/借来的)

SELECT  sys.objects.name,(avg_total_user_cost * avg_user_impact) * (user_seeks + user_scans) AS Impact,'CREATE NONCLUSTERED INDEX ix_IndexName ON ' + sys.objects.name COLLATE DATABASE_DEFAULT + ' ( ' + IsNull(mid.equality_columns,'') + CASE WHEN mid.inequality_columns IS NULL 
                THEN ''  
    ELSE CASE WHEN mid.equality_columns IS NULL 
                    THEN ''  
        ELSE ',' END + mid.inequality_columns END + ' ) ' + CASE WHEN mid.included_columns IS NULL 
                THEN ''  
    ELSE 'INCLUDE (' + mid.included_columns + ')' END + ';' AS CreateIndexStatement,mid.equality_columns,mid.inequality_columns,mid.included_columns 
    FROM sys.dm_db_missing_index_group_stats AS migs 
            INNER JOIN sys.dm_db_missing_index_groups AS mig ON migs.group_handle = mig.index_group_handle 
            INNER JOIN sys.dm_db_missing_index_details AS mid ON mig.index_handle = mid.index_handle AND mid.database_id = DB_ID() 
            INNER JOIN sys.objects WITH (nolock) ON mid.OBJECT_ID = sys.objects.OBJECT_ID 
    WHERE     (migs.group_handle IN 
        ( 
        SELECT     TOP (500) group_handle 
            FROM          sys.dm_db_missing_index_group_stats WITH (nolock) 
            ORDER BY (avg_total_user_cost * avg_user_impact) * (user_seeks + user_scans) DESC))  
        AND OBJECTPROPERTY(sys.objects.OBJECT_ID,'isusertable')=1 
    ORDER BY 2 DESC,3 DESC
原文链接:https://www.f2er.com/mssql/76205.html

猜你在找的MsSQL相关文章