为什么这个查询不使用我创建的索引?

时间:2016-10-13 13:59:57

标签: sql database postgresql postgresql-9.5

运行postgres 9.5

SELECT events.id, events.start_time, events.host_id, events.title from events
JOIN accountsevents ON accountsevents.events_id = events.id
WHERE accountsevents.accounts_id = %(account_id)s OR events.host_id = %(account_id)s
GROUP BY events.id
ORDER BY start_time DESC

我有这个查询和postgres说成本超过100k。似乎过分了。这是我唯一没有利用我为每个表创建的索引的查询。

Indexes:
    "events_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "events_host_id_fkey" FOREIGN KEY (host_id) REFERENCES accounts(id)
Referenced by:
    TABLE "accountsevents" CONSTRAINT "accountsevents_events_id_fkey" FOREIGN KEY (events_id) REFERENCES events(id)
    TABLE "eventsinterests" CONSTRAINT "eventsinterests_events_id_fkey" FOREIGN KEY (events_id) REFERENCES events(id)


Indexes:
    "accountsevents_pkey" PRIMARY KEY, btree (id, accounts_id, events_id)
Foreign-key constraints:
    "accountsevents_accounts_id_fkey" FOREIGN KEY (accounts_id) REFERENCES accounts(id)
    "accountsevents_events_id_fkey" FOREIGN KEY (events_id) REFERENCES events(id)

我觉得索引设置错误或者我只是错过了查询的内容。最初的序列扫描正在扼杀它。

添加EXPLAIN VERBOSE

"Sort  (cost=124388.27..124390.10 rows=732 width=40) (actual time=1533.902..1533.928 rows=470 loops=1)"
"  Output: events.id, events.start_time, events.host_id, events.title"
"  Sort Key: events.start_time DESC"
"  Sort Method: quicksort  Memory: 66kB"
"  ->  HashAggregate  (cost=124346.12..124353.44 rows=732 width=40) (actual time=1533.658..1533.759 rows=470 loops=1)"
"        Output: events.id, events.start_time, events.host_id, events.title"
"        Group Key: events.id"
"        ->  Hash Join  (cost=4912.30..124344.29 rows=732 width=40) (actual time=56.671..1532.831 rows=971 loops=1)"
"              Output: events.id, events.start_time, events.host_id, events.title"
"              Hash Cond: (accountsevents.events_id = events.id)"
"              Join Filter: ((accountsevents.accounts_id = 1) OR (events.host_id = 1))"
"              Rows Removed by Join Filter: 2761882"
"              ->  Seq Scan on public.accountsevents  (cost=0.00..45228.52 rows=2762852 width=8) (actual time=0.005..466.094 rows=2762853 loops=1)"
"                    Output: accountsevents.events_id, accountsevents.accounts_id"
"              ->  Hash  (cost=2795.91..2795.91 rows=104191 width=40) (actual time=53.579..53.579 rows=104181 loops=1)"
"                    Output: events.id, events.start_time, events.host_id, events.title"
"                    Buckets: 65536  Batches: 4  Memory Usage: 2462kB"
"                    ->  Seq Scan on public.events  (cost=0.00..2795.91 rows=104191 width=40) (actual time=0.004..26.171 rows=104181 loops=1)"
"                          Output: events.id, events.start_time, events.host_id, events.title"
"Planning time: 0.201 ms"
"Execution time: 1534.024 ms"

1 个答案:

答案 0 :(得分:3)

没有索引可以帮助您解决此问题。

问题是您在OR条件下有WHERE,因此在表格加入之前无法应用过滤器,这就是索引可以帮助你。尝试用OR替换AND,看看PostgreSQL如何做得更好。

这样PostgreSQL必须计算整个连接,并且之后只能过滤掉行 - 请参阅大量的由加入过滤器删除的行

索引唯一可以使用的是嵌套循环连接,这会更加昂贵。所以我不认为这个查询有更好的计划。

你可以看到PostgreSQL对行数的估计非常好,这通常表明PostgreSQL确实做对了。

也许你可以通过像

这样的查询做得更好
SELECT * FROM
   (SELECT ... FROM events JOIN accountsevents ON ...
       WHERE accountsevents.accounts_id = 1
    UNION
    SELECT ... FROM events JOIN accountsevents ON ...
       WHERE events.host_id = 1) sub
GROUP BY ... ORDER BY ...

但我不会赌它 (注意:此查询在语义上略有不同,但在您的情况下可能并不重要。)