I have a table with 14_000 rows. Not to many. My query
SELECT "wells".* FROM "wells" WHERE (LOWER(name) LIKE '%abc%' OR code LIKE '%ABC%')
ORDER BY "wells"."name_nso" ASC, "wells"."extra_name" ASC
LIMIT 10;
takes "Execution Time: 2.701 ms"
For this table i have two indexes:
CREATE INDEX wells_btree_idx_on_name_nso
ON public.wells USING btree
(name_nso COLLATE pg_catalog."default" ASC NULLS LAST)
TABLESPACE pg_default;
and
CREATE INDEX wells_gin_idx_on_name_lower
ON public.wells USING gin
(lower(name) COLLATE pg_catalog."default" gin_trgm_ops)
TABLESPACE pg_default;
If i remove LIMIT 10, it takes "Execution Time: 0.894 ms". 4 times faster.
Is it worth looking into how to speed up a query with LIMIT 10 to those 0.894 ms, or are those 2.701 ms fast enough and not worth bothering with?
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
CREATE INDEX wells_idx_name_nso_extra_name ON "wells" ("name_nso","extra_name");
SELECT
"wells".*
FROM
"wells"
WHERE
(
LOWER("wells".name) LIKE '%abc%'
OR "wells".code LIKE '%ABC%'
)
ORDER BY
"wells"."name_nso" ASC,
"wells"."extra_name" ASC LIMIT 10