Performance 值更改后,查询所用的时间是原来的60倍

Performance 值更改后,查询所用的时间是原来的60倍,performance,postgresql,sql-execution-plan,execution-time,postgresql-performance,Performance,Postgresql,Sql Execution Plan,Execution Time,Postgresql Performance,我尝试运行两个查询,不同之处仅在于where子句,其中我将状态从“CA”更改为“FL”。执行时间增加了60倍,你们能告诉我一个正确的方法吗,为什么两个符号会改变查询时间这么多 第一个问题: SELECT (COUNT(DISTINCT "Location"."doctor_id")) AS "Location__td", (COUNT(DISTINCT "Location"."id")) AS "Location__tl" FROM "public"."locations" AS

我尝试运行两个查询,不同之处仅在于where子句,其中我将状态从“CA”更改为“FL”。执行时间增加了60倍,你们能告诉我一个正确的方法吗,为什么两个符号会改变查询时间这么多

第一个问题:

SELECT (COUNT(DISTINCT "Location"."doctor_id")) AS "Location__td",
       (COUNT(DISTINCT "Location"."id")) AS "Location__tl"
FROM "public"."locations" AS "Location"
JOIN
  (SELECT location_id, (array_agg("DoctorP"."fullname"))[1] fullname, (array_agg("DoctorP"."license"))[1] license, (array_agg("DoctorP"."gender"))[1] gender
   FROM services
   JOIN
     (SELECT id,
             fullname,
             license,
             gender
      FROM "public"."doctors" AS "DoctorP"
      WHERE "DoctorP"."id" NOT IN
          (SELECT doctor_id
           FROM folder_doctors
           WHERE folder_id IN (26,
                               25,
                               24))) AS "DoctorP" ON "DoctorP"."id" = doctor_id
   WHERE plan_desc = 'Commercial'
   GROUP BY location_id) AS "Service" ON ("Location"."id" = "Service"."location_id")
WHERE coordinates IS NOT NULL
  AND ST_Contains(ST_MakeEnvelope(-57.121123689709975, 50.33132725216629, -128.31252993970998, 18.208317875535407), "Location"."coordinates")
  AND "Location"."state" = 'CA'
LIMIT 1

22478

