在 slick table 模式中使用 index 或 foreignKey 有什么好处吗?

Is there any benefit using index or foreignKey in slick table schema?

我应该在 slick schema 中使用索引和 foreignKey table 吗?是否有性能或查询规划器等任何好处? 我们正在使用 Flyway 数据库迁移工具,因此我们不会将此模式与 schema.create

一起使用

设置索引可以帮助您加快搜索(数据检索)速度,但会减慢插入速度。所以你需要根据你的要求来决定你想要什么。如果有更多的搜索和数据库中的数据是巨大的,你应该去索引。

另一方面,外键用于维护用于连接关系数据库的不同表之间的关系。添加外键不会对性能有任何影响。

您可以在此处获得有关索引的更多见解 -> Indexing, 对于这里的外键 -> Foreign Key

光滑模式中的外键 table 在两种情况下会帮助你

第一个来自slick doc

...foreign key can be used to navigate to the referenced data with a join. For this purpose, it behaves the same as a manually defined utility method for finding the joined data ...

并且如果您使用 slick 生成数据库模式(例如,在测试中)