【发布时间】:2019-12-08 01:50:30
【问题描述】:
我有以下带有解释的查询:
SELECT "carts".* FROM "carts" WHERE ("carts"."content_updated_at" IS NOT NULL) ORDER BY carts.content_updated_at desc LIMIT 50 OFFSET 0
QUERY PLAN
------------------------------------------------------------------------------
Limit (cost=208231.51..208231.63 rows=50 width=1267)
-> Sort (cost=208231.51..208558.04 rows=130615 width=1267)
Sort Key: content_updated_at DESC
-> Seq Scan on carts (cost=0.00..203892.57 rows=130615 width=1267)
Filter: (content_updated_at IS NOT NULL)
如果我在content_updated_at IS NOT NULL 上添加索引,是否会提高此查询的性能?
这里是没有ORDER BY 子句的解释:
EXPLAIN for: SELECT "carts".* FROM "carts" WHERE ("carts"."content_updated_at" IS NOT NULL) LIMIT 50 OFFSET 0
QUERY PLAN
------------------------------------------------------------------------
Limit (cost=0.00..75.03 rows=50 width=1270)
-> Seq Scan on carts (cost=0.00..204483.29 rows=136264 width=1270)
Filter: (content_updated_at IS NOT NULL)
EXPLAIN ANALYZE:
EXPLAIN ANALYZE SELECT "carts".* FROM "carts" WHERE ("carts"."content_updated_at" IS NOT NULL) ORDER BY carts.content_updated_at desc LIMIT 50 OFFSET 0;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------
Limit (cost=209373.22..209373.34 rows=50 width=1270) (actual time=18482.469..18482.620 rows=50 loops=1)
-> Sort (cost=209373.22..209717.30 rows=137633 width=1270) (actual time=18482.463..18482.517 rows=50 loops=1)
Sort Key: content_updated_at DESC
Sort Method: top-N heapsort Memory: 50kB
-> Seq Scan on carts (cost=0.00..204801.15 rows=137633 width=1270) (actual time=0.553..18283.431 rows=139318 loops=1)
Filter: (content_updated_at IS NOT NULL)
Rows Removed by Filter: 3023640
【问题讨论】:
标签: database postgresql indexing