86314,0,LOG,00000,"duration: 819.900 ms execute <unnamed>: selectcount(recv_id) from skytf.my_invite_rec where recv_id=$1andresult=2","parameters: $1 = '125662840'",,,,,,,,""
2 sql 语句
1
select count(recv_id) from skytf.my_invite_rec where recv_id=$1andresult=2
3 表结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
skytf=> /d my_invite_rec Table "skytf.my_invite_rec" Column | Type | Modifiers -------------+-----------------------------+-------------------- invite_id | numeric(12,0) | notnull invite_time | timestamp without time zone | user_id | numeric(10,0) | notnull recv_id | numeric(10,0) | notnull invite_msg | character varying(512) | appinfo | character varying(32) | extinfo | character varying(128) | group_id | numeric(12,0) | result | numeric(10,0) | notnull default 2 Indexes: "pk_my_invite_rec" PRIMARY KEY, btree (invite_id) "idx_my_invite_rec_recvid" btree (recv_id) "idx_my_invite_rec_userid" btree (user_id)
4 老的执行计划
1 2 3 4 5 6 7 8 9 10 11 12 13 14
skytf=> explain analyze select count(recv_id) from skytf.my_invite_rec where recv_id=125662840 andresult=2; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ --------------------- Aggregate (cost=42015.40..42015.41 rows=1 width=10) (actual time=739.196..739.196 rows=1 loops=1) -> Bitmap Heap Scan on my_invite_rec (cost=5039.49..41753.20 rows=104879 width=10) (actual time=739.190..739.190 rows=0 loops=1) Recheck Cond: (recv_id = 125662840::numeric) Filter: (result = 2::numeric) -> Bitmap Index Scan on idx_my_invite_rec_recvid (cost=0.00..5013.27 rows=329714 width=0) (actual time=213.024..213.024 rows=330385 loops=1) Index Cond: (recv_id = 125662840::numeric) Total runtime: 739.243 ms (7 rows) Time: 739.800 ms
skytf=> select recv_id from my_invite_rec where result=2 limit 10; recv_id ----------- 103103900 106460840 153989140 153099420 335932 132509748 110848636 101857296 107371328 135262800 (10 rows) Time: 0.556 ms
skytf=> select count(*) from my_invite_rec where recv_id=103103900; count ------- 5 (1 row) Time: 0.681 ms
skytf=> select count(*) from my_invite_rec where recv_id=107371328; count ------- 28 (1 row) Time: 0.668 ms
skytf=> explain analyze select count(*) from my_invite_rec where recv_id=103103900; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ -------------- Aggregate (cost=95.78..95.79 rows=1 width=0) (actual time=0.058..0.058 rows=1 loops=1) -> Index Scan using idx_my_invite_rec_recvid on my_invite_rec (cost=0.00..95.66 rows=48 width=0) (actual time=0.044..0.055 rows=5 loops=1) Index Cond: (recv_id = 103103900::numeric) Total runtime: 0.093 ms (4 rows) Time: 0.773 ms
从上面可以看出,SQL都在 1 ms 以下,非常的迅速啊,猜想可能与记录数有关。
7 猜想先前的两个recv_id的记录数比较多,查询如下
1 2 3 4 5 6 7 8 9 10 11 12 13
skytf=> select count(*) from my_invite_rec where recv_id=125662840; count -------- 332424 (1 row) Time: 804.972 ms
skytf=> select count(*) from my_invite_rec where recv_id=148161000; count -------- 416366 (1 row) Time: 850.757 ms
果然如此,是因为这两个 recv_id 对应的数据比较多,PG花了很长的时间寻找符合条件记录。
8 后来在德哥的指导下,创建联合索引
1 2
skytf=> _create index idx_my_invite_rec_result on my_invite_rec (result,recv_id) where recv_id in (125662840,148161000);_CREATE INDEX Time: 4949.886 ms