{"id":23082502,"url":"https://github.com/steventen/sql_tracker","last_synced_at":"2025-06-22T04:05:56.066Z","repository":{"id":11408921,"uuid":"69583739","full_name":"steventen/sql_tracker","owner":"steventen","description":"Rails SQL Query Tracker","archived":false,"fork":false,"pushed_at":"2022-04-05T15:37:11.000Z","size":38,"stargazers_count":176,"open_issues_count":1,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-24T22:06:43.322Z","etag":null,"topics":["rails","sql","tracking"],"latest_commit_sha":null,"homepage":"https://stevenyue.com/blogs/tracking-sql-queries-in-rails/","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/steventen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-29T15:59:11.000Z","updated_at":"2024-05-01T20:14:10.000Z","dependencies_parsed_at":"2022-08-07T06:16:16.845Z","dependency_job_id":null,"html_url":"https://github.com/steventen/sql_tracker","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/steventen/sql_tracker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steventen%2Fsql_tracker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steventen%2Fsql_tracker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steventen%2Fsql_tracker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steventen%2Fsql_tracker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/steventen","download_url":"https://codeload.github.com/steventen/sql_tracker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steventen%2Fsql_tracker/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261233204,"owners_count":23128196,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rails","sql","tracking"],"created_at":"2024-12-16T14:53:59.619Z","updated_at":"2025-06-22T04:05:51.027Z","avatar_url":"https://github.com/steventen.png","language":"Ruby","readme":"# Rails SQL Query Tracker\n\n[![Code Climate](https://codeclimate.com/github/steventen/sql_tracker/badges/gpa.svg)](https://codeclimate.com/github/steventen/sql_tracker)\n[![Build Status](https://travis-ci.org/steventen/sql_tracker.svg?branch=master)](https://travis-ci.org/steventen/sql_tracker)\n\n`sql_tracker` tracks SQL queries by subscribing to Rails' `sql.active_record` event notifications.\n\nIt then aggregates and generates report to give you insights about all the sql queries happened in your Rails application.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngroup :development, :test do\n  ... ...\n  gem 'sql_tracker'\nend\n```\n\nAnd then execute:\n\n    $ bundle\n\n\n## Tracking\n\nTo start tracking, simply start your rails application server. When your server is shutting down, `sql_tracker` will dump all the tracking data into one or more json file(s) under the `tmp` folder of your application.\n\n`sql_tracker` can also track sql queries when running rails tests (e.g. your controller or integration tests), it will dump the data after all the tests are finished.\n\n### Tracking Using a Block\n\nIt is also possible to track queries executed within a block. This method uses a new subscriber to `sql.active_record` event notifications for each invocation. Results using this method are not saved to a file.\n\n```ruby\nquery_data = SqlTracker.track do\n  # Run some active record queries\nend\n\nquery_data.values\n# =\u003e\n# [{\n#  :sql=\u003e\"SELECT * FROM users\",\n#  :count=\u003e1,\n#  :duration=\u003e1.0,\n#  :source=\u003e[\"app/models/user.rb:12\"]\n# }]\n```\n\n## Reporting\n\nTo generate report, run\n```bash\nsql_tracker tmp/sql_tracker-*.json\n```\nThe output report looks like this:\n```\n==================================\nTotal Unique SQL Queries: 24\n==================================\nCount | Avg Time (ms)   | SQL Query                                                                                                 | Source\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n8     | 0.33            | SELECT `users`.* FROM `users` WHERE `users`.`id` = xxx LIMIT 1                                            | app/controllers/users_controller.rb:125:in `create'\n      |                 |                                                                                                           | app/controllers/projects_controller.rb:9:in `block in update'\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n4     | 0.27            | SELECT `projects`.* FROM `projects` WHERE `projects`.`user_id` = xxx AND `projects`.`id` = xxx LIMIT 1    | app/controllers/projects_controller.rb:4:in `update'\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n2     | 0.27            | UPDATE `projects` SET `updated_at` = xxx WHERE `projects`.`id` = xxx                                      | app/controllers/projects_controller.rb:9:in `block in update'\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n2     | 1.76            | SELECT projects.* FROM projects WHERE projects.priority BETWEEN xxx AND xxx ORDER BY created_at DESC      | app/controllers/projects_controller.rb:35:in `index'\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n... ...\n```\nBy default, the report will be sorted by the total count of each query, you can also choose to sort it by average duration:\n```bash\nsql_tracker tmp/sql_tracker-*.json --sort-by=duration\n```\n\n## Configurations\n\nAll the configurable variables and their defaults are list below:\n```ruby\nSqlTracker::Config.enabled = true\nSqlTracker::Config.tracked_paths = %w(app lib)\nSqlTracker::Config.tracked_sql_command = %w(SELECT INSERT UPDATE DELETE)\nSqlTracker::Config.output_path = File.join(Rails.root.to_s, 'tmp')\n```\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteventen%2Fsql_tracker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsteventen%2Fsql_tracker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteventen%2Fsql_tracker/lists"}