小编Flo*_*aie的帖子

连接表的索引

在Google上搜索联接表索引时,我遇到了这个问题.

现在,我相信它在接受的答案中提供了一些虚假信息,或者我不明白一切是如何运作的.给出以下表格(在PostGreSQL 9.4上运行):

CREATE TABLE "albums" ("album_id" serial PRIMARY KEY, "album_name" text)
CREATE TABLE "artists" ("artist_id" serial PRIMARY KEY, "artist_name" text)
CREATE TABLE "albums_artists" ("album_id" integer REFERENCES "albums", "artist_id" integer REFERENCES "artists")
Run Code Online (Sandbox Code Playgroud)

我试图从上面提到的问题复制场景,首先在albums_artists表的两列上创建一个索引,然后为每列创建一个索引(不保留两列上的索引).

当使用EXPLAIN命令进行普通的传统选择时,我会期待非常不同的结果,如下所示:

SELECT "artists".* FROM "test"."artists"
    INNER JOIN "test"."albums_artists" ON ("albums_artists"."artist_id" = "artists"."artist_id")
    WHERE ("albums_artists"."album_id" = 1)
Run Code Online (Sandbox Code Playgroud)

但是,当实际运行解释时,我得到的结果与每种情况完全相同(每列上有一个索引,两列上有一个索引).

我一直在阅读文档PostgreSQL的关于索引,并没有作出,我得到的结果任何意义:

Hash Join  (cost=15.05..42.07 rows=11 width=36) (actual time=0.024..0.025 rows=1 loops=1)
  Hash Cond: (artists.artist_id = albums_artists.artist_id)
  ->  Seq Scan on artists  (cost=0.00..22.30 rows=1230 width=36) (actual …
Run Code Online (Sandbox Code Playgroud)

postgresql database-design database-indexes

5
推荐指数
1
解决办法
7487
查看次数