【问题标题】:sql full text search keyword from table's column来自表列的sql全文搜索关键字
【发布时间】:2016-04-22 15:24:17
【问题描述】:

我有以下问题

INSERT INTO FirstNames select FirstName from temp_names where not exists 
(select FirstName from FirstNames where CONTAINS(FirstName, temp_names.FirstName))

但我收到错误消息:“temp_names”附近的语法不正确。

因为某些原因它看不到表 temp_names。

我尝试使用不包含如下内容的查询:

INSERT INTO FirstNames select FirstName from temp_names where not exists 
(select FirstName from FirstNames where FirstName = temp_names.FirstName)

它确实有效,但是当我达到 200 万条及以上的记录时,它变得非常沉重。

如何使用全文搜索并将列名传递给它,就像上面的第一个查询一样?

谢谢。

编辑

找到完全匹配与否并不重要

【问题讨论】:

  • 你想找到完全匹配的还是你想做一个FULL TEXT SEARCH
  • @VR46 我想找到完全匹配
  • Mysql Sql 服务器。你用的是哪一个
  • @VR46 很抱歉发生冲突,我使用的是 SQL server 而不是 mysql
  • 你不能像这样使用包含,如果你想找到完全匹配,那么 Contains 不会那样做。您的第二个查询对我来说很好

标签: sql sql-server full-text-search contains containstable


【解决方案1】:

对于这个查询:

INSERT INTO FirstName(FirstName)
    select tn.FirstName
    from temp_names tn
    where not exists (select 1
                      from FirstNames fn
                      where fn.FirstName = tn.FirstName
                     );

您想要在FirstName(FirstName) 上建立索引:

create unique index unq_firstname_firstname on FirstName(FirstName);

这应该会加快查询速度,假设 FirstName.FirstNametemp_names.FirstName 具有相同的数据类型和排序规则。

注意:如果temp_names 可能有重复项,那么您真的想要:

INSERT INTO FirstName(FirstName)
    select distinct tn.FirstName
    from temp_names tn
    where not exists (select 1
                      from FirstNames fn
                      where fn.FirstName = tn.FirstName
                     );

【讨论】:

  • 两个表的列具有相同的数据类型,并且两个表具有相同的排序规则。 temp_names 有重复的行,我应该创建一个索引并使用您提供的第二个查询还是使用第二个查询而不为该列编制索引?
  • 你想要有效解决这个问题的索引
  • 我知道了,非常感谢,今天我会测试一下,并会回复你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
相关资源
最近更新 更多