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

  • SEARCH
  • Home
  • 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 6788747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:31:19+00:00 2026-05-26T17:31:19+00:00

DataMapper appears to be generating grossly sub-optimal queries for associations that use a join

  • 0

DataMapper appears to be generating grossly sub-optimal queries for associations that use a join table. What can I do to improve the performance of these queries? Note that it generates the same queries with an implicit join table, as well (:through => Resource)

Here’s my setup:

class Left
  include DataMapper::Resource

  property :id, Serial

  has n, :joins
  has n, :rights, :through => :joins
end

class Join
  include DataMapper::Resource

  belongs_to :left,  :key => true
  belongs_to :right, :key => true
end

class Right
  include DataMapper::Resource

  property :id, Serial

  property :one, String
  property :two, Integer
end

Now, say I have some data populated and I want to grab all the rights associated with a left object:

Left.first.rights

DataMapper executes the following query:

SELECT rights.id, rights.one, rights.two FROM rights INNER JOIN joins ON rights.id = joins.right_idINNER JOIN lefts ON joins.left_id = lefts.idWHERE joins.left_id = 1GROUP BY rights.id, rights.one, rights.twoORDER BY rights.id

  • DataMapper is generating a completely unnecessary JOIN (first bold section). I don’t trust my RDBMS (MySQL) to be smart about ignoring this. It shows up in the explain plan, as “Using index; Using temporary; Using filesort”, at least.

  • What’s up with the GROUP BY? It also seems completely unnecessary here – due to the composite key on the join table, I can’t have duplicates. Plus, wouldn’t GROUP BY rights.id have the same effect? Or even better, SELECT DISTINCT

These queries are extremely slow (on tables with more properties on both sides), and I’m not sure how to properly index the tables to support it.

It’s far faster to query from the join model: Join.all(:left => Left.first).rights, though it is performing two statements:

SELECT right_id FROM joins WHERE left_id = 1
SELECT id, one, two FROM rights WHERE id = 1 ORDER BY id

Interestingly, Left.first.joins.rights goes the same route and executes the two queries above.

  • 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-26T17:31:19+00:00Added an answer on May 26, 2026 at 5:31 pm

    How about removing Join class definition, and defining Left as:

    class Left
      include DataMapper::Resource
    
      property :id, Serial
    
      has n, :rights, :through => Resource
    end
    

    Believe DM, it creates good mapping for you.

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

Sidebar

Related Questions

I need help constructing a CodeIgniter Datamapper ORM query that includes join fields. I
I have a padrino install that is using datamapper and logging queries to a
From this example: http://datamapper.wanwizard.eu/pages/getadvanced.html#Deep.Relationship.Include You can see that it says: At this time, deep
I'm working with DataMapper and trying to use associations between models Project and Task.
need to use a good PHP ORM that has elements of Datamapper and I
I was looking at DataMapper , which appeared at first glance to use the
I have a Sinatra DataMapper app hitting a sqlite3 database that I am attempting
I'm just diving into Datamapper (and Sinatra) and have a question about associations. Below
Checked ActiveRecord, DataMapper, Sequel: some use globals (static variables) some require open db connection
I've got two models that look like this class Stage include DataMapper::Resource property :id,

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.