简体   繁体   English

Sql Server 2008 FullText优化

[英]Sql Server 2008 FullText Optimization

I am playing a little with fulltext indexes in Sql Server 2008. I created the index, catalog and full populate it. 我在Sql Server 2008中使用全文索引玩一点。我创建了索引,目录并完整填充它。 My table has almost 400,000 records. 我的桌子有近400,000条记录。 My full text index is defined to a varchar(Max) column (lets call it Text for now). 我的全文索引被定义为varchar(Max)列(现在我们称之为Text)。 I am executing the follow query: 我正在执行以下查询:

select * from MyTable where contains(Text, 'house'). select * from MyTable where contains(Text,'house')。

This query returns almost 20,000 records in 14 seconds. 此查询在14秒内返回近20,000条记录。 I think thats very slow. 我觉得这很慢。 But, when i execute this query: 但是,当我执行此查询时:

select Count(*) from MyTable where contains(Text, 'house') 从MyTable中选择Count(*)包含(Text,'house')

It takes only 1 second to show the result. 只需1秒即可显示结果。

I've been looking and as far as I can see, the execution plan is the same for both queries. 我一直在寻找,据我所知,两个查询的执行计划都是一样的。 Why is Sql Server taking that much to show the fist query result? 为什么Sql Server会花费那么多来显示第一个查询结果?

What I have already did: 我已经做了什么:

I bought a SSD and put both mdf and ldf on this disk. 我买了一个SSD并将mdf和ldf放在这个磁盘上。 But when I execute the first query, i can see that logs and a tmp database are being created at my HDD (not in the SSD). 但是当我执行第一个查询时,我可以看到我的硬盘(而不是SSD)中正在创建日志和tmp数据库。 My SSD is D: and all temp files are being created at C:. 我的SSD是D:并且所有临时文件都是在C:创建的。

Is this taking so long because sql needs those files to populate the enterprise manager result grid? 这需要这么长时间,因为sql需要这些文件来填充企业管理器结果网格吗? is there anything i can do to optimize the Database? 有什么我可以做的优化数据库? I really need the queries to run in less than 2 seconds. 我真的需要在不到2秒的时间内运行查询。

Do you really need to display all that text, or do you want to run some more queries on the results and narrow them further? 您是否真的需要显示所有文本,或者您是否希望对结果运行更多查询并进一步缩小范围? I assume that most of the 14 seconds are used to display the results -- if you don't need to show them or only want to show some subset of these results, execution time should go down. 我假设14秒中的大部分都用于显示结果 - 如果您不需要显示它们或只想显示这些结果的某些子集,则执行时间应该会减少。

Is there any reason why you need to return 20,000 records? 您有什么理由需要返回20,000条记录吗? Could you add paging to the query so you return the result set in chunks of 20, 100, 1000 or something smaller than 20,000? 您是否可以向查询添加分页,以便以20,100,1000或小于20,000的块的形式返回结果集? It's going to take time to return a result set that large, no matter how much you optimize. 无论您优化多少,都需要时间来返回大的结果集。

Your query's extra time is probably from populating the grid in Management Studio. 您的查询的额外时间可能来自填充Management Studio中的网格。

Keep in mind that when you do a SELECT * you're also including the Text column itself in the grid results. 请记住,当您执行SELECT *时,您还要在网格结果中包含Text列本身。 Try selecting only columns other than your Text column and you will probably see the execution time drop closer to that of your SELECT COUNT(*) query. 尝试仅选择Text列以外的列,您可能会看到执行时间下降更接近SELECT COUNT(*)查询的执行时间。 You might want to also include DATALENGTH(Text) in your select list. 您可能还想在选择列表中包含DATALENGTH(文本)。

There's also an option in Management Studio that controls the maximum characters retrieved into the grid. Management Studio中还有一个选项,用于控制检索到网格中的最大字符数。 You can find it under Tools --> Options --> Query Results --> Results to Grid. 您可以在工具 - >选项 - >查询结果 - >结果到网格下找到它。

I agree with Christian. 我同意克里斯蒂安。 Your problem is with the rendering of the result. 你的问题是渲染结果。 Try output to Text rather than Grid to eliminate some of this overhead. 尝试输出到Text而不是Grid来消除一些开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM