SQL QUERY TIEMOUT Using Ruby on Rails

begin
  Timeout::timeout(5) {
    @db.query(select * from books")
  }
rescue Timeout::Error
  puts "Too slow, forget about it."
end

For instance, In above statment, @db.query will take 10 seconds to
complete.

But,

I would like to kill that query after 5 seconds.

how to kill the running @db.query in rescue statment.? With in 5
seconds whatever records fetched from db, How can i get those results?

Any ideas??..

As far as I understand the mechanisms of a RDBMS thats an "all or
nothing" strategy. You could kill the task after 5 secs, but wouldn't
get anything or wait until finished and have it all.

Another way would be to fetch in bunches of about 1000 Datasets and
stop the time, and when the 5 secs are gone, you can use all the
"bunches" you queried so far

HTH
Norbert