SQL(Postgres)最佳联接数

SQL(Postgres)最佳联接数,sql,postgresql,Sql,Postgresql,此查询正在postgres版本12中执行。此查询包含8个连接,持续约5秒 查询1 select * from "public"."products" "P" inner join "system"."categories" "C" on "C"."id" = "P"."id_category"

此查询正在postgres版本12中执行。此查询包含8个连接,持续约5秒

查询1

select *
from "public"."products" "P"
    inner join "system"."categories" "C" on "C"."id" = "P"."id_category"
    inner join "public"."businesses" "E" on "E"."id" = "P"."id_business"
    left join "public"."product_files" "pf" on "pf"."id_product" = "P"."id"
    left join "system"."files" "f" on "f"."name" = "pf"."img_code"
    left join "public"."product_variations" "pv" on ("pv"."id_product" = "P"."id" and "pv"."status" <> 'Deleted')
    left join "public"."product_stocks" "ps" on ("ps"."id_product_variation" = "pv"."id" and "ps"."status" <> 'Deleted')
    left join "public"."product_stocks" "pps" on ("pps"."id_product" = "P"."id" and "pps"."status" <> 'Deleted')
    inner join search_products( array['tires'], 8, 1, 'es') "search" on search.id = "P"."id"
where "P"."status" <> 'Deleted'
Merge Join  (cost=112948.60..121805.61 rows=4996 width=1145) (actual time=2003.599..2426.892 rows=40 loops=1)
  Merge Cond: ("P".id = search.id)
  Buffers: shared hit=760531, temp read=16912 written=18837
  ->  Merge Left Join  (cost=112888.52..120950.73 rows=287945 width=1105) (actual time=1607.013..2093.722 rows=380961 loops=1)
        Merge Cond: ("P".id = pf.id_product)
        Buffers: shared hit=752079, temp read=15561 written=15606
        ->  Merge Left Join  (cost=16288.22..19167.29 rows=57631 width=771) (actual time=165.803..271.662 rows=76193 loops=1)
              Merge Cond: ("P".id = pps.id_product)
              Buffers: shared hit=3820, temp read=2706 written=2733
              ->  Merge Left Join  (cost=16287.81..16577.01 rows=57631 width=686) (actual time=165.787..217.878 rows=56921 loops=1)
                    Merge Cond: ("P".id = pv.id_product)
                    Buffers: shared hit=2058, temp read=2706 written=2733
                    ->  Sort  (cost=14888.93..15033.01 rows=57631 width=514) (actual time=156.825..175.154 rows=56920 loops=1)
                          Sort Key: "P".id
                          Sort Method: external merge  Disk: 21840kB
                          Buffers: shared hit=1430, temp read=2706 written=2733
                          ->  Hash Join  (cost=43.49..2484.49 rows=57631 width=514) (actual time=0.266..64.052 rows=57631 loops=1)
                                Hash Cond: ("P".id_business = "E".id)
                                Buffers: shared hit=1430
                                ->  Hash Join  (cost=37.81..2322.14 rows=57631 width=374) (actual time=0.214..39.402 rows=57631 loops=1)
                                      Hash Cond: ("P".id_category = "C".id)
                                      Buffers: shared hit=1427
                                      ->  Seq Scan on products "P"  (cost=0.00..2132.41 rows=57631 width=252) (actual time=0.009..12.754 rows=57631 loops=1)
                                            Filter: ((status)::text <> 'Deleted'::text)
                                            Rows Removed by Filter: 2
                                            Buffers: shared hit=1412
                                      ->  Hash  (cost=25.14..25.14 rows=1014 width=122) (actual time=0.201..0.201 rows=1014 loops=1)
                                            Buckets: 1024  Batches: 1  Memory Usage: 124kB
                                            Buffers: shared hit=15
                                            ->  Seq Scan on categories "C"  (cost=0.00..25.14 rows=1014 width=122) (actual time=0.007..0.078 rows=1014 loops=1)
                                                  Buffers: shared hit=15
                                ->  Hash  (cost=4.19..4.19 rows=119 width=140) (actual time=0.047..0.048 rows=119 loops=1)
                                      Buckets: 1024  Batches: 1  Memory Usage: 29kB
                                      Buffers: shared hit=3
                                      ->  Seq Scan on businesses "E"  (cost=0.00..4.19 rows=119 width=140) (actual time=0.013..0.024 rows=119 loops=1)
                                            Buffers: shared hit=3
                    ->  Sort  (cost=1398.88..1399.05 rows=70 width=172) (actual time=8.956..8.958 rows=3 loops=1)
                          Sort Key: pv.id_product
                          Sort Method: quicksort  Memory: 43kB
                          Buffers: shared hit=628
                          ->  Hash Right Join  (cost=4.58..1396.73 rows=70 width=172) (actual time=8.853..8.912 rows=70 loops=1)
                                Hash Cond: (ps.id_product_variation = pv.id)
                                Buffers: shared hit=628
                                ->  Seq Scan on product_stocks ps  (cost=0.00..1259.35 rows=50589 width=85) (actual time=0.009..7.030 rows=50595 loops=1)
                                      Filter: ((status)::text <> 'Deleted'::text)
                                      Rows Removed by Filter: 73
                                      Buffers: shared hit=626
                                ->  Hash  (cost=3.70..3.70 rows=70 width=87) (actual time=0.048..0.049 rows=70 loops=1)
                                      Buckets: 1024  Batches: 1  Memory Usage: 16kB
                                      Buffers: shared hit=2
                                      ->  Seq Scan on product_variations pv  (cost=0.00..3.70 rows=70 width=87) (actual time=0.020..0.039 rows=70 loops=1)
                                            Filter: ((status)::text <> 'Deleted'::text)
                                            Rows Removed by Filter: 66
                                            Buffers: shared hit=2
              ->  Index Scan using product_stocks_id_product_id_product_variation_id_location_key on product_stocks pps  (cost=0.41..1819.97 rows=50589 width=85) (actual time=0.013..17.822 rows=49924 loops=1)
                    Filter: ((status)::text <> 'Deleted'::text)
                    Rows Removed by Filter: 1
                    Buffers: shared hit=1762
        ->  Materialize  (cost=96600.25..98040.03 rows=287955 width=334) (actual time=1441.203..1613.160 rows=380961 loops=1)
              Buffers: shared hit=748259, temp read=12855 written=12873
              ->  Sort  (cost=96600.25..97320.14 rows=287955 width=334) (actual time=1441.198..1567.183 rows=284596 loops=1)
                    Sort Key: pf.id_product
                    Sort Method: external merge  Disk: 102840kB
                    Buffers: shared hit=748259, temp read=12855 written=12873
                    ->  Merge Left Join  (cost=0.84..44546.48 rows=287955 width=334) (actual time=0.021..1013.742 rows=287955 loops=1)
                          Merge Cond: ((pf.img_code)::text = (f.name)::text)
                          Buffers: shared hit=748259
                          ->  Index Scan using product_files_pkey on product_files pf  (cost=0.42..10516.05 rows=287955 width=66) (actual time=0.005..184.173 rows=287955 loops=1)
                                Buffers: shared hit=289884
                          ->  Index Scan using files_pkey on files f  (cost=0.42..29304.42 rows=455180 width=268) (actual time=0.005..338.206 rows=455178 loops=1)
                                Buffers: shared hit=458375
  ->  Sort  (cost=60.08..62.58 rows=1000 width=40) (actual time=313.554..313.558 rows=36 loops=1)
        Sort Key: search.id
        Sort Method: quicksort  Memory: 25kB
        Buffers: shared hit=8452, temp read=1351 written=3231
        ->  Function Scan on search_products search  (cost=0.25..10.25 rows=1000 width=40) (actual time=313.544..313.545 rows=8 loops=1)
              Buffers: shared hit=8452, temp read=1351 written=3231
