English 中文(简体)
2. 自我就业优化
原标题:Self-join query optimizations

I have a table, dev_base_low which stores that have several fields but for this example we will focus on the key fields, from, to, carrier, type, v_type, goods_category. The table contains 4,000 records and are indexed on the key fields.

目标很简单,在<代码> 类型栏下。 我愿加入<条码>——中枢——发言。 我的第一项尝试是同所有发言者一道,与中枢一道:

WITH 
    temp_hub as 
    (SELECT * FROM dev_base_low WHERE carrier_type =  hub )

SELECT
    1
FROM 
  (select * from temp_hub where type =  spoke-hub ) leg1 
    INNER JOIN (select * from temp_hub where type =  hub-hub ) leg2 
        on  
                        leg1.to = leg2.from and
                        leg1.carrier = leg2.carrier and
                        leg1.from <> leg2.to and
                        leg1.v_type = leg2.v_type and
                        leg1.goods_category = leg2.goods_category 

我们可以想象的最好使用散草的做法,是第二,产出8 000个记录。 EXPLAIN ANALYZEproduct, to my knowledge from below, indexes should not have a fundamental differences given that it is ah participation and also because the total records are in no ways large:

QUERY PLAN
Hash Join  (cost=224.21..308.71 rows=1 width=4) (actual time=6.369..15.949 rows=8982 loops=1)
  Hash Cond: ((temp_hub.to = temp_hub_1.from) AND (temp_hub.carrier = temp_hub_1.carrier) AND (temp_hub.v_type = temp_hub_1.v_type) AND (temp_hub.goods_category = temp_hub_1.goods_category))
  Join Filter: (temp_hub.from <> temp_hub_1.to)
  Rows Removed by Join Filter: 4
  CTE temp_hub
    ->  Seq Scan on dev_base_low  (cost=0.00..139.72 rows=3738 width=155) (actual time=0.020..2.094 rows=3738 loops=1)
          Filter: (carrier_type =  hub ::text)
  ->  CTE Scan on temp_hub  (cost=0.00..84.11 rows=19 width=160) (actual time=0.027..1.514 rows=1240 loops=1)
        Filter: (type =  spoke-hub ::text)
        Rows Removed by Filter: 2498
  ->  Hash  (cost=84.11..84.11 rows=19 width=160) (actual time=6.313..6.314 rows=1088 loops=1)
        Buckets: 2048 (originally 1024)  Batches: 1 (originally 1)  Memory Usage: 107kB
        ->  CTE Scan on temp_hub temp_hub_1  (cost=0.00..84.11 rows=19 width=160) (actual time=0.011..5.106 rows=1088 loops=1)
              Filter: (type =  hub-hub ::text)
              Rows Removed by Filter: 2650
Planning Time: 1.871 ms
Execution Time: 16.928 ms

www.un.org/Depts/DGACM/index_spanish.htm 问题在于,增加了第二名自雇人员,不幸的是,我在20秒里很难在我的客户旁听。 如下所示:

SELECT
    1
FROM 
  (select * from temp_hub where type =  spoke-hub ) leg1 
    INNER JOIN (select * from temp_hub where type =  hub-hub ) leg2 
        on  
                        leg1.to = leg2.from and
                        leg1.carrier = leg2.carrier and
                        leg1.from <> leg2.to and
                        leg1.v_type = leg2.v_type and
                        leg1.goods_category = leg2.goods_category 
  INNER JOIN (select * from temp_hub where type =  hub-spoke  ) leg3
        on  leg2.to = leg3.from and
                        leg2.carrier = leg3.carrier and
                        leg1.from <> leg3.to and
                        leg2.from <> leg3.to and
                        leg2.v_type = leg3.v_type and
                        leg2.goods_category = leg3.goods_category

我曾尝试过几次优化,使用分局和计算中心,使用不同的合并方法(现金、开具、合并)和检查非银记忆分配会议,但没有实际好处。 我估计第2台内车的总产出不到400 000份。

www.un.org/Depts/DGACM/index_french.htm

  1. Is there anything wrong with the query joins or methods?
  2. Are there any optimizations on troubleshooting query performance that I can run?
  3. Even given 2 tables, with 8,000 and 4,000 records respectively, is there anything I can do to ensure that the runtime remains below 20 seconds?

http://www.ohchr.org。

因此,即使在时间缩短了2分钟之后,我也知道可以选择增加时间,但我猜测,这不符合目的。 在以下查询中添加<编码>EXPLAIN:

