FreeText Query is slow - includes TOP and Order By

前端 未结 6 1416
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 15:03

The Product table has 700K records in it. The query:

SELECT TOP 1 ID, Name FROM Product WHERE contains(Name, \'\"White Dress\"\') ORDER BY DateMadeN

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-13 15:46

    Looks like FullTextMatch has over 400K executions. Why is this happening?

    Since you have an index combined with TOP 1, optimizer thinks that it will be better to traverse the index, checking each record for the entry.

    How can it be made faster?

    If updating the statistics does not help, try adding a hint to your query:

    SELECT  TOP 1 *
    FROM    product pt
    WHERE   CONTAINS(name, '"test1"')
    ORDER BY
            datemadenew DESC
    OPTION (HASH JOIN)
    

    This will force the engine to use a HASH JOIN algorithm to join your table and the output of the fulltext query.

    Fulltext query is regarded as a remote source returning the set of values indexed by KEY INDEX provided in the FULLTEXT INDEX definition.

    Update:

    If your ORM uses parametrized queries, you can create a plan guide.

    • Use Profiler to intercept the query that the ORM sends verbatim
    • Generate a correct plan in SSMS using hints and save it as XML
    • Use sp_create_plan_guide with an OPTION USE PLAN to force the optimizer always use this plan.

提交回复
热议问题