Planning Time: 2.632 ms
Execution Time: 2440.414 ms
选择*
来自“public”。“products”“P”
内部联接“系统”。“C”上的“类别”“C”。“id”=“P”。“id\U类别”
内部连接“公共”。“企业”在“E”上的“E”。“id”=“P”。“id\u企业”
左键连接“public”。“产品”文件“pf”在“pf”上。“id\u产品”=“P”。“id”
左键连接“系统”“文件”“f”“f”“f”“名称”“pf”“img_代码”
左键连接“公共”。“产品”变体“pv”在(“pv”。“id产品”=“P”。“id”和“pv”。“状态”“已删除”)
左键连接“公共”。“产品库存”“ps”在(“ps”。“id\U产品变化”=“pv”。“id”和“ps”。“状态”“已删除”)
左键加入“公共”。“产品”储存在(“pps”。“id产品”=“P”。“id”和“pps”。“状态”“已删除”)
内部连接搜索_产品(数组['tires'],8,1,'es')在search.id=“P”“id”上的“search”
其中“P”。“状态”为“已删除”
Postgres查询1的查询解释(分析、缓冲区)

select *
from "public"."products" "P"
    inner join "system"."categories" "C" on "C"."id" = "P"."id_category"
    inner join "public"."businesses" "E" on "E"."id" = "P"."id_business"
    left join "public"."product_files" "pf" on "pf"."id_product" = "P"."id"
    left join "system"."files" "f" on "f"."name" = "pf"."img_code"
    left join "public"."product_variations" "pv" on ("pv"."id_product" = "P"."id" and "pv"."status" <> 'Deleted')
    left join "public"."product_stocks" "ps" on ("ps"."id_product_variation" = "pv"."id" and "ps"."status" <> 'Deleted')
    left join "public"."product_stocks" "pps" on ("pps"."id_product" = "P"."id" and "pps"."status" <> 'Deleted')
    inner join search_products( array['tires'], 8, 1, 'es') "search" on search.id = "P"."id"
