{"id":23089228,"url":"https://github.com/steventen/where_in_vs_tmp_table","last_synced_at":"2025-07-29T06:32:44.561Z","repository":{"id":146880803,"uuid":"70746325","full_name":"steventen/where_in_vs_tmp_table","owner":"steventen","description":"SQL Query Between WHERE IN and TEMP TABLE","archived":false,"fork":false,"pushed_at":"2016-10-15T12:43:47.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-03T17:23:58.579Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"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":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-12T22:18:03.000Z","updated_at":"2016-10-12T22:19:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"0270f810-38bb-4f68-8ed8-8558b2293ad8","html_url":"https://github.com/steventen/where_in_vs_tmp_table","commit_stats":{"total_commits":6,"total_committers":1,"mean_commits":6.0,"dds":0.0,"last_synced_commit":"98ed69e857989cbc29cce6612645e2e58f8ec4ce"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/steventen/where_in_vs_tmp_table","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steventen%2Fwhere_in_vs_tmp_table","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steventen%2Fwhere_in_vs_tmp_table/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steventen%2Fwhere_in_vs_tmp_table/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steventen%2Fwhere_in_vs_tmp_table/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/steventen","download_url":"https://codeload.github.com/steventen/where_in_vs_tmp_table/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steventen%2Fwhere_in_vs_tmp_table/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267639569,"owners_count":24119780,"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","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-12-16T20:31:03.295Z","updated_at":"2025-07-29T06:32:44.511Z","avatar_url":"https://github.com/steventen.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Compare WHERE IN with TEMP TABLE\nThis benchmark aims to compare PostgreSQL queries among `WHERE IN (...)`, `WHERE ANY (...)` and `TEMP TABLE` in Ruby.\n\nBlog post: http://stevenyue.com/blogs/build-sql-queries-with-temporary-table-vs-where-in/\n\n## Test Case\nGiven a `requests` table with schema like this:\n\n```sql\nCREATE TABLE \"requests\" (\n  \"id\" serial primary key,\n  \"account_id\" bigint NOT NULL,\n  \"request_date\" date NOT NULL,\n  \"total\" bigint DEFAULT 0 NOT NULL\n);\n```\n\nWrite a sql query that returns the sum of `total`, group by `account_id` for a given list of `account_ids`.\n\n## Approach 1: Use 'WHERE IN'\nThis approach uses `WHERE IN (...)` clause, where the `(...)` is a list of `account_ids`:\n```sql\nSELECT account_id, SUM(total) AS total_requests\nFROM requests r\nWHERE r.account_id IN (1,2,3,4, ...)\nGROUP BY r.account_id\n```\n\n## Approach 2: Use 'WHERE ANY'\nThis approach uses `WHERE ANY ( VALUES (), ... )` clause, where the ` VALUES (), () ...` contains values from `account_ids`:\n```sql\nSELECT account_id, SUM(total) AS total_requests\nFROM requests r\nWHERE r.account_id = ANY ( VALUES (1), (2), (3), ... )\nGROUP BY r.account_id\n```\n\n## Approach 3: Use 'TEMP TABLE'\nThis approach creates a `TEMP TABLE` called `tmp_accounts`, which only includes those selected `account_ids`:\n```sql\nCREATE TEMP TABLE tmp_accounts (\n  \"account_id\" INT8,\n  CONSTRAINT \"id_pkey\" PRIMARY KEY (\"account_id\")\n)\nON COMMIT DROP;\n```\nand then uses `INNER JOIN` to do the filtering and get the results:\n```sql\nSELECT r.account_id, SUM(total) AS total_requests\nFROM requests r\nINNER JOIN tmp_accounts ON tmp_accounts.account_id = r.account_id\nGROUP BY r.account_id\n```\nNote that the temp table will get dropped after the transaction.\n\n## Run Benchmark\n1. Rename `config.yml.example` to `config.yml`, and update the database configuration in the file accordingly.\n\n2. Run `bundle install`\n\n3. Modify default settings in `benchmark.rb`\n\n4. Run `bundle exec ruby benchmark.rb`.\n\nThe code will create the database, create the `requests` table, seed random data, and then run the benchmark, and after that, it will drop the database.\n\nBy default, the code will generate `500,000` rows of requests for `5,000` account ids. Then it will randomly choose `50%` of the account ids for the queries, and run each query only `1` time. You can tune these settings inside [benchmark.rb](benchmark.rb).\n\n## Benchmark Results\nHardware specs: Macbook Pro 2.2GHz Intel Core i7, 16GB Memory, 250 GB SSD\n\nDatabase: PostgreSQL 9.4.5\n\nSeeds: 500,000 rows of requests, 5,000 account ids, and randomly choose 50% of the account ids.\n\nWhen the index on `account_id` column is enabled, by setting the variable `index_account_id` to `true` in `benchmark.rb`:\n```bash\n                    user     system      total        real\nuse_where_in    0.000000   0.000000   0.000000 (  0.242052)\nuse_where_any   0.010000   0.000000   0.010000 (  0.219573)\nuse_tmp_table   0.000000   0.000000   0.000000 (  0.193532)\n```\n\nWhen there is no index on `account_id`, the results are:\n```bash\n                    user     system      total        real\nuse_where_in    0.000000   0.000000   0.000000 (  6.290538)\nuse_where_any   0.000000   0.000000   0.000000 (  0.192422)\nuse_tmp_table   0.000000   0.000000   0.000000 (  0.193068)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteventen%2Fwhere_in_vs_tmp_table","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsteventen%2Fwhere_in_vs_tmp_table","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteventen%2Fwhere_in_vs_tmp_table/lists"}