Nested Loop  (cost=224.16..393.19 rows=1 width=4)
"  Join Filter: ((temp_hub.""from"" <> temp_hub_1.""to"") AND (temp_hub_1.""from"" <> temp_hub_2.""to"") AND (temp_hub.""to"" = temp_hub_1.""from"") AND (temp_hub.carrier = temp_hub_1.carrier) AND (temp_hub.v_type = temp_hub_1.v_type) AND (temp_hub.goods_category = temp_hub_1.goods_category) AND (temp_hub_2.""from"" = temp_hub_1.""to""))"
  CTE temp_hub
    ->  Seq Scan on dev_base_low  (cost=0.00..139.72 rows=3738 width=155)
          Filter: (carrier_type =  hub ::text)
  ->  Hash Join  (cost=84.44..168.84 rows=1 width=320)
        Hash Cond: ((temp_hub.carrier = temp_hub_2.carrier) AND (temp_hub.v_type = temp_hub_2.v_type) AND (temp_hub.goods_category = temp_hub_2.goods_category))
"        Join Filter: (temp_hub.""from"" <> temp_hub_2.""to"")"
        ->  CTE Scan on temp_hub  (cost=0.00..84.11 rows=19 width=160)
              Filter: (type =  spoke-hub ::text)
        ->  Hash  (cost=84.11..84.11 rows=19 width=160)
              ->  CTE Scan on temp_hub temp_hub_2  (cost=0.00..84.11 rows=19 width=160)
                    Filter: (type =  hub-spoke ::text)
  ->  CTE Scan on temp_hub temp_hub_1  (cost=0.00..84.11 rows=19 width=160)
        Filter: (type =  hub-hub ::text)
问题回答

经过小的改动后,现在的问答时间是第二次。

Nested Loop  (cost=0.58..1804.05 rows=43262 width=4) (actual time=0.235..926.969 rows=310362 loops=1)
"  Join Filter: ((dev_base.""from"" <> dev_base_2.""to"") AND (dev_base.carrier = dev_base_2.carrier) AND (dev_base.v_type = dev_base_2.v_type) AND (dev_base.goods_category = dev_base_2.goods_category))"
  Rows Removed by Join Filter: 2564
  ->  Nested Loop  (cost=0.29..941.32 rows=7986 width=93) (actual time=0.194..493.947 rows=23818 loops=1)
        ->  Seq Scan on dev_base  (cost=0.00..297.30 rows=2031 width=53) (actual time=0.128..4.099 rows=1778 loops=1)
              Filter: ((carrier_type =  hub ::text) AND (type =  spoke-hub ::text))
              Rows Removed by Filter: 5842
        ->  Memoize  (cost=0.29..0.82 rows=1 width=53) (actual time=0.039..0.269 rows=13 loops=1778)
"              Cache Key: dev_base.""from"", dev_base.""to"", dev_base.carrier, dev_base.v_type, dev_base.goods_category"
              Hits: 0  Misses: 1778  Evictions: 0  Overflows: 0  Memory Usage: 2164kB
              ->  Index Scan using idx_base_routes on dev_base dev_base_1  (cost=0.28..0.81 rows=1 width=53) (actual time=0.035..0.253 rows=13 loops=1778)
"                    Index Cond: ((""from"" = dev_base.""to"") AND (carrier = dev_base.carrier) AND (v_type = dev_base.v_type) AND (goods_category = dev_base.goods_category))"
"                    Filter: ((carrier_type =  hub ::text) AND (type =  hub-hub ::text) AND (dev_base.""from"" <> ""to""))"
                    Rows Removed by Filter: 36
  ->  Memoize  (cost=0.29..0.88 rows=1 width=53) (actual time=0.004..0.009 rows=13 loops=23818)
"        Cache Key: dev_base_1.""from"", dev_base_1.carrier, dev_base_1.v_type, dev_base_1.goods_category, dev_base_1.""to"""
        Hits: 22644  Misses: 1174  Evictions: 0  Overflows: 0  Memory Usage: 1070kB
        ->  Index Scan using idx_base_routes on dev_base dev_base_2  (cost=0.28..0.87 rows=1 width=53) (actual time=0.049..0.127 rows=10 loops=1174)
"              Index Cond: ((""from"" = dev_base_1.""to"") AND (carrier = dev_base_1.carrier) AND (v_type = dev_base_1.v_type) AND (goods_category = dev_base_1.goods_category))"
"              Filter: ((carrier_type =  hub ::text) AND (type =  hub-spoke ::text) AND (dev_base_1.""from"" <> ""to""))"
              Rows Removed by Filter: 11
Planning Time: 3.428 ms
Execution Time: 943.901 ms

简言之,索引在加入书和邮袋中具有重要意义,使用CTE和。 可在说明中实现,且其索引没有好处。 就象我国这样的案件而言,如果其写成文,其效率就会大大提高;

WITH
    temp_hub AS NOT MATERIALIZED
    (SELECT * FROM dev_base WHERE carrier_type =  hub )

http://www.postgresql.org https://www.postgresql.org/docs/ 当前/queries-with.html。 请注意<代码>WITH说明不含任何计算费用的业务。

@Charlieface的评论指出了这一点。





相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签