zoukankan      html  css  js  c++  java
  • 向SQL Server全文索引进军,艰难历程

     

          写这篇文章本来不在计划之内,以前也没有接触过全文索引。偶然的一次备课中。突然有了灵感。决定将自己创建及其使用全文索引的过程记录下来,以备后续使用。

    至于全文索引的概念,网上一大堆,这里就不再赘述。直接切入正题。

    创建全文索引步骤:

    1:对着数据库点击右键-选择属性-选择文件,选中“使用全文索引”

     

    该选项只要勾选就可以了,默认都是勾选的。

    2:对着表点击右键-全文索引-定义全文索引

    3:点击下一步,如果这个表中没有唯一性索引就会出现下图所示

     

    4:选择表列,选择断字符语言。

    5:点击下一步,这里的选项要注意,如果不想再表、视图更改的时候更新全文索引,那就选择不跟踪更改;这样就可以选择是否在创建索引时启动完全填充了。

    6:点击下一步创建索引要保存的目录,全文索引的索引文件是以文件的形式保存到硬盘上的。

    7:之后就可以设置自动填充、手动跟踪更改,还有设置计划了。

    上述步骤虽然简单,但是我在创建时,碰到了N多问题

    问题描述:

    01.首先我对表点击右键的时候,全文索引菜单项是灰色,不可用状态

    如下图

     

    通过查询资料发现需要执行如下命令开启该菜单项目   

     EXEC sp_fulltext_database 'ensable'

    这样全文索引就变成可用状态了。

    如果想让全文索引重新变暗(当然一般人不会这么做),可以执行如下代码

     EXEC sp_fulltext_database 'disable'

    02.在使用全文索引进行检索时,必须要开启全文检索服务

     

    默认该服务是关闭的。

    03.当我创建好全文索引后,通过如下语句进行索引关键字时

    Select content from info infoinfo

    where contains(content, '但为了获得最佳性能,建议使用主键约束或唯一约束。')

    出现了如下提示:

    "SQL Server 在与全文筛选器后台程序宿主(FDHost)进程通信时遇到错误 0x80070218。请确保 FDHost 进程正在运行。若要重新启动 FDHost 进程,请运行 sp_fulltext_service 'restart_all_fdhosts' 命令或重新启动 SQL Server 实例。 "

    经过反复摸索, 发现需要在 SQL配置工具里面, 更改全文服务的运行账户, 有默认的 "系统内设"-->"本地账户", 搞定!

     

    但是更改完该设置后,我依然没能笑出声来,原因是接下来虽然没有错误发生,但是却检索不出我需要的结果,但通过like进行检索,的确有数据。

    select content from infoinfo

     where content like '%但为了获得最佳性能,建议使用主键约束或唯一约束。%'

    这时候,我差点崩溃了。没办法,继续搜索资料。

    最后,从微软官网上搜索到这样一篇文章

     

    里面详细讲述了产生该问题的原因,以及对应的解决方案

    请注意

    重要提示: 本文包含有关修改注册表的信息。 在修改注册表之前,请务必对其进行备份,并确保您了解如何还原注册表发生问题。有关如何备份、 还原,和编辑注册表单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

    256986Microsoft Windows 注册表的说明

    症状

    具有 300 KB 以上的文本 (超过 150 KB ntext列的 UNICODE 文本) 的文本的全文启用的列上运行全文索引包含查询时,您可能会收到一条消息,"0 行受影响",即使存在有效的结果。

    原因

    Microsoft 搜索服务 (MSSearch.exe) 维护 SQL Server 7.0 全文搜索来搜索的基于字符的数据类型,如字符、 varchar文本ntext的全文目录。文本可以有多个 300 KB 的数据根据您定义的列 (ntext可以有多个 150 KB 的 UNICODE 文本)。全文索引填充时,MSSearch 服务从文本列中检索所有非重复的单词,并将它们存储在虚拟内存中,它将它们合并到主索引之前。MSSearch 属性可防止 MSSearch 服务占用的虚拟内存。MSSearch 属性由下面的注册表和它的值控制:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Search\1.0\Indexer\SQLServer\ <Full_Text_Catalog_Name> \ MaxBytesPerFile

    例如: Full_Text_Catalog_Name = SQL0000500005
    此注册表项的默认值设置为 307200 (300 KB)。因此,MSSearch 服务假定 300 KB 的文本作为重要和填充全文索引时将忽略文本列的其余的数据。
    因此,当您运行全文的 CONTAINS 子句查询来搜索后 300 KB 的文本将出现在文本栏中的单词,不可能获得有效的结果。

    替代方法

    警告: 如果注册表编辑器使用不当,可能会导致严重的问题,可能需要您重新安装操作系统。注册表编辑器使用不当所产生的问题,Microsoft 不能保证您可以解决。使用注册表编辑器需要您自担风险。
    若要变通解决此问题:

    1. 开始菜单上,单击运行
    2. 键入 regedit,然后单击确定
    3. 在注册表编辑器中找到下面的项:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Search\1.0\Indexer\SQLServer\Full_Text_Catalog_Name\

    (例如: Full_Text_Catalog_Name = SQL0000500005)。

    1. 单击类型 REG_DWORD 的MaxBytesPerFile项。
    2. 编辑菜单上,单击修改,然后指定根据您的要求的注册表项的数值数据
    3. 退出注册表编辑器。
    4. 重新生成全文目录。

    但是真正按照这个文档进行操作时,再次碰到了问题,就是第3步到1.0后,没有Indexer文件夹,我晕,只能再次硬着头皮查资料。结果,网上资料很少,却搜到一篇一模一样的英文文章,是这样写的

    PRB: A Full-Text Search Query on TEXT or NTEXT Column Does Not Return Valid Results

    Article ID: 241113 - View products that this article applies to.

    This article was previously published under Q241113

    Notice

    IMPORTANT: This article contains information about modifying the registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, click the following article number to view the article in the Microsoft Knowledge Base:

    256986

    (http://kbalertz.com/Feedback.aspx?kbNumber=256986/EN-US/ )

    Description of the Microsoft Windows Registry

    Expand all | Collapse all

    SYMPTOMS

    When you run a full-text CONTAINS query on a full-text enabled text column that has more than 300 KB of text (more than 150 KB of UNICODE text for an ntextcolumn ), you may receive a "0 rows affected" message even though valid results are present.

    Back to the top | Give Feedback

    CAUSE

    The Microsoft Search service (MSSearch.exe) maintains the full-text catalogs for SQL Server 7.0 full-text search to search character-based data types such as char,varchartext and ntext. Columns that you define as text can have more than 300 KB of data (ntext can have more than 150 KB of UNICODE text). When the full-text index is populated, MSSearch service retrieves all the distinct words from the text column and stores them in the virtual memory before it merges them into the master index. The MSSearch property prevents the MSSearch service from consuming all the virtual memory. The MSSearch property is controlled by the following registry and its value:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Search\1.0\Indexer\SQLServer\ <Full_Text_Catalog_Name>\ MaxBytesPerFile

    For example: Full_Text_Catalog_Name = SQL0000500005

    The default value of this registry key is set to 307200 (300 KB). As a result, MSSearch service assumes 300 KB of text as significant and ignores the remaining data of the text column while it populates the full-text index. 

    Therefore, when you run a full-text CONTAINS clause query to search a word that is present in the text column after 300 KB of text, you may not obtain valid results.

    Back to the top | Give Feedback

    WORKAROUND

    WARNING: If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk. 

    To work around this problem:

    1. On the Start menu, click Run.
    2. Type regedit, and then click OK.
    3. Locate the following key in Registry Editor:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Search\1.0\Indexer\SQLServer\Full_Text_Catalog_Name\

    (for example: Full_Text_Catalog_Name = SQL0000500005).

    1. Click the MaxBytesPerFile key of type REG_DWORD.
    2. On the Edit menu, click Modify, and then specify the Value Data of the registry key based on your requirement.
    3. Quit Registry Editor.
    4. Rebuild the full-text catalog.

         这里列出该文章的目的,是为了后续使用(顺便也来个英汉对照,学习下英文),当我看完时,突然想到一个问题,就是既然没有Indexer文件夹,我能不能手动创建一个呢?结果我一个一个文件创建,最终创建出来的内容如下图

     

    这里我将文本最大大小设置成了30M。

    然后删除全文索引,从新创建后,一切都搞定。最终我看到了我要检索的数据。

    但是这里有个问题,由于我数据库中的数据量比较少,和like检索效率无法对比,最后只能狂加数据,稍微能看出一点效果。

    谨以此献给耗时4个小时的自己。

         版权所有(微冷的雨),请勿转载,如转载请注明出处 。

                                                                                                                      2013年6月15日星期六 上午

  • 相关阅读:
    JavaScript Patterns and Good Parts
    [Buzz Today]2012.05.015
    The first glance at Python
    The first glance at JavaScript
    [Buzz Today]2012.05.06
    vs.net2008 下 带完善后台管理的flash+js 轮显广告
    Vs.net2008 下 Ajaxpro 使用
    允许asp上传大文件的设置
    使用AutocompleteExtender无效或者没反应,输入啥都在webservice里面跟踪不到
    C#精髓 GridView 72般绝技
  • 原文地址:https://www.cnblogs.com/weilengdeyu/p/3137383.html
Copyright © 2011-2022 走看看