1 строка (0.420 s)
Limit  (cost=36592.78..36592.79 rows=1 width=16)
  ->  Aggregate  (cost=36592.78..36592.79 rows=1 width=16)
    ->  Hash Join  (cost=36303.68..36592.76 rows=4 width=16)
          Hash Cond: (services.location_id = "Location".id)
          ->  HashAggregate  (cost=36059.78..36181.48 rows=12170 width=8)
                Group Key: services.location_id
                ->  Hash Join  (cost=1353.90..35573.63 rows=194461 width=8)
                      Hash Cond: (services.doctor_id = "DoctorP".id)
                      ->  Seq Scan on services  (cost=0.00..30816.61 rows=388935 width=16)
                            Filter: (plan_desc = 'Commercial'::text)
                      ->  Hash  (cost=1154.22..1154.22 rows=15974 width=8)
                            ->  Seq Scan on doctors "DoctorP"  (cost=20.86..1154.22 rows=15974 width=8)
                                  Filter: (NOT (hashed SubPlan 1))
                                  SubPlan 1
                                    ->  Index Scan using folder_doctors_folder_id_index on folder_doctors  (cost=0.28..20.78 rows=34 width=8)
                                          Index Cond: (folder_id = ANY ('{26,25,24}'::integer[]))
          ->  Hash  (cost=243.66..243.66 rows=19 width=16)
                ->  Bitmap Heap Scan on locations "Location"  (cost=4.88..243.66 rows=19 width=16)
                      Recheck Cond: ((coordinates IS NOT NULL) AND ('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry ~ coordinates))
                      Filter: ((state = 'CA'::bpchar) AND _st_contains('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry, coordinates))
                      ->  Bitmap Index Scan on locations_coordinates_gix  (cost=0.00..4.88 rows=60 width=0)
                            Index Cond: ((coordinates IS NOT NULL) AND ('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry ~ coordinates))
第二个问题:

SELECT (COUNT(DISTINCT "Location"."doctor_id")) AS "Location__td",
       (COUNT(DISTINCT "Location"."id")) AS "Location__tl"
FROM "public"."locations" AS "Location"
JOIN
  (SELECT location_id, (array_agg("DoctorP"."fullname"))[1] fullname, (array_agg("DoctorP"."license"))[1] license, (array_agg("DoctorP"."gender"))[1] gender
   FROM services
   JOIN
     (SELECT id,
             fullname,
             license,
             gender
      FROM "public"."doctors" AS "DoctorP"
      WHERE "DoctorP"."id" NOT IN
          (SELECT doctor_id
           FROM folder_doctors
           WHERE folder_id IN (26,
                               25,
                               24))) AS "DoctorP" ON "DoctorP"."id" = doctor_id
   WHERE plan_desc = 'Commercial'
   GROUP BY location_id) AS "Service" ON ("Location"."id" = "Service"."location_id")
WHERE coordinates IS NOT NULL
  AND ST_Contains(ST_MakeEnvelope(-57.121123689709975, 50.33132725216629, -128.31252993970998, 18.208317875535407), "Location"."coordinates")
  AND "Location"."state" = 'FL'
LIMIT 1

399

1 строка (27.734 s)
Limit  (cost=36550.09..36550.10 rows=1 width=16)
->  Aggregate  (cost=36550.09..36550.10 rows=1 width=16)
    ->  Nested Loop  (cost=36137.80..36550.09 rows=1 width=16)
          Join Filter: ("Location".id = services.location_id)
          ->  Bitmap Heap Scan on locations "Location"  (cost=78.02..94.78 rows=1 width=16)
                Recheck Cond: ((coordinates IS NOT NULL) AND ('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry ~ coordinates) AND (state = 'FL'::bpchar))
                Filter: _st_contains('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry, coordinates)
                ->  BitmapAnd  (cost=78.02..78.02 rows=4 width=0)
                      ->  Bitmap Index Scan on locations_coordinates_gix  (cost=0.00..4.88 rows=60 width=0)
                            Index Cond: ((coordinates IS NOT NULL) AND ('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry ~ coordinates))
                      ->  Bitmap Index Scan on locations_state  (cost=0.00..72.89 rows=3813 width=0)
                            Index Cond: (state = 'FL'::bpchar)
          ->  HashAggregate  (cost=36059.78..36181.48 rows=12170 width=8)
                Group Key: services.location_id
                ->  Hash Join  (cost=1353.90..35573.63 rows=194461 width=8)
                      Hash Cond: (services.doctor_id = "DoctorP".id)
                      ->  Seq Scan on services  (cost=0.00..30816.61 rows=388935 width=16)
                            Filter: (plan_desc = 'Commercial'::text)
                      ->  Hash  (cost=1154.22..1154.22 rows=15974 width=8)
                            ->  Seq Scan on doctors "DoctorP"  (cost=20.86..1154.22 rows=15974 width=8)
                                  Filter: (NOT (hashed SubPlan 1))
                                  SubPlan 1
                                    ->  Index Scan using folder_doctors_folder_id_index on folder_doctors  (cost=0.28..20.78 rows=34 width=8)
                                          Index Cond: (folder_id = ANY ('{26,25,24}'::integer[]))
第一个问题的解释:

SELECT (COUNT(DISTINCT "Location"."doctor_id")) AS "Location__td",
       (COUNT(DISTINCT "Location"."id")) AS "Location__tl"
FROM "public"."locations" AS "Location"
JOIN
  (SELECT location_id, (array_agg("DoctorP"."fullname"))[1] fullname, (array_agg("DoctorP"."license"))[1] license, (array_agg("DoctorP"."gender"))[1] gender
   FROM services
   JOIN
     (SELECT id,
             fullname,
             license,
             gender
      FROM "public"."doctors" AS "DoctorP"
      WHERE "DoctorP"."id" NOT IN
          (SELECT doctor_id
           FROM folder_doctors
           WHERE folder_id IN (26,
                               25,
                               24))) AS "DoctorP" ON "DoctorP"."id" = doctor_id
   WHERE plan_desc = 'Commercial'
   GROUP BY location_id) AS "Service" ON ("Location"."id" = "Service"."location_id")
WHERE coordinates IS NOT NULL
  AND ST_Contains(ST_MakeEnvelope(-57.121123689709975, 50.33132725216629, -128.31252993970998, 18.208317875535407), "Location"."coordinates")
  AND "Location"."state" = 'CA'
LIMIT 1

22478

1 строка (0.420 s)
Limit  (cost=36592.78..36592.79 rows=1 width=16)
  ->  Aggregate  (cost=36592.78..36592.79 rows=1 width=16)
    ->  Hash Join  (cost=36303.68..36592.76 rows=4 width=16)
          Hash Cond: (services.location_id = "Location".id)
          ->  HashAggregate  (cost=36059.78..36181.48 rows=12170 width=8)
                Group Key: services.location_id
                ->  Hash Join  (cost=1353.90..35573.63 rows=194461 width=8)
                      Hash Cond: (services.doctor_id = "DoctorP".id)
                      ->  Seq Scan on services  (cost=0.00..30816.61 rows=388935 width=16)
                            Filter: (plan_desc = 'Commercial'::text)
                      ->  Hash  (cost=1154.22..1154.22 rows=15974 width=8)
                            ->  Seq Scan on doctors "DoctorP"  (cost=20.86..1154.22 rows=15974 width=8)
                                  Filter: (NOT (hashed SubPlan 1))
                                  SubPlan 1
                                    ->  Index Scan using folder_doctors_folder_id_index on folder_doctors  (cost=0.28..20.78 rows=34 width=8)
                                          Index Cond: (folder_id = ANY ('{26,25,24}'::integer[]))
          ->  Hash  (cost=243.66..243.66 rows=19 width=16)
                ->  Bitmap Heap Scan on locations "Location"  (cost=4.88..243.66 rows=19 width=16)
                      Recheck Cond: ((coordinates IS NOT NULL) AND ('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry ~ coordinates))
                      Filter: ((state = 'CA'::bpchar) AND _st_contains('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry, coordinates))
                      ->  Bitmap Index Scan on locations_coordinates_gix  (cost=0.00..4.88 rows=60 width=0)
                            Index Cond: ((coordinates IS NOT NULL) AND ('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry ~ coordinates))
第二个问题的解释:

SELECT (COUNT(DISTINCT "Location"."doctor_id")) AS "Location__td",
       (COUNT(DISTINCT "Location"."id")) AS "Location__tl"
FROM "public"."locations" AS "Location"
JOIN
  (SELECT location_id, (array_agg("DoctorP"."fullname"))[1] fullname, (array_agg("DoctorP"."license"))[1] license, (array_agg("DoctorP"."gender"))[1] gender
   FROM services
   JOIN
     (SELECT id,
             fullname,
             license,
             gender
      FROM "public"."doctors" AS "DoctorP"
      WHERE "DoctorP"."id" NOT IN
          (SELECT doctor_id
           FROM folder_doctors
           WHERE folder_id IN (26,
                               25,
                               24))) AS "DoctorP" ON "DoctorP"."id" = doctor_id
   WHERE plan_desc = 'Commercial'
   GROUP BY location_id) AS "Service" ON ("Location"."id" = "Service"."location_id")
WHERE coordinates IS NOT NULL
  AND ST_Contains(ST_MakeEnvelope(-57.121123689709975, 50.33132725216629, -128.31252993970998, 18.208317875535407), "Location"."coordinates")
  AND "Location"."state" = 'FL'
LIMIT 1

399

1 строка (27.734 s)
Limit  (cost=36550.09..36550.10 rows=1 width=16)
->  Aggregate  (cost=36550.09..36550.10 rows=1 width=16)
    ->  Nested Loop  (cost=36137.80..36550.09 rows=1 width=16)
          Join Filter: ("Location".id = services.location_id)
          ->  Bitmap Heap Scan on locations "Location"  (cost=78.02..94.78 rows=1 width=16)
                Recheck Cond: ((coordinates IS NOT NULL) AND ('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry ~ coordinates) AND (state = 'FL'::bpchar))
                Filter: _st_contains('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry, coordinates)
                ->  BitmapAnd  (cost=78.02..78.02 rows=4 width=0)
                      ->  Bitmap Index Scan on locations_coordinates_gix  (cost=0.00..4.88 rows=60 width=0)
                            Index Cond: ((coordinates IS NOT NULL) AND ('01030000000100000005000000A00927FB808F4CC0F42970EE682A4940A00927FB808F4CC0C698FE515435324068C2C93E000A60C0C698FE515435324068C2C93E000A60C0F42970EE682A4940A00927FB808F4CC0F42970EE682A4940'::geometry ~ coordinates))
                      ->  Bitmap Index Scan on locations_state  (cost=0.00..72.89 rows=3813 width=0)
                            Index Cond: (state = 'FL'::bpchar)
          ->  HashAggregate  (cost=36059.78..36181.48 rows=12170 width=8)
                Group Key: services.location_id
                ->  Hash Join  (cost=1353.90..35573.63 rows=194461 width=8)
                      Hash Cond: (services.doctor_id = "DoctorP".id)
                      ->  Seq Scan on services  (cost=0.00..30816.61 rows=388935 width=16)
                            Filter: (plan_desc = 'Commercial'::text)
                      ->  Hash  (cost=1154.22..1154.22 rows=15974 width=8)
                            ->  Seq Scan on doctors "DoctorP"  (cost=20.86..1154.22 rows=15974 width=8)
                                  Filter: (NOT (hashed SubPlan 1))
                                  SubPlan 1
                                    ->  Index Scan using folder_doctors_folder_id_index on folder_doctors  (cost=0.28..20.78 rows=34 width=8)
                                          Index Cond: (folder_id = ANY ('{26,25,24}'::integer[]))

(array_agg(“DoctorP.”“fullname”)[1]fullname,
如果要从列表中选择第一项,这似乎是一种非常昂贵的方法。另外,用“<代码> >不存在(…)”/代码>替换“<代码> < /代码>。请考虑标签中的说明。<代码>(ArayaAgg(“Doc Trp””“Full NoX”)(1)全名,< /代码>如果要从列表中选择第一项,这看起来是一种非常昂贵的方式。另外,替换<代码>不在(…)<代码> > <代码>不存在(…)>代码>请考虑标签中的说明。