pratski
pratski

Reputation: 1488

Rails: How to find out what query is being run in SQL when a page loads?

I want to know what queries are being run when a User is interacting with different pages on the website? I'd also like to know how long each query took. How and where can I see that?

Upvotes: 2

Views: 847

Answers (3)

fmendez
fmendez

Reputation: 7338

There's a log folder in every rails application with a .log file for each of the environment you've run your application in. You'll see these kind of stats in it:

Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2012-11-29 11:09:47 -0400
Served asset /jquery.js - 200 OK (3ms)

Started GET "/users/auth/google/callback?_method=post&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=id_res&openid.op_endpoint=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fud&openid.response_nonce=2012-10-26T19%3A29%3A52ZkvQlFeFr5Rk78g&openid.return_to=http%3A%2F%2Flocalhost%3A3000%2Fusers%2Fauth%2Fgoogle%2Fcallback%3F_method%3Dpost&openid.assoc_handle=AMlYA9VjNZ-QIIMe5bhvtPLBsAdm5xMltOa7MEwUoW4Opx9tXd_khhcS&openid.signed=op_endpoint%2Cclaimed_id%2Cidentity%2Creturn_to%2Cresponse_nonce%2Cassoc_handle%2Cns.ext1%2Cext1.mode%2Cext1.type.ext2%2Cext1.value.ext2%2Cext1.type.ext0%2Cext1.value.ext0%2Cext1.type.ext3%2Cext1.value.ext3&openid.sig=ehFAJ1m8nPces8%2Bj6Ud%2FicpuohY%3D&openid.identity=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fid%3Fid%3DAItOawljL9RKBE7iUQHk94UhJQ-4sDOTawUfpNc&openid.claimed_id=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fid%3Fid%3DAItOawljL9RKBE7iUQHk94UhJQ-4sDOTawUfpNc&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_response&openid.ext1.type.ext2=http%3A%2F%2Faxschema.org%2FnamePerson%2Ffirst&openid.ext1.value.ext2=Fernando&openid.ext1.type.ext0=http%3A%2F%2Faxschema.org%2Fcontact%2Femail&openid.ext1.value.ext0=fernando%40findhorsesforsale.net&openid.ext1.type.ext3=http%3A%2F%2Faxschema.org%2FnamePerson%2Flast&openid.ext1.value.ext3=Mendez" for 127.0.0.1 at 2012-10-26 15:29:52 -0400

Started GET "/run_dates/current_pdf_generation_status.js?pdf_generation_id=e3ef90b05844012f4fc2723c91dfe57c&_=1332637701414" for 127.0.0.1 at 2012-03-24 21:08:29 -0400
Processing by RunDatesController#current_pdf_generation_status as JS
  Parameters: {"pdf_generation_id"=>"e3ef90b05844012f4fc2723c91dfe57c", "_"=>"1332637701414"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  RunDate Load (0.1ms)  SELECT "run_dates".* FROM "run_dates" WHERE (pdf_generation_id = 'e3ef90b05844012f4fc2723c91dfe57c') LIMIT 1
  Rendered run_dates/current_pdf_generation_status.js.erb (0.1ms)
Completed 200 OK in 4ms (Views: 2.0ms | ActiveRecord: 0.3ms)
.....

Upvotes: 2

Muhamamd Awais
Muhamamd Awais

Reputation: 2385

Have a look at development.log if you are in development mode, it will show you all the queries with the time taken to ran that query. For production, see production.log and for staging look at staging.log

You can delete that file rails will create it again if you want to look at specific queries. but i would suggest instead of deleting, when you are hitting the page, print something different like '-----------------' that will differentiate other queries from the queries of the page you want to see.

Upvotes: 1

pierallard
pierallard

Reputation: 3371

If you are the User, you can use some browser plugin (e.g. Firebug)

enter image description here

If you are not the user, you can check the log to see what queries are done.

Upvotes: 0

Related Questions