-2

What are the implications (on performance and other aspects) of having Rails Associations between ActiveRecord Models that are not mapped to a Database relationship

I have seen this in a real project and have searched for these implications without any luck

These are some of the documents I have found

http://guides.rubyonrails.org/association_basics.html

http://www.ibm.com/developerworks/library/os-railsn1/

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99

1 Answers1

0

May not be an answer but it would make for a long comment and may add value to the OP.

Correct me if I am wrong but I believe what the OP is getting at is that the code creates the relation but the database does not have a key relationship using foreign and primary keys inside the actual database. Since rails uses Object-Relational (ORM) structure instead of a Database-Relational structure.

Not sure if there are really any draw back implications (although I am sure many would disagree) and the second article seems to focus mostly on n+1 issues which you can resolve by writing appropriate code. Many n+1 issues spring out of code where people do not want to be explicit with their attribute selections and thus rails obliges by returning all attributes of a record. then when you request that objects relationship rails graciously runs another query for you and returns what you've asked for but when you are doing this in large iterations this can cause a large load and create performance issues.

You can avoid these issues by explicitly selecting exactly what you want by using select and group as well as using include or eager_load statements. Yes these are more complicated to write originally as many of them will require more SQL and less railsy syntax but the performance increase can be drastic when querying large tables or multiple relationships.

These are statements you would generally write by hand in stored-procedures or queries in SQL. Rails still gives you these freedoms but many times they are ignored because they look more like SQL than ruby and I have seen more than once people complaining about both the fact that if they change the DB they will have to change this too and that it "looks bad". Well if you were writing this from scratch in a standard database both these issues still apply.

As many have stated "ORM is just a tool" and any tool that is used improperly can have drastic implications. As long as you understand the tools you are using ORM can be very powerful and far more concise but if you ignore features your tool provides expect to have consequences with an extensive reach.

If you use a chainsaw as a hammer expect to lose an arm

UPDATE

I am unaware of any improvements in performance due to relationships inside the database other than it offers a hint as to where to index values. (You can create indexes during rails migrations although the primary ones are created for you the relational ones generally are not). Adding indexing to relational fields will generally speed up the database performance whether or not the specific relationship is actually defined in the database. Someone Asked a Similar Question

Database Relationships are more about Data Integrity than anything else and these issues should be handled inside your models in an ORM design not in the database its self.

Here is another resource to take a look at

And a few more SO questions on the subject

In SQL Server 2008, do relationships make queries faster?

Does Foreign Key improve query performance?

What's wrong with foreign keys?

Is there a severe performance hit for using Foreign Keys in SQL Server?

SQL Server Foreign Key constraint benefits

You get the idea just about asking the right question.

Community
  • 1
  • 1
engineersmnky
  • 25,495
  • 2
  • 36
  • 52
  • The first paragraph is exactly what I mean. Are there any resources where I can read more about the posible implications. And thanks for the rest of the post is really usefull. I need to know if is worth creating those relationships in the dabatabase and improve the performance – Mauricio Gracia Gutierrez Jul 29 '14 at 15:37