我有一张名为“test”的表,其中包含数百万条记录。每行都包含一个浮点型“特征”,以及该特征在“id”项中出现的“次数”。该表的主键是“id”和“feature”的组合,即每个项可能具有多个特征。通常每个项目ID会有几百到几千个特征条目。
create table test
(
id int not null,
feature double not null,
count int not null
);
任务是查找与给定参考项最相似的500个项。相似性是根据两个项中相同的特征值数量来衡量的。我想到的查询如下所示,但即使正确使用索引,其执行计划仍包含“using temporary”和“using filesort”,这对我的用例来说性能不可接受。
select
t1.id,
t2.id,
sum( least( t1.count, t2.count )) as priority
from test as t1
inner join test as t2
on t2.feature = t1.feature
where t1.id = {some user supplied id value}
group by t1.id, t2.id
order by priority desc
limit 500;
有没有关于如何改进这个的想法?方案可以根据需要进行修改并添加索引。
SHOW CREATE TABLE test
的输出结果? - Quassnoitest
(id
int(11) NOT NULL,feature
double NOT NULL,count
int(11) NOT NULL, KEYidx_one
(feature
), KEYidx_two
(id
) ) ENGINE=InnoDB DEFAULT CHARSET=utf8' - BuschnicKSELECT COUNT(DISTINCT id) FROM test
返回什么? - Quassnoi