【问题标题】:How to apply index in descending order in grails domain class?如何在grails域类中按降序应用索引?
【发布时间】:2016-07-25 10:55:55
【问题描述】:

我写了一个领域类

class ReportCallByUser {
Integer userId;
String userName;
String reportName;
Date timeOfReportCall;

static constraints = {
}

static mapping = {

    timeOfReportCall index: 'time_of_report_call_index'
}

最后一行在数据库中创建一个索引,但按升序排列。如何按降序在“timeOfReportCall”上创建索引?
提前致谢。

【问题讨论】:

    标签: grails grails-orm grails-domain-class


    【解决方案1】:

    在创建索引方面没有 order 这样的东西。这是查询指定排序的能力:

    ReportCallByUser.list( [ sort:'timeOfReportCall', order:'[desc|asc]' ] )
    

    【讨论】:

    • 至少 postgres 允许索引具有“ASC、DESC、NULLS FIRST 和/或 NULLS LAST”。见postgresql.org/docs/current/static/sql-createindex.html
    • 很可能是这样,但你不能在 GORM 的 static mapping 构建器中实现它。喜欢timeOfReportCall index: 'time_of_report_call_index', order:'desc'
    【解决方案2】:

    我假设当您使用 GORM 查询数据库时,默认情况下您希望结果按降序排列。如果是这种情况,您可以在映射中指定排序顺序。

    static mapping = {
        sort timeOfReportCall:"desc"
    }
    

    希望对您有所帮助。

    【讨论】:

    • 我想应用索引,以便我可以通过 timeOfReportCall 的降序快速得到结果。在“静态映射”中截断的代码中,闭包按 ASCENDING 顺序创建索引。所以我正在寻找通过编码以降序应用索引的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多