where "P"."status" <> 'Deleted'
Merge Join  (cost=112948.60..121805.61 rows=4996 width=1145) (actual time=2003.599..2426.892 rows=40 loops=1)
  Merge Cond: ("P".id = search.id)
  Buffers: shared hit=760531, temp read=16912 written=18837
  ->  Merge Left Join  (cost=112888.52..120950.73 rows=287945 width=1105) (actual time=1607.013..2093.722 rows=380961 loops=1)
        Merge Cond: ("P".id = pf.id_product)
        Buffers: shared hit=752079, temp read=15561 written=15606
        ->  Merge Left Join  (cost=16288.22..19167.29 rows=57631 width=771) (actual time=165.803..271.662 rows=76193 loops=1)
              Merge Cond: ("P".id = pps.id_product)
              Buffers: shared hit=3820, temp read=2706 written=2733
              ->  Merge Left Join  (cost=16287.81..16577.01 rows=57631 width=686) (actual time=165.787..217.878 rows=56921 loops=1)
                    Merge Cond: ("P".id = pv.id_product)
                    Buffers: shared hit=2058, temp read=2706 written=2733
                    ->  Sort  (cost=14888.93..15033.01 rows=57631 width=514) (actual time=156.825..175.154 rows=56920 loops=1)
                          Sort Key: "P".id
                          Sort Method: external merge  Disk: 21840kB
                          Buffers: shared hit=1430, temp read=2706 written=2733
                          ->  Hash Join  (cost=43.49..2484.49 rows=57631 width=514) (actual time=0.266..64.052 rows=57631 loops=1)
                                Hash Cond: ("P".id_business = "E".id)
                                Buffers: shared hit=1430
                                ->  Hash Join  (cost=37.81..2322.14 rows=57631 width=374) (actual time=0.214..39.402 rows=57631 loops=1)
                                      Hash Cond: ("P".id_category = "C".id)
                                      Buffers: shared hit=1427
                                      ->  Seq Scan on products "P"  (cost=0.00..2132.41 rows=57631 width=252) (actual time=0.009..12.754 rows=57631 loops=1)
                                            Filter: ((status)::text <> 'Deleted'::text)
                                            Rows Removed by Filter: 2
                                            Buffers: shared hit=1412
                                      ->  Hash  (cost=25.14..25.14 rows=1014 width=122) (actual time=0.201..0.201 rows=1014 loops=1)
                                            Buckets: 1024  Batches: 1  Memory Usage: 124kB
                                            Buffers: shared hit=15
                                            ->  Seq Scan on categories "C"  (cost=0.00..25.14 rows=1014 width=122) (actual time=0.007..0.078 rows=1014 loops=1)
                                                  Buffers: shared hit=15
                                ->  Hash  (cost=4.19..4.19 rows=119 width=140) (actual time=0.047..0.048 rows=119 loops=1)
                                      Buckets: 1024  Batches: 1  Memory Usage: 29kB
                                      Buffers: shared hit=3
                                      ->  Seq Scan on businesses "E"  (cost=0.00..4.19 rows=119 width=140) (actual time=0.013..0.024 rows=119 loops=1)
                                            Buffers: shared hit=3
                    ->  Sort  (cost=1398.88..1399.05 rows=70 width=172) (actual time=8.956..8.958 rows=3 loops=1)
                          Sort Key: pv.id_product
                          Sort Method: quicksort  Memory: 43kB
                          Buffers: shared hit=628
                          ->  Hash Right Join  (cost=4.58..1396.73 rows=70 width=172) (actual time=8.853..8.912 rows=70 loops=1)
                                Hash Cond: (ps.id_product_variation = pv.id)
                                Buffers: shared hit=628
                                ->  Seq Scan on product_stocks ps  (cost=0.00..1259.35 rows=50589 width=85) (actual time=0.009..7.030 rows=50595 loops=1)
                                      Filter: ((status)::text <> 'Deleted'::text)
                                      Rows Removed by Filter: 73
                                      Buffers: shared hit=626
                                ->  Hash  (cost=3.70..3.70 rows=70 width=87) (actual time=0.048..0.049 rows=70 loops=1)
                                      Buckets: 1024  Batches: 1  Memory Usage: 16kB
                                      Buffers: shared hit=2
                                      ->  Seq Scan on product_variations pv  (cost=0.00..3.70 rows=70 width=87) (actual time=0.020..0.039 rows=70 loops=1)
                                            Filter: ((status)::text <> 'Deleted'::text)
                                            Rows Removed by Filter: 66
                                            Buffers: shared hit=2
              ->  Index Scan using product_stocks_id_product_id_product_variation_id_location_key on product_stocks pps  (cost=0.41..1819.97 rows=50589 width=85) (actual time=0.013..17.822 rows=49924 loops=1)
                    Filter: ((status)::text <> 'Deleted'::text)
                    Rows Removed by Filter: 1
                    Buffers: shared hit=1762
        ->  Materialize  (cost=96600.25..98040.03 rows=287955 width=334) (actual time=1441.203..1613.160 rows=380961 loops=1)
              Buffers: shared hit=748259, temp read=12855 written=12873
              ->  Sort  (cost=96600.25..97320.14 rows=287955 width=334) (actual time=1441.198..1567.183 rows=284596 loops=1)
                    Sort Key: pf.id_product
                    Sort Method: external merge  Disk: 102840kB
                    Buffers: shared hit=748259, temp read=12855 written=12873
                    ->  Merge Left Join  (cost=0.84..44546.48 rows=287955 width=334) (actual time=0.021..1013.742 rows=287955 loops=1)
                          Merge Cond: ((pf.img_code)::text = (f.name)::text)
                          Buffers: shared hit=748259
                          ->  Index Scan using product_files_pkey on product_files pf  (cost=0.42..10516.05 rows=287955 width=66) (actual time=0.005..184.173 rows=287955 loops=1)
                                Buffers: shared hit=289884
                          ->  Index Scan using files_pkey on files f  (cost=0.42..29304.42 rows=455180 width=268) (actual time=0.005..338.206 rows=455178 loops=1)
                                Buffers: shared hit=458375
  ->  Sort  (cost=60.08..62.58 rows=1000 width=40) (actual time=313.554..313.558 rows=36 loops=1)
        Sort Key: search.id
        Sort Method: quicksort  Memory: 25kB
        Buffers: shared hit=8452, temp read=1351 written=3231
        ->  Function Scan on search_products search  (cost=0.25..10.25 rows=1000 width=40) (actual time=313.544..313.545 rows=8 loops=1)
              Buffers: shared hit=8452, temp read=1351 written=3231
