Postgres Trigram和订购非常慢

时间:2018-07-18 09:53:18

标签: sql postgresql group-by

我正在尝试针对多列建立模糊搜索,并对每列与相应搜索词之间的距离进行加权。

我有以下查询-

var objs = [ 
  { first_nom: 'Lazslo', last_nom: 'Jamf'     },
  { first_nom: 'Pig',    last_nom: 'Bodine'   },
  { first_nom: 'Pirate', last_nom: 'Prentice' }
];

var sortBy = function (fn) {
  return function (a, b) {
    var fa = fn(a)
    var fb = fn(b)
    return -(fa < fb) || +(fa > fb)
  }
}

var getLastName = function (o) { return o.last_nom }
var sortByLastName = sortBy(getLastName)

objs.sort(sortByLastName)
console.log(objs.map(getLastName))

具有如下索引-

<?php YII_DEBUG === true ? Yii::app()->clientScript->scriptMap = array('jquery.js'=>'/js/jquery-3.3.1.js') : Yii::app()->clientScript->scriptMap = array('jquery.min.js'=>'/js/jquery-3.3.1.min.js') ?>
<?php Yii::app()->getClientScript()->registerCoreScript('jquery'); ?>

用于匹配的列。

我们表中有约500,000条记录,查询耗时超过三秒钟。

我在合并函数上也有表达索引

有什么办法可以加快速度吗?

解释结果-

select sf_id 
from (
  select * 
  from (
     select sf_id , 
            (1.0 - cast(coalesce(mailingcity, '') <->> 'san ant' as float)) * 3.0  as score  
     from contacts 
     order by score desc 
     limit 1000
  ) as mailingcity

  union

  select * 
  from (
    select sf_id, 
           (1.0 - cast(coalesce(lastname, '') <->> 'anders' as float)) * 5.0 as score  
    from contacts 
    order by score 
    desc limit 1000
  ) as lastname
) 
as agg 
group by sf_id 
order by sum(score) desc

根据下面@a_horse_with_no_name的建议,以下查询现在在〜250ms上运行

create index contact_lastname_trgrm_idx on contacts using gin (lastname gin_trgm_ops) 

0 个答案:

没有答案