更改值后,查询需要60倍的时间

时间:2017-03-10 12:28:57

标签: performance postgresql sql-execution-plan execution-time postgresql-performance

我尝试运行2个查询,区别仅在于我将状态从“CA”更改为“FL”的where子句。并且执行时间增加了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)

第二次查询:

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=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))

第二个查询的解释:

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[]))

0 个答案:

没有答案