Planning Time: 2.632 ms
Execution Time: 2440.414 ms
Merge Join(成本=112948.60..121805.61行=4996宽度=1145)(实际时间=2003.599..2426.892行=40个循环=1)
合并条件:(“P.id=search.id)
缓冲区:共享命中=760531,临时读取=16912写入=18837
->合并左连接(成本=112888.52..120950.73行=287945宽度=1105)(实际时间=1607.013..2093.722行=380961循环=1)
合并条件:(“P.id=pf.id\u产品)
缓冲区:共享命中=752079,临时读取=15561写入=15606
->合并左连接(成本=16288.22..19167.29行=57631宽度=771)(实际时间=165.803..271.662行=76193循环=1)
合并条件:(“P.id=pps.id\u产品)
缓冲区:共享命中=3820,临时读取=2706写入=2733
->合并左连接(成本=16287.81..16577.01行=57631宽度=686)(实际时间=165.787..217.878行=56921循环=1)
合并条件:(“P.id=pv.id\u产品)
缓冲区:共享命中=2058,临时读取=2706写入=2733
->排序(成本=14888.93..15033.01行=57631宽度=514)(实际时间=156.825..175.154行=56920循环=1)
排序键:“P”.id
排序方法:外部合并磁盘:21840kB
缓冲区:共享命中=1430,临时读取=2706写入=2733
->散列联接(成本=43.49..2484.49行=57631宽度=514)(实际时间=0.266..64.052行=57631循环=1)
散列条件:(“P.id\u business=“E.id”)
缓冲区:共享命中=1430
->散列联接(成本=37.81..2322.14行=57631宽度=374)(实际时间=0.214..39.402行=57631循环=1)
散列条件:(“P.id\u category=“C.id”)
缓冲区:共享命中=1427
->产品“P”上的顺序扫描(成本=0.00..2132.41行=57631宽度=252)(实际时间=0.009..12.754行=57631循环=1)
筛选器:((状态)::文本“已删除”::文本)
被筛选器删除的行:2
缓冲区:共享命中=1412
->散列(成本=25.14..25.14行=1014宽度=122)(实际时间=0.201..0.201行=1014循环=1)
存储桶:1024批:1内存使用量:124kB
缓冲区:共享命中=15
->对类别“C”的顺序扫描(成本=0.00..25.14行=1014宽度=122)(实际时间=0.007..0.078行=1014圈=1)
缓冲区:共享命中=15
->散列(成本=4.19..4.19行=119宽度=140)(实际时间=0.047..0.048行=119循环=1)
存储桶:1024批:1内存使用量:29kB
缓冲区:共享命中=3
->业务“E”上的顺序扫描(成本=0.00..4.19行=119宽度=140)(实际时间=0.013..0.024行=119圈=1)
缓冲区:共享命中=3
->排序(成本=1398.88..1399.05行=70宽度=172)(实际时间=8.956..8.958行=3个循环=1)
排序键:pv.id\U产品
排序方法:快速排序内存:43kB
缓冲区:共享命中=628
->散列右连接(成本=4.58..1396.73行=70宽度=172)(实际时间=8.853..8.912行=70循环=1)
散列条件:(ps.id\u产品变量=pv.id)
缓冲区:共享命中=628
->产品上的序列扫描(成本=0.00..1259.35行=50589宽度=85)(实际时间=0.009..7.030行=50595循环=1)
筛选器:((状态)::文本“已删除”::文本)
被筛选器删除的行:73
缓冲区:共享命中=626
->散列(成本=3.70..3.70行=70宽度=87)(实际时间=0.048..0.049行=70循环=1)
存储桶:1024批:1内存使用量:16kB
缓冲区:共享命中=2
->产品变更pv上的顺序扫描(成本=0.00..3.70行=70宽度=87)(实际时间=0.020..0.039行=70圈=1)
筛选器:((状态)::文本“已删除”::文本)
被筛选器删除的行:66
缓冲区:共享命中=2
->使用产品库存pps上的产品库存id产品id产品变异id位置键进行索引扫描(成本=0.41..1819.97行=50589宽度=85)(实际时间=0.013)。