title: Slow SQL Query on large database
body: <p>I have the following SQL query to find data that does not exist in another table and due to the replace sequences its taking over 35 minutes to complete?</p>
  
  <pre><code>DECLARE @p0 NVarChar(4) = 'UUID'
  DECLARE @p1 NVarChar(1) = ''
  DECLARE @p2 NVarChar(1) = 'p'
  DECLARE @p3 NVarChar(1) = ''
  DECLARE @p4 NVarChar(4) = 'UUID'
  DECLARE @p5 NVarChar(1) = ''
  DECLARE @p6 NVarChar(1) = 'p'
  DECLARE @p7 NVarChar(1) = ''
  
  SELECT 
      u.ProductUUID, u.id, c.id, c.MappingUUID
  FROM 
      dbo.Product_Data u
  LEFT OUTER JOIN 
      dbo.Mapping_Data c ON REPLACE(REPLACE(c.MappingUUID, @p0, @p1), @p2, @p3) = REPLACE(REPLACE(u.ProductUUID, @p4, @p5), @p6, @p7)
  WHERE 
      c.GN_Paid IS NULL
  </code></pre>
  
  <p>The 2x <code>replace</code> statements are because of 2 different prefixes that can occur between the 2 tables:</p>
  
  <p>Table 1:</p>
  
  <pre><code>UUID0000000000165019
  </code></pre>
  
  <p>Table 2:</p>
  
  <pre><code>i00165019
  </code></pre>
  
  <p>Although they end with the same id, I was looking at replacing the beginning and returning the results but it seems this is inefficient so wonder if any of you gurus have dealt with this and use a different method?</p>
  
  <p>Many thanks in advance</p>