· 8 years ago · Nov 21, 2017, 06:28 PM
1CREATE TABLE public.devices
2(
3 id integer NOT NULL DEFAULT nextval('devices_id_seq'::regclass),
4 token text NOT NULL,
5 ...some other columns...,
6 CONSTRAINT devices_pkey PRIMARY KEY (id, token)
7)
8
9CREATE INDEX IF NOT EXISTS idx_devices_lower_token ON devices (lower(token));
10
11SELECT * FROM "devices" WHERE (id > '0') AND ((lower(token) IN ('92134f4bb6a2263fd044a5a04440bc1a','cbf7d4a094bd266fd3f1b7ef4196172a', 'aa0f7650cf28010b5c01b77ae206f7fb'))) ORDER BY id LIMIT 1000;
12
13"Limit (cost=58116.96..58119.46 rows=1000 width=345)"
14" -> Sort (cost=58116.96..58189.47 rows=29005 width=345)"
15" Sort Key: id"
16" -> Bitmap Heap Scan on devices (cost=1061.91..56526.65 rows=29005 width=345)"
17" Recheck Cond: (lower(token) = ANY ('{92134f4bb6a2263fd044a5a04440bc1a,cbf7d4a094bd266fd3f1b7ef4196172a}'::text[]))"
18" Filter: (id > 0)"
19" -> Bitmap Index Scan on idx_devices_lower_token (cost=0.00..1054.65 rows=29005 width=0)"
20" Index Cond: (lower(token) = ANY ('{92134f4bb6a2263fd044a5a04440bc1a,cbf7d4a094bd266fd3f1b7ef4196172a}'::text[]))"
21
22SELECT * FROM "devices" WHERE (id > '0') AND ((lower(token) IN ('92134f4bb6a2263fd044a5a04440bc1a','cbf7d4a094bd266fd3f1b7ef4196172a', 'aa0f7650cf28010b5c01b77ae206f7fb', ... up to 1k tokens ...))) ORDER BY id LIMIT 1000;
23
24"Limit (cost=3900016.07..3900018.57 rows=1000 width=345)"
25" -> Sort (cost=3900016.07..3907219.10 rows=2881214 width=345)"
26" Sort Key: id"
27" -> Seq Scan on devices (cost=0.00..3742042.21 rows=2881214 width=345)"
28" Filter: ((id > 0) AND (lower(token) = ANY ('{92134f4bb6a2263fd044a5a04440bc1a,cbf7d4a094bd266fd3f1b7ef4196172a,6116af468e0017ffe5c49309869be773,4a5ebc1cec6c452e74fbf1a4e22fe804,0a9981eaf530d22ad9fe12a73a0b486c,61eb008e86a5afeaa9edd3e56ba01e0b,a31775a866571d9c2ecadc6b2a8127a3,dfcb4bc34771883bb2a54ee44299b058,7c93581a24053021bd41b7cc4654bf9e,43182b0010cea255fef1d10849858d7c,c3f6503eafeaa1d25a6f5672310ef4e5,672641b1fcd23f67deab34deaf28f1d8,cb52e085618ecbdf6eef1669b7b38008,335626a32455e61e1b1de16550e43af0, (...)"
29
30create index IF NOT EXISTS idx_devices_token on devices (token);
31
32SELECT * FROM "devices" WHERE (id > '0') AND ((token IN ('92134f4bb6a2263fd044a5a04440bc1a','cbf7d4a094bd266fd3f1b7ef4196172a', 'aa0f7650cf28010b5c01b77ae206f7fb', ... 1k tokens ...))) ORDER BY id LIMIT 1000;
33
34"Limit (cost=25.70..25.71 rows=3 width=345)"
35" -> Sort (cost=25.70..25.71 rows=3 width=345)"
36" Sort Key: id"
37" -> Bitmap Heap Scan on devices (cost=13.69..25.68 rows=3 width=345)"
38" Recheck Cond: (token = ANY ('{92134f4bb6a2263fd044a5a04440bc1a,cbf7d4a094bd266fd3f1b7ef4196172a,aa0f7650cf28010b5c01b77ae206f7fb}'::text[]))"
39" Filter: (id > 0)"
40" -> Bitmap Index Scan on idx_devices_token (cost=0.00..13.69 rows=3 width=0)"
41" Index Cond: (token = ANY ('{92134f4bb6a2263fd044a5a04440bc1a,cbf7d4a094bd266fd3f1b7ef4196172a,aa0f7650cf28010b5c01b77ae206f7fb}'::text[]))"