I've got the following query:
SELECT
* FROM ( SELECT split_part(full_path, '/', 4)::INT AS account_id,
split_part(full_path, '/', 6)::INT AS note_id,
split_part(full_path, '/', 9)::TEXT AS variation,
st_size,
segment_index,
reverse(split_part(reverse(full_path), '/', 1)) as file_name,
s.segment_data,
i.st_ino,
full_path,
(i.st_size / 1000000::FLOAT)::NUMERIC(5,2) || 'MB' AS size_mb
FROM gorfs.inodes i
JOIN gorfs.inode_segments s
ON i.st_ino = s.st_ino_target
WHERE i.checksum_md5 IS NOT NULL
AND s.full_path ~ '/userfiles/account/[0-9]+/[a-z]+/[0-9]+'
AND i.st_size > 0) as test WHERE account_id = 12115;
Explain analyze for the query above: http://explain.depesz.com/s/rHOU
It seems the query is not using the Index I've created: ix_full_path
Table inodes_segments:
CREATE TABLE gorfs.inode_segments
(
st_ino "gorfs"."ino_t" NOT NULL,
segment_index "gorfs"."pathname_component" NOT NULL,
st_ino_target "gorfs"."ino_t",
full_path "gorfs"."absolute_pathname",
segment_data "bytea",
CONSTRAINT pk_inode_segments PRIMARY KEY ("st_ino", "segment_index"),
CONSTRAINT fk_host_inode_must_exist FOREIGN KEY (st_ino)
REFERENCES gorfs.inodes (st_ino) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_target_inode_must_exist FOREIGN KEY (st_ino_target)
REFERENCES gorfs.inodes (st_ino) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT uc_no_duplicate_full_paths UNIQUE ("full_path"),
CONSTRAINT cc_only_root_can_be_its_own_parent CHECK ("st_ino_target" IS NULL OR "st_ino"::bigint <> "st_ino_target"::bigint OR "st_ino"::bigint = 2)
)
Table inodes:
CREATE TABLE gorfs.inodes
(
st_dev "gorfs"."dev_t" DEFAULT NULL::bigint,
st_ino "gorfs"."ino_t" NOT NULL DEFAULT "nextval"('"gorfs"."inodes_st_ino_idseq"'::"regclass"),
st_mode "gorfs"."mode_t" NOT NULL,
st_nlink "gorfs"."nlink_t" NOT NULL,
st_uid "gorfs"."uid_t" NOT NULL,
st_gid "gorfs"."gid_t" NOT NULL,
st_rdev "gorfs"."dev_t",
st_size "gorfs"."off_t",
st_blksize "gorfs"."blksize_t",
st_blocks "gorfs"."blkcnt_t",
st_atime "gorfs"."time_t" NOT NULL,
st_mtime "gorfs"."time_t" NOT NULL,
st_ctime "gorfs"."time_t" NOT NULL,
checksum_md5 "md5_hash",
media_subtype_id integer,
external_size "gorfs"."off_t",
CONSTRAINT pk_inodes PRIMARY KEY ("st_ino"),
CONSTRAINT fk_media_subtype_must_exist FOREIGN KEY (media_subtype_id)
REFERENCES public.media_subtypes (media_subtype_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT cc_mount_devices_not_supported CHECK ("st_dev" IS NULL)
)
I've already created the following indexes:
CREATE INDEX CONCURRENTLY ix_full_path ON gorfs.inode_segments USING btree (("full_path")::"text");
CREATE INDEX CONCURRENTLY ix_checksum_st_size ON gorfs.inodes USING btree ("checksum_md5","st_size") WHERE checksum_md5 IS NOT NULL;
- But it didn't work.
Question:
What can I do to improve the Query?