2

INTフィールドのNULL値をテストすると、クエリがクロールまで遅くなるという非常に奇妙な問題があります。

クエリは次のとおりです。

WITH CommonRows (Dm2Id) AS (
    SELECT LibraryId
      FROM dbo.Items AS i1
      JOIN Test.Items AS i2
        ON i1.Dm2Id = i2.Dm2Id
     WHERE HasNew = 1
       AND HasOld = 1
       -- Note this: odd things happening here
       AND LibraryId IS NOT NULL
)
SELECT COALESCE(New.ImageLibraryId, Old.ImageLibraryId) AS ImageLibraryId,
        COALESCE(New.Field, Old.Field) AS Field,
        New.Value AS New,
        Old.Value AS Old
   FROM (SELECT ImageLibraryId,
                LEFT(CAST(TitleItemId AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS TitleItemId,
                LEFT(CAST(Title AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS Title,
                LEFT(CAST(Author AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS Author,
                LEFT(CAST(AuthorFirstname AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS AuthorFirstname,
                LEFT(CAST(AuthorLastname AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS AuthorLastname,
                LEFT(CAST(Teaser AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS Teaser,
                LEFT(CAST(TeaserListView AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS TeaserListView,
                LEFT(CAST(Language AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS Language,
                LEFT(CAST(PubYear AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS PubYear,
                LEFT(CAST(FictionNonFiction AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS FictionNonFiction,
                LEFT(CAST(TargetAudience AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS TargetAudience,
                LEFT(CAST(SeriesTitle AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS SeriesTitle,
                LEFT(CAST(SeriesSeqNo AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS SeriesSeqNo,
                LEFT(CAST(SeriesTotalCnt AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS SeriesTotalCnt
           FROM dbo.TitleItems
          WHERE ImageLibraryId IN (SELECT * FROM CommonRows)) p
UNPIVOT (Value FOR Field IN
            (TitleItemId, Title, Author, AuthorFirstname, AuthorLastname, Teaser,
             TeaserListView, Language, PubYear, FictionNonFiction, TargetAudience,
             SeriesTitle, SeriesSeqNo, SeriesTotalCnt)) AS New
   FULL OUTER JOIN
        (SELECT ImageLibraryId,
                LEFT(CAST(TitleItemId AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS TitleItemId,
                LEFT(CAST(Title AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS Title,
                LEFT(CAST(Author AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS Author,
                LEFT(CAST(AuthorFirstname AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS AuthorFirstname,
                LEFT(CAST(AuthorLastname AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS AuthorLastname,
                LEFT(CAST(Teaser AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS Teaser,
                LEFT(CAST(TeaserListView AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS TeaserListView,
                LEFT(CAST(Language AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS Language,
                LEFT(CAST(PubYear AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS PubYear,
                LEFT(CAST(FictionNonFiction AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS FictionNonFiction,
                LEFT(CAST(TargetAudience AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS TargetAudience,
                LEFT(CAST(SeriesTitle AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS SeriesTitle,
                LEFT(CAST(SeriesSeqNo AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS SeriesSeqNo,
                LEFT(CAST(SeriesTotalCnt AS NVARCHAR(MAX)), 20) COLLATE DATABASE_DEFAULT AS SeriesTotalCnt
           FROM CatalogSearch.dbo.TitleItems
          WHERE ImageLibraryId IN (SELECT * FROM CommonRows)) p
UNPIVOT (Value FOR Field IN
            (TitleItemId, Title, Author, AuthorFirstname, AuthorLastname, Teaser,
             TeaserListView, Language, PubYear, FictionNonFiction, TargetAudience,
             SeriesTitle, SeriesSeqNo, SeriesTotalCnt)) AS Old
     ON (New.ImageLibraryId = Old.ImageLibraryId AND New.Field = Old.Field COLLATE DATABASE_DEFAULT)
  WHERE (New.Value <> Old.Value COLLATE DATABASE_DEFAULT
         OR New.Value IS NULL AND Old.Value IS NOT NULL
         OR New.Value IS NOT NULL AND Old.Value IS NULL)
         -- fjernet rækker hvor New.Value indeholder mere information end Old.Value, men Old.Value indgår i starten af New.Value
         AND NOT (New.Field IN ('Teaser', 'TeaserListView') AND New.Value LIKE Old.Value + '%')
         -- fjernet række fordi den har fået et ekstra space med i Old.Value i midten af teksten
         AND NOT (New.Field IN ('Teaser', 'TeaserListView') AND New.ImageLibraryId = 800314)
         -- fjernet række fordi den tolkes som ukendt i New.Value (pga. fejldata i Bibkat) og som dansk i Old.Value
         AND NOT (New.Field = 'Language' AND New.ImageLibraryId = 800252)
        -- This is being tested separately in TitleItemGroups
        AND NOT New.Field = 'TitleItemId'

上記のように実行すると、クエリは数分間実行されてから、一度に数行しか返されません。クエリが終了するまで待つ忍耐力がありませんでした。クエリが終了するまでに 10 分以上かかると思います。

しかし、 (この場合はまったく同じ結果が得られます-これを確認しました)に置き換えるAND LibraryId IS NOT NULLと、クエリは30秒で実行されます。AND LibraryId > 0

なんで?

4

1 に答える 1