Hello Team,
I have a 12.11 postgresql database on Linux. When I ran the below query, the query planner uses the gist index. However, the query runs faster if I drop the gist index, because the query planner uses the b-tree index. Why does the query planner use the gist index instead of the b-tree?
SELECT DISTINCT w."InstanceId",
w."LastStepId" ,
sl."SlaPercent",
sl."RemainingSla" ,
sl."DefaultSla" ,
sl."PriorityScore",
sl."KpiDuration" ,
sl."E2EDurationHour" ,
sl."E2EDurationDay" ,
sl."E2EDurationHour" ,
sl."E2EDurationDay" ,
sl."TeamDuration" AS "GroupTime",
poolsource."PartyId",
sl."SLASTATUS"
FROM dsi."Pool_Datasource" poolsource
JOIN dsi."Instance" w ON poolsource."InstanceId" = w."InstanceId"
AND NOT (EXISTS ( SELECT 1 FROM dsi."Pool_Party" WHERE "Pool_Party"."Code" = 'TEAM-FIXPOWERUSER'
AND poolsource."RelatedPartyId" = "Pool_Party"."PartyId"))
AND w."ApplicationCd" = 'SolIncident'
LEFT JOIN dsi."Records" sl ON sl."InstanceId" = w."InstanceId"
LEFT JOIN dsi."Tickets" tc ON tc."WFINSTANCEID" = w."InstanceId"
WHERE w."LastStepId" = poolsource."StepId" AND w."EntityStatusCd" <> 'CLOSE' AND
NOT (EXISTS (SELECT 1 FROM dsi."Tickets" tc_1
WHERE tc_1."OLDPLATFORMCODE" = 'Davinci' AND tc_1."TICKETTYPEID" = 1
AND w."InstanceId" = tc_1."WFINSTANCEID"
AND (tc_1."STATUS" = ANY (ARRAY['SOLUTION', 'REJECT']))));
w."LastStepId" ,
sl."SlaPercent",
sl."RemainingSla" ,
sl."DefaultSla" ,
sl."PriorityScore",
sl."KpiDuration" ,
sl."E2EDurationHour" ,
sl."E2EDurationDay" ,
sl."E2EDurationHour" ,
sl."E2EDurationDay" ,
sl."TeamDuration" AS "GroupTime",
poolsource."PartyId",
sl."SLASTATUS"
FROM dsi."Pool_Datasource" poolsource
JOIN dsi."Instance" w ON poolsource."InstanceId" = w."InstanceId"
AND NOT (EXISTS ( SELECT 1 FROM dsi."Pool_Party" WHERE "Pool_Party"."Code" = 'TEAM-FIXPOWERUSER'
AND poolsource."RelatedPartyId" = "Pool_Party"."PartyId"))
AND w."ApplicationCd" = 'SolIncident'
LEFT JOIN dsi."Records" sl ON sl."InstanceId" = w."InstanceId"
LEFT JOIN dsi."Tickets" tc ON tc."WFINSTANCEID" = w."InstanceId"
WHERE w."LastStepId" = poolsource."StepId" AND w."EntityStatusCd" <> 'CLOSE' AND
NOT (EXISTS (SELECT 1 FROM dsi."Tickets" tc_1
WHERE tc_1."OLDPLATFORMCODE" = 'Davinci' AND tc_1."TICKETTYPEID" = 1
AND w."InstanceId" = tc_1."WFINSTANCEID"
AND (tc_1."STATUS" = ANY (ARRAY['SOLUTION', 'REJECT']))));
With GIST Index: (Total exec time : 34s)
Index Scan using "Pool_Party_Code_gist" on "Pool_Party" (cost=0.28..8.30 rows=1 width=4) (actual time=0.097..0.097 rows=1 loops=330870)
Index Cond: (("Code")::text = 'TEAM-FIXPOWERUSER'::text)
Index Cond: (("Code")::text = 'TEAM-FIXPOWERUSER'::text)
Without GIST Index: (Total exec time: 4s)
Index Scan using "Pool_Party_Code_idx" on "Pool_Party" (cost=0.42..8.44 rows=1 width=4) (actual time=0.007..0.007 rows=1 loops=330870)
Index Cond: (("Code")::text = 'TEAM-FIXPOWERUSER'::text)
Index Cond: (("Code")::text = 'TEAM-FIXPOWERUSER'::text)