Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4016798
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:47:03+00:00 2026-05-20T09:47:03+00:00

I am using the Grails framework for a project, which uses Hibernate for ORM.

  • 0

I am using the Grails framework for a project, which uses Hibernate for ORM. Grails supports named queries, which translate to hibernate criteria. I’ve been using P6Spy and SQLProfiler to try to increase performance of one of these named queries.

The issue is that when I run the query with no order by clause, it runs in about .1200 seconds. When I add the order by clause, it increases to 15 seconds.

Here’s (roughly) what the end result query looks like:

Select 
    * 
from 
    article
left outer join 
    feed_articles 
on 
    article.id = feed_articles.article_id
where 
    ( 
        feed_articles.feed_id = 1 or 
        feed_articles.feed_id = 43 or 
        feed_articles.feed_id = 67
    )
order by 
    article.updated
limit
    50

In case there are any grails experts reading, here’s a stripped down version of the article domain:

static hasMany = [articleFeeds: ArticleFeed]

Date updated

static namedQueries = {

    containedInFeeds { feedList ->
        articleFeeds{
            or{
                feedList.each{ feed ->
                    eq("feed", feed)
                }
            }
        }
    }
}

Grails Note
I’ve tried this both by adding

orderBy("updated", "desc") 
maxResults("50") 

in the domain, and by trying

Article.containedInFeeds().listDistinct(max: 50, sort: "updated", order: "desc")

in the controller, but the resulting SQL seems to be identical either way.

I’ve tried adding an index to the article table for the updated field, but that lead to no performance improvements.

The article table has roughly 190,000 items.

I also ran an explain on the queries (as a blog post I came across suggested), and noticed that without the order by, only “Using Where” is listed as extras. When using the ordered by “Using Where, Using Temporary, Using Filesort” are all listed.

I’m guessing that I have not created an index properly, that the mysql instance needs some tuning done, or both.

Edit

More information

I’ve been searching the web for more information on indexing, especially on when joins and order by’s are used. The closest write-up I’ve been able to find is this: http://hackmysql.com/case5 but the site says it’s not longer maintained, and I’m guessing those techniques are obsolete (they didn’t work for me at least). I’d really like to gain an understanding of how/why things should be indexed, and not simply be given the answer.

Here are the requested table definitions and explain output.

Article Table

CREATE TABLE  `mydb`.`article` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `version` bigint(20) NOT NULL,
    `link` varchar(255) DEFAULT NULL,
    `image_id` bigint(20) DEFAULT NULL,
    `unique_id` varchar(255) DEFAULT NULL,
    `author` varchar(255) DEFAULT NULL,
    `title` varchar(255) DEFAULT NULL,
    `source` varchar(255) DEFAULT NULL,
    `updated` datetime DEFAULT NULL,
    `description` varchar(1000) DEFAULT NULL,
    `date_created` datetime NOT NULL,
    PRIMARY KEY (`id`),
    KEY `FK317B135BE74D38` (`image_id`),
    KEY `Updated_Idx_Test` (`id`,`updated`) USING BTREE,
    CONSTRAINT `FK317B135BE74D38` FOREIGN KEY (`image_id`) REFERENCES `remote_image`  (`id`)
    )
 ENGINE=InnoDB AUTO_INCREMENT=195939 DEFAULT CHARSET=latin1;

_Feed_Articles table_

CREATE TABLE  `mydb`.`feed_articles` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `version` bigint(20) NOT NULL,
    `article_id` bigint(20) NOT NULL,
    `feed_id` bigint(20) NOT NULL,
    PRIMARY KEY (`id`) USING BTREE,
    KEY `FK9E0121145D70E756` (`article_id`),
    KEY `FK9E012114A51FD776` (`feed_id`),  
    CONSTRAINT `FK9E0121145D70E756` FOREIGN KEY (`article_id`) REFERENCES `article` (`id`),  
    CONSTRAINT `FK9E012114A51FD776` FOREIGN KEY (`feed_id`) REFERENCES `feed` (`id`)
    ) 
ENGINE=InnoDB AUTO_INCREMENT=231684 DEFAULT CHARSET=latin1;

Explain Output

I did slightly modify the query, by changing the where clause to pull from 10 feed_id’s:

 feed_articles.feed_id = 1 or
 .
 .
 .
 feed_articles.feed_id = 10

Output:

+----+-------------+-----------+--------+---------------------------------------+--------------------+---------+----------------------------+-------+----------------------------------------------+
| id | select_type | table     | type   | possible_keys                         | key                | key_len | ref                        | rows  | Extra                                        |
+----+-------------+-----------+--------+---------------------------------------+--------------------+---------+----------------------------+-------+----------------------------------------------+
|  1 | SIMPLE      | feed_item | range  | FK9E0121145D70E756,FK9E012114A51FD776 | FK9E012114A51FD776 | 8       | NULL                       | 51909 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | item      | eq_ref | PRIMARY,Updated_Idx_Test              | PRIMARY            | 8       | wiumidev.feed_item.item_id |     1 |                                              |
+----+-------------+-----------+--------+---------------------------------------+--------------------+---------+----------------------------+-------+----------------------------------------------+

Thanks for the help so far.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:47:04+00:00Added an answer on May 20, 2026 at 9:47 am

    Yes, of course. You need to create indexes.
    Try to create on feed_articles.feed_id (first) and a composite index on article.id, article.updated (second).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Grails 1.3.4, which uses Hibernate, going against an Oracle 11g database. My
I have been working Quartz framework in my grails project with lib called quartz-all-1.7.3.
I have been working on the project where application is using grails 1.2.2.... Now
I'm using the layout support (sitemesh) in Grails which works fine. I'd like to
I plan to build a database management system using Grails as the main framework.
I have been using Grails for the past few months and I really like
As I've been using Grails more and more, I find myself writing code in
I have been using Grails for last 3 weeks (learning and working). I have
I'm writing a web app using grails and spring-security 3.0.3 which requires me to
I'm using Grails 1.1 beta2. I need to import a large amount of data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.