Sql postgres中星型模式的唯一结果提取与成本降低
我有下面的星型模式,其中包含一系列表,如下所示,用于确定图书馆中图书的可用性 f_book_availability(此表包含参考其他维度表的书籍可用性,我将在稍后解释)索引主键Sql postgres中星型模式的唯一结果提取与成本降低,sql,postgresql,query-performance,star-schema,table-index,Sql,Postgresql,Query Performance,Star Schema,Table Index,我有下面的星型模式,其中包含一系列表,如下所示,用于确定图书馆中图书的可用性 f_book_availability(此表包含参考其他维度表的书籍可用性,我将在稍后解释)索引主键 +-------------------------------------------------------------------------------+ | id | book_id |publisherid | location_id | genre_id | date_id| a
+-------------------------------------------------------------------------------+
| id | book_id |publisherid | location_id | genre_id | date_id| available |
| | | | | | | |
+-------------------------------------------------------------------------------+
| 1 | 1 | 1 | 72 | 1 | 1 | 1 |
| | | | | | | |
+-------------------------------------------------------------------------------+
| 2 | 2 | 1 | 60 | 2 | 1 | 1 |
| | | | | | | |
+-------------------------------------------------------------------------------+
+-----------+-----------+------------+
| id | type | name |
+------------------------------------+
| 1 | 1 | LOR |
+------------------------------------+
| 2 | 2 | My life |
+-----------+-----------+------------+
+-----------+------------
| id | name |
+-----------------------+
| 1 | abc |
+-----------------------+
| 2 | def |
+-----------+------------
+-----------+------------
| id | name |
+-----------------------+
| 1 | fantasy |
+-----------------------+
| 2 | horror |
+-----------+------------
d_book-此维度表具有类似书本的名称和类型的详细信息。类型仅具有id 1和2。1表示“由出版商出版”,2表示“自行出版”,没有任何参考表。索引主键
+-------------------------------------------------------------------------------+
| id | book_id |publisherid | location_id | genre_id | date_id| available |
| | | | | | | |
+-------------------------------------------------------------------------------+
| 1 | 1 | 1 | 72 | 1 | 1 | 1 |
| | | | | | | |
+-------------------------------------------------------------------------------+
| 2 | 2 | 1 | 60 | 2 | 1 | 1 |
| | | | | | | |
+-------------------------------------------------------------------------------+
+-----------+-----------+------------+
| id | type | name |
+------------------------------------+
| 1 | 1 | LOR |
+------------------------------------+
| 2 | 2 | My life |
+-----------+-----------+------------+
+-----------+------------
| id | name |
+-----------------------+
| 1 | abc |
+-----------------------+
| 2 | def |
+-----------+------------
+-----------+------------
| id | name |
+-----------------------+
| 1 | fantasy |
+-----------------------+
| 2 | horror |
+-----------+------------
d_publisher:此维度表包含发布者信息。索引主键
+-------------------------------------------------------------------------------+
| id | book_id |publisherid | location_id | genre_id | date_id| available |
| | | | | | | |
+-------------------------------------------------------------------------------+
| 1 | 1 | 1 | 72 | 1 | 1 | 1 |
| | | | | | | |
+-------------------------------------------------------------------------------+
| 2 | 2 | 1 | 60 | 2 | 1 | 1 |
| | | | | | | |
+-------------------------------------------------------------------------------+
+-----------+-----------+------------+
| id | type | name |
+------------------------------------+
| 1 | 1 | LOR |
+------------------------------------+
| 2 | 2 | My life |
+-----------+-----------+------------+
+-----------+------------
| id | name |
+-----------------------+
| 1 | abc |
+-----------------------+
| 2 | def |
+-----------+------------
+-----------+------------
| id | name |
+-----------------------+
| 1 | fantasy |
+-----------------------+
| 2 | horror |
+-----------+------------
d_位置-这个维度很棘手。(注意,架构已经存在,我无法修改它)。它具有位置id和父位置id。注意:对于id,我们保存层次结构,例如,如果您选择id 72,它是一个叶节点,并告诉库的机架,您可以看到每个叶节点有四个具有不同父节点的条目,您可以从中了解层次结构国家->城市->库->机架。这对每个地方都适用。例如,如果您有图书馆的位置,那么您可以找到层次结构国家->城市->图书馆。索引1)主键(id和parentId),2)parentId
d_流派:此维度表具有流派信息索引主键
+-------------------------------------------------------------------------------+
| id | book_id |publisherid | location_id | genre_id | date_id| available |
| | | | | | | |
+-------------------------------------------------------------------------------+
| 1 | 1 | 1 | 72 | 1 | 1 | 1 |
| | | | | | | |
+-------------------------------------------------------------------------------+
| 2 | 2 | 1 | 60 | 2 | 1 | 1 |
| | | | | | | |
+-------------------------------------------------------------------------------+
+-----------+-----------+------------+
| id | type | name |
+------------------------------------+
| 1 | 1 | LOR |
+------------------------------------+
| 2 | 2 | My life |
+-----------+-----------+------------+
+-----------+------------
| id | name |
+-----------------------+
| 1 | abc |
+-----------------------+
| 2 | def |
+-----------+------------
+-----------+------------
| id | name |
+-----------------------+
| 1 | fantasy |
+-----------------------+
| 2 | horror |
+-----------+------------
d_date:这个维度表有所有的日期(注意,我没有显示其他列,如月、日、年、周、周数,但我并不是为了简单起见而显示它,只是为了让您知道它不仅仅是日期,因为它看起来很愚蠢:))索引-1)主键2)日期
从这个表中,我试图准确地了解一本书是否在某一特定日期出版,以及它的出版商、流派、日期、位置和它的直接父位置等信息
我编写了以下查询
select
fba.id,
location.c_code as country,
parentLocation.name as parentPlace,
location.id as locationId,
location.name as locationName,
publisher.id as "publisherId",
publisher.name as publisherName,
case when book.type = 1 then 'published' else 'self-published' end as "bookType",
book.type as typeId,
genre.name as genreName,
book.id as "bookId",
book.name as bookTitle,
d."date",
fba.available
from f_book_availability fba
join d_book book on fba.product_id = product.id
join d_publisher publisher on fba.publisherid = publisher.id
join d_location location on fba.location_id = location.id
join d_location parentLocation on location.parent_id = parentLocation.id
join d_genre genre on fba.genre_id = genre.id
join d_date d on fba.date_id = d.id
where
location.id <> location.parent_id
and d."date" >= now() and d."date" <= '2020-12-01'
and location.c_code ='FR'
and book.type = 1
and genre.name = 'fantasy'
预期输出:正如您在上面看到的,由于与父位置和条件的联接,存在重复的输出
Unique(成本=12070116.66..12072185.22行=63648宽度=179)
->排序(成本=12070116.66..12070275.78行=63648宽度=179)
排序键:fba.id,parentLocation.name,location.id,location.name,publisher.id,publisher.name,(大小写为(book.type=1)然后“发布”::text-ELSE“self-published”::text-END),genre.name,book.id,book.name,d.date,fba.available
->散列联接(成本=3316.39..12059378.75行=63648宽度=179)
哈希条件:(location.parent\u id=parentLocation.id)
->散列联接(成本=2601.08..12057653.07行=19090宽度=141)
哈希条件:(fa.publisher\u id=publisher.id)
->散列联接(成本=2475.28..12057477.06行=19090宽度=120)
散列条件:(fba.date\u id=d.id)
->聚集(成本=2466.05..12051967.29行=2092656宽度=124)
计划人数:2人
->散列联接(成本=1466.05..11841701.69行=871940宽度=124)
散列条件:(fba.location\u id=location.id)
->散列联接(成本=820.92..11816871.09行=1374762宽度=99)
散列条件:(fa.book\u id=book.id)
->散列联接(成本=8.30..11808952.45行=2706393宽度=54)
散列条件:(fba.genre\u id=genre.id)
->f_book_availability fba上的并行顺序扫描(成本=0.00..11047094.57行=278758458宽度=45)
->散列(成本=8.29..8.29行=1宽度=25)
->d_类型的序列扫描(成本=0.00..8.29行=1宽度=25)
过滤器:((科技英语)::text='fantasy'::text)
->散列(成本=702.26..702.26行=8829宽度=53)
->顺序扫描d_书(成本=0.00..702.26行=8829宽度=53)
过滤器:(类型=1)
->散列(成本=613.88..613.88行=2500宽度=33)
->d_位置上的顺序扫描(成本=0.00..613.88行=2500宽=33)
筛选器:((id父项id)和((c代码)::text='FR'::text))
->散列(成本=8.86..8.86行=29宽度=8)
->在d_日期d使用日期_unique进行索引扫描(成本=0.28..8.86行=29宽度=8)
索引条件:((日期>=now())和(日期散列(成本=98.69..98.69行=2169宽度=29)
->d_publisher上的顺序扫描(成本=0.00..98.69行=2169宽度=29)
->散列(成本=546.25..546.25行=13525宽度=22)
->d_位置上的顺序扫描父位置(成本=0.00..546.25行=13525宽度=22)
很抱歉,如果文章不在这里,如果有输入错误,因为我必须工作两个小时来创建ascii表,并且可能有输入错误,因为我更改了列名,只是为了给出示例作为答案添加,以便可以勾选,请:) 根本没有什么基础