google-cloud-platform - Google Cloud Spanner 是否支持索引交集/组合/合并?

标签 google-cloud-platform google-cloud-spanner

有关相关功能的说明:

Postgres(索引组合)、MySQL(索引合并)和 MongoDB(索引交集)有一个功能,当给定查询没有找到多列索引时,数据库使用多个单列索引(索引) where 子句中有多个列。以下是 Postgres 的文档对此功能的介绍 - https://www.postgresql.org/docs/8.3/indexes-bitmap-scans.html

链接摘录

Beginning in release 8.1, PostgreSQL has the ability to combine multiple indexes (including multiple uses of the same index) to handle cases that cannot be implemented by single index scans. The system can form AND and OR conditions across several index scans. For example, a query like WHERE x = 42 OR x = 47 OR x = 53 OR x = 99 could be broken down into four separate scans of an index on x, each scan using one of the query clauses. The results of these scans are then ORed together to produce the result. Another example is that if we have separate indexes on x and y, one possible implementation of a query like WHERE x = 5 AND y = 6 is to use each index with the appropriate query clause and then AND together the index results to identify the result rows.

我的用例:

我想构建一个 UI,用户可以在其中使用表中的多个字段(目前有 30 多个字段并且还在不断增加)搜索(过滤)实体。过滤的实体数量需要显示在 UI 中,并在用户对过滤器进行的每次更新时刷新。因此,它需要很快(最好< 1s)。为所有可能的组合创建多个列索引是不可行的,而且即使完成也可能效率低下。

以下是我通过运行一些查询观察到的结果。

Case 1:
select count(*) from TableName@{FORCE_INDEX=_BASE_TABLE} where stringColumn = 'str1';
Table Scan: TableName (full scan: true)    ~11.72s


Case 2:
select count(*) from TableName where stringColumn = 'str1';
Index Scan: IndexForStringColumn    1.55s


Case 3:
select count(*) from TableName where ts > '2019-01-01';
Index Scan: IndexForTS    450902    1    985.66 ms


Case 4:
select count(*) from TableName where stringColumn = 'str1' and ts > '2019-01-01';
Index Scan: IndexForTS    450903    1    1.07 s
  • 情况 1 至 3。正如预期的那样。案例 1 没有使用任何索引,因此 TableScan 11.72 秒。
  • 案例 4 属于异常情况。据说只用过 TS 索引。但运行时间似乎要低得多(1.07s)。好像 这也使用了 IndexForStringColumn。

问题:

  1. Google Cloud Spanner 是否支持在单个查询中使用多个单列索引的功能?当我尝试在 Cloud Spanner 中运行一些基准测试时,它看起来好像受支持,但没有关于此的官方文档。
  2. 如果不受支持,是否可以通过其他方式使用 Google Cloud Spanner 构建此功能?

预先感谢您的帮助!

最佳答案

  1. 不幸的是,索引交集和并集处于积压状态。 Cloud Spanner 将选择一个索引(如果适用),但范围仅限于单个索引。如果您有一个大的联合索引,将使用最具选择性的单列索引。

  2. 您始终可以通过重写 SQL 语句来制作索引交集和并集。例如,

从 x = 1 和 y = 1 的位置选择 *;

可以重写为

SELECT * FROM A WHERE key IN ((SELECT key FROM A WHERE x = 1) INTERSECT (SELECT key FROM A FROM y = 1));

同样,

从 x = 1 或 y = 1 的位置选择 *;

可以重写为

SELECT * FROM A WHERE key IN ((SELECT key FROM A WHERE x = 1) UNION (SELECT key FROM A FROM y = 1)); -- 如果您不希望有大量行满足任一谓词,则可以添加 ALL。

希望这有帮助。

关于google-cloud-platform - Google Cloud Spanner 是否支持索引交集/组合/合并?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57265851/

相关文章:

kubernetes - Kubernetes(GKE)cronjob不起作用

google-cloud-platform - Cloud Spanner - WHERE 子句中大量项目的读取性能

jpa - 使用 micronaut-data-hibernate-jpa 库连接到 jpa 时出现错误

ios - 将 Firebase(GoogleService-Info.plist 和 API key )迁移到 iOS App Store 上的生产环境?

google-cloud-platform - GCP 防火墙规则是单向应用还是双向应用?

python - 使用 Python 从谷歌云存储下载多个文件

ssl - 启用 SSL 的 GCP 运行状况检查

google-cloud-platform - 使用 Google Spanner 的导出/导入工具

google-cloud-platform - 在 Google Cloud Spanner 中的 ARRAY<STRING(MAX)> 上创建索引

java - GCP Spanner 中的动态查询生成器