We have a table of
id|school_id|parent_ids
where parent_ids
is an array of ids.
If we don't have the school_id
and only parent_id
to search for, then the query will search through all the table rows in parent_ids
array, there might be thousands of rows, and parent_id
might actually be within just few of them.
Does using IN in query for the array column could be a performance barrier in this case?
EDIT
Here is the dump of table structure:
-- ----------------------------
-- Table structure for schools_messages
-- ----------------------------
DROP TABLE IF EXISTS "public"."schools_messages";
CREATE TABLE "public"."schools_messages" (
"id" int4 NOT NULL DEFAULT nextval('schools_messages_id_seq'::regclass),
"message" jsonb NOT NULL DEFAULT '[]'::jsonb,
"details" jsonb NOT NULL DEFAULT '[]'::jsonb,
"school_id" int4 NOT NULL,
"created_at" timestamp(0),
"updated_at" timestamp(0),
"parents_ids" int4[] DEFAULT ARRAY[]::integer[]
)
;
ALTER TABLE "public"."schools_messages" OWNER TO "prod_schools";
-- ----------------------------
-- Primary Key structure for table schools_messages
-- ----------------------------
ALTER TABLE "public"."schools_messages" ADD CONSTRAINT "schools_messages_pkey" PRIMARY KEY ("id");
-- ----------------------------
-- Foreign Keys structure for table schools_messages
-- ----------------------------
ALTER TABLE "public"."schools_messages" ADD CONSTRAINT "schools_messages_school_id_foreign" FOREIGN KEY ("school_id") REFERENCES "public"."trk_schools" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
create table
statement for the table in question (including all indexes), the query you are using and the execution plan generated usingexplain (analyze, buffers)
. Formatted text please, no screen shots. Edit your question. Do not post code in comments