索引表以进行高级搜索

时间:2021-01-02 06:32:44

标签: mysql indexing database-design

假设我有一个表 shapes 表,其中的列类似于

create table shapes (
  id int,
  region_id int,
  clusted_id int,
  color varcahr(60),
  edges int,
  x int, y int, z int,
  orientation int,
  created_at date,
  updated_at date,
  visible tinyint
  ...
);

我需要创建一个“高级搜索”功能来查询这个超过 200 万行的表。用户可以根据所有可用列的混合和匹配进行查询。类似的东西

select * from shapes where created_at between ? and ? and x+y < ? and region_id in (?, ?, ?) ...

where 子句中可以出现出现的字段或未出现的字段。这就是“高级”搜索。此外,没有什么是全文。假设所有内容都是 int、date、boolean 或 enum。

  • 假设形状均匀分布在所有列的所有可能值中,为该表编制索引的最佳做法是什么?
  • 我是否必须将表中字段的所有子集索引在一起?
  • 是否有其他外部服务可以帮助提高性能?喜欢弹性搜索?

谢谢!

1 个答案:

答案 0 :(得分:1)

没有“好的”解决方案。

观察他们真正搜索的内容。然后创建一些索引,每个索引大约 2 列。制作复合索引时,从用 x+y < ? 测试的东西开始。根据用户填写的表单字段构建查询。不要费心索引不可搜索的表达式,例如 has_many

更多:

http://mysql.rjweb.org/doc.php/eav
http://mysql.rjweb.org/doc.php/index_cookbook_mysql