Query /SP running VERY slow on Sqlserver Replicated DB but fine on publisher and test databases

Seeberg, Paul (EHS) 21 Reputation points
2022-09-28T13:34:41.6+00:00

Running sqlserver 2017 on 3 servers, Prod, Test, Replication. All are identical. Set up identically. Prod replicates (transactional) to (shockingly) Replication. Test is standalone . The replication process runs very good.

We are having 2 SPs (and the query within when run by itself) run in 25 seconds on Production and Test. We kill the query after 20 minutes on the replication database. Rebooted countless times. Undated statistics, rebuilt indexes and such.

Me (DBA) and my team, all walked through the Article properties to ensure all the indexes and constraints and everything got into the snapshot. We can see everything on the Replication database. The database runs great except for these 2 SPs (well the query within). It is hitting some very large tables and is relatively complicated. With both Prod and REP effectively being the same, we are bashing our heads trying to figure out why the performance difference.

As a test, I copied the PROD data to TEST.. and it performed exactly the same, within a few seconds, of PROD.

None of the servers are showing us running out of memory or CPU on the windows server (2012 R2)

Any idea what I can look at, what settings or something that would make the replication database run differently than the publisher? Since i can move the db to other databases and get same results as prod, there must me something with the replication that is missing or wrong.

Any thoughts ?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,793 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,556 questions
{count} votes

Accepted answer
  1. Erland Sommarskog 101.6K Reputation points MVP
    2022-10-03T21:28:50.83+00:00

    A general comment is that this query is need of a major overhaul. There are all sorts of things in this query that can act as go-slower button. Unfortunately, I think the work for that rewrite goes beyond what you can expect from a forum. One tip, though, is to insert the result from these string-splitting operations into a temp table.

    However, sometimes you may be lucky, and that seems to be the case on the subscriber. To wit, I found a configuration difference. In the slow plan, the optimizer has CardinalityEstimationModelVersion="140", whereas the fast plan has CardinalityEstimationModelVersion="70". Microsoft rewrote the cardinality estimator in SQL 2014 and has refined it since. Often to the better, but as often with optimization, it can sometimes backfire. Not the least with a query that was not good from the start, but where you were lucky, so you need saw any need for tuning.

    To test this hypothesis, add this hint to the end of the query on the slow server:

       OPTION (USE HINT('QUERY_OPTIMIZER_COMPATIBILITY_LEVEL_110'))  
     
    

    The cardinality estimator can also be controlled by a database-scoped configuration or the compatibility level. I would guess that the subscriber has compatibility level < 120.


1 additional answer

Sort by: Most helpful
  1. YufeiShao-msft 7,061 Reputation points
    2022-09-29T08:35:46.51+00:00

    Hi @Seeberg, Paul (EHS) ,

    There are many factors to SQL Server that can cause the same query to perform differently across different environment
    The more common, the hardware, OS, the version of SQL Server, CU or SP, compatibility, the amount of data and execution plan and so on , one of which can result in very different query plans and performance

    You can choose to monitor the query or sp to trace the cause of the problem
    From this article, you can choose some ways like SQL monitor, review the query plan and query execution statistics to find the expensive queries
    https://www.red-gate.com/hub/product-learning/sql-monitor/why-is-my-query-suddenly-slow

    Troubleshoot a query that shows a significant performance difference between two servers

    -------------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments