TOP 2 faster than TOP 1 on Sql server?

we have a table with + 500k rows in the Sql Server 2005 database, and one of its columns has a full-text index.

We ran some tests and found that SELECT TOP 1 ... WHERE CONTAINS (fullTextColumn, 'anyValue') takes more than two minutes to return. However, the same choice, but with TOP 2 instead of 1, return after a few seconds.

Any idea on why this is happening?

Thanks!

Edit: Additional information about the problem. In TOP-1, the Sql query plan uses remote scanning, and in TOP 2 - index search. Very strange.

Edit 2: These are both Sql Execution Plans:

TOP 1

  StmtText
 -------------------------------------------------- -------------------------------------------------- ----------------------------------------------
   | --Top (TOP EXPRESSION: ((1)))
        | --Nested Loops (Left Semi Join, WHERE: ([ECRReload]. [Dbo]. [TECR_PRODUTO_COMPLETO_VENDAVEIS]. [IdProduto] = [Full-text Search Engine]. [KEY])))
             | --Index Scan (OBJECT: ([ECRReload]. [Dbo]. [TECR_PRODUTO_COMPLETO_VENDAVEIS]. [Ix_tecr_produto_completo_vendaveis01]))
             | --Remote Scan (OBJECT: (CONTAINS))

TOP 2

  StmtText
 -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -----------------------------------------------
   | --Top (TOP EXPRESSION: ((2)))
        | --Nested Loops (Inner Join, OUTER REFERENCES: ([Full-text Search Engine]. [KEY]))
             | --Remote Scan (OBJECT: (CONTAINS))
             | --Index Seek (OBJECT: ([ECRReload]. [Dbo]. [TECR_PRODUTO_COMPLETO_VENDAVEIS]. [Ix_tecr_produto_completo_vendaveis01]), SEEK: ([ECRReload]. [Dbo]. [TECRUT_END_RODROPRODROPLO_DOM] [DEC] _TEC_OPEND_RODROPLOUT_DEC_PLAY_TEC_UT_END_ROD_PLOUT_DEC_UT_UT Engine]. [KEY]) ORDERED FORWARD)
+4
source share
1 answer

I am not 100% sure if I can say that this is fixed, but it seems. Last night, our DBA rebuilt the FullText directory and rebuilt, and it started working. Both queries now work in less than a second.

I can’t explain why, but it works.

0
source

Source: https://habr.com/ru/post/1300043/


All Articles