postgresql使用索引做查询条件 值不同 查询时间差距过大
payee_id 在pay_request表中已经创建了索引
payee_id索引属性
名 运算符类别模式 运算符类别 排序顺序 Nulls排序
payee_id pg_catalog int8_ops AES NULLS LAST
查询语句 如下
select * from pay_request where payee_id = 35858 order by ctime desc limit 20; // 时间: 2.473s
select count(1) from pay_request where payee_id = 35858;//count:6345
select * from pay_request where payee_id = 92351 order by ctime desc limit 20; // 时间: 0.224s
select count(1) from pay_request where payee_id = 92351;//count:1281573
explain payee_id 35858结果
Limit (cost=0.43..439.01 rows=20 width=981)
-> Index Scan Backward using pay_request_ctime_idx on pay_request (cost=0.43..118066.22 rows=5384 width=981)
Filter: (payee_id = 35858)
explain payee_id 92351结果
Limit (cost=0.43..2.26 rows=20 width=981)
-> Index Scan Backward using pay_request_ctime_idx on pay_request (cost=0.43..118066.22 rows=1286676 width=981)
Filter: (payee_id = 92351)