简体   繁体   English

何时以及如何在Grails中创建索引?

[英]When and how do I have to create an Index in Grails?

In Grails you can add custom indices to your domain classes. 在Grails中,您可以向域类添加自定义索引

  1. Does Grails generate indices by default for my tabels? Grails是否默认为我的表格生成索引?
  2. Is there a rule which columns I have to use for my index? 我必须使用哪些列用于索引吗?
  3. Do my queries change when an index is set? 设置索引时我的查询是否会更改?

This isn't really a Grails question, except for the part about when and if Grails creates indexes. 这不是Grails的问题,除了关于何时以及Grails创建索引的部分。 You need them like you would in any application that uses a database - create them to improve lookup performance. 您需要它们,就像在任何使用数据库的应用程序中一样 - 创建它们以提高查找性能。

Grails doesn't actually create any, Hibernate does that when it generates the DDL that creates your tables. Grails实际上并没有创建任何东西,Hibernate在生成创建表的DDL时会这样做。 You can see this DDL at any time by running grails schema-export - the generated file will be target/ddl.sql . 您可以通过运行grails schema-export随时查看此DDL - 生成的文件将是target/ddl.sql

In general you'll see unique constraints which will typically create a unique index, and in MySQL and some other databases you'll see indexes created on foreign keys (but this isn't done for Oracle for some reason). 通常,您将看到通常会创建唯一索引的唯一约束,并且在MySQL和其他一些数据库中,您将看到在外键上创建的索引(但由于某种原因,这不是为Oracle完成的)。

There is some mapping support for getting Hibernate to create indexes as you noted in your question, but in general you'll need to create them yourself since they are often database-specific. 正如你在问题中提到的那样,有一些映射支持让Hibernate创建索引,但一般来说你需要自己创建它们,因为它们通常是特定于数据库的。 Use the http://grails.org/plugin/database-migration plugin for this. 使用http://grails.org/plugin/database-migration插件。

In general you will use indexes on columns that are part of frequent queries and queries with high execution cost. 通常,您将在频繁查询和具有高执行成本的查询的列上使用索引。 This will happen using any relational database and any development framework. 使用任何关系数据库和任何开发框架都会发生这种情况。

About Grails, I found this post very useful in how indexes are defined in Grails: http://grails.asia/grails-how-to-create-custom-table-index-or-composite-index 关于Grails,我发现这篇文章对于如何在Grails中定义索引非常有用: http//grails.asia/grails-how-to-create-custom-table-index-or-composite-index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM