T-SQL |更好地替代WHERE NOT EXISTS

时间:2017-07-04 15:10:19

标签: sql-server tsql staging-table

我有以下问题:

INSERT INTO [Table A]  ([student_name], [class_id],[contact_detail], [birth_date],[note_average])
            SELECT [student_name] = case when CHARINDEX('.', [student_name])>0 then LEFT([student_name],CHARINDEX('.', [student_name])-1)
                        else [student_name] end
                        ,[class_id]
                        ,case when reverse(SUBSTRING(REVERSE([contact_detail]),1,CHARINDEX(':', REVERSE([contact_detail])))) like ':' 
                                        then ([contact_detail] + '|')
                                        else    [contact_detail]
                                        end as [contact_detail]
                         ,[birth_date]
                         ,CAST([note_average] AS decimal(13,2)) as [note_average]
                         ,GETDATE()
            FROM [Table A] 
            WHERE CAST([birth_date] AS DATE) <= CAST(GETDATE() AS DATE)
                    AND LEN([student_name]) >= 5
                    AND NOT EXISTS
                            (
                      SELECT [student_name]
                                              ,[class_id]
                                              ,[contact_detail]
                                              ,[birth_date]
                      FROM [Table A]  a
                      WHERE  '%' + ods.[student_name] + '%' LIKE a.[student_name]
                                AND '%' +  ods.[class_id] + '%'  LIKE a.[class_id]
                                AND '%' + ods.[contact_detail] + '%' LIKE a.[contact_detail]
                                AND ods.[birth_date] = a.[birth_date]
                            )
GO

我不想插入重复的值,我的表中没有密钥。我的问题是:这个查询花了很多时间来插入新值。我正在尝试插入1000000行。

我有哪些替代方案?

非常感谢!

1 个答案:

答案 0 :(得分:0)

假设您确实希望匹配完全匹配的匹配项,请尝试以下操作:

INSERT INTO [Table A]  ([student_name], [os_name], [class_id],[contact_detail], [birth_date],[note_average])
    SELECT ods.[student_name] = case when CHARINDEX('.', ods.[student_name])>0 then LEFT(ods.[student_name],CHARINDEX('.', ods.[student_name])-1)
            else ods.[student_name] end
        ,ods.[class_id]
        ,case when reverse(SUBSTRING(REVERSE(ods.[contact_detail]),1,CHARINDEX(':', REVERSE(ods.[contact_detail])))) like ':' 
            then (ods.[contact_detail] + '|')
            else    ods.[contact_detail]
            end as [contact_detail]
         ,ods.[birth_date]
         ,CAST(ods.[note_average] AS decimal(13,2)) as ods.[note_average]
         ,GETDATE()
    FROM [Table A] ods
    OUTER JOIN [Table A] a ON ods.[student_name] = a.[student_name]
        AND ods.[os_name] = a.[os_name]
        AND ods.[class_id] = a.[class_id]
        AND ods.[contact_detail] = a.[contact_detail]
        AND ods.[birth_date] = a.[birth_date]
    WHERE CAST(ods.[birth_date] AS DATE) <= CAST(GETDATE() AS DATE)
        AND LEN(ods.[student_name]) >= 5
        -- Only include when no matching duplicate is found.
        AND a.[student_name] IS NULL

如果你愿意,你可以坚持使用子查询和NOT EXISTS,但也可以使用SELECT 0或类似,而不是选择所有那些列,这是不必要的。我相信,将LIKE中的比较更改为=将为您提供所需的结果,并在必要时使索引可以使用索引进行优化。

AND NOT EXISTS
(
  SELECT 0
  FROM [Table A]  a
  WHERE  ods.[student_name] = a.[student_name]
            AND ods.[os_name] = a.[os_name]
            AND ods.[class_id] = a.[class_id]
            AND ods.[contact_detail] = a.[contact_detail]
            AND ods.[birth_date] = a.[birth_date]
        )

如果您的查询正常工作,那么您可以在需要更好的性能时查看添加索引。您可以在[birth_date]或[student_name]上添加索引以获得可接受的查询性能。