1

Please, help to optimize object destroing in Rails application: I have relatively big database, and when I want to delete user from it all dependent objects removing takes > 1 minute. It's very long. Database has indexes for field, used to find data, without indexes it takes > 5 minutes.

I see that every one query takes a little time: 0.1 - 1ms. but there many queries. Question is: how can I tell rails to remove ALL related objects using one database transaction? I think this should speed up this operation and some other operations too.

Thanks

user1859243
  • 295
  • 1
  • 5
  • 11
  • Can you expound a little bit? When you say "big database" are you referring to Big Data? If so, I highly doubt you'll be able to get it much faster than that without delegating the different tasks to parallel processes. – trevorhinesley May 18 '13 at 16:07
  • 1
    Check out http://stackoverflow.com/questions/2797339/rails-dependent-destroy-vs-dependent-delete-all --- you may benefit from moving to `dependent: :delete_all` – Jesse Wolgamott May 18 '13 at 17:30

1 Answers1

0

If you want to delete an object and all of it's dependancies, use the destroy method:

Post.last.destroy

If you want to quickly destroy all Posts, use the destroy_all method:

Post.destroy_all

fbonetti
  • 6,652
  • 3
  • 34
  • 32
  • 1
    Actually, I'm doing something like users = User.where(:some => condition); users.destroy. And this .destroy method deletes > 150 000 objects from database with ~ 250 sql queries. Question is how to speed up that queries, may be with transaction or some other way... – user1859243 May 18 '13 at 16:40