Check out the following simple query:
SELECT * FROM "teammgr_team" WHERE ("teammgr_team"."real" = true AND "teammgr_team"."name" = 'abc');
But it takes too long:
-------------------------------------------------------------------------------------------------------------------
Seq Scan on teammgr_team (cost=0.00..114772.49 rows=118 width=121) (actual time=24.544..618.185 rows=12 loops=1)
Filter: ("real" AND ((name)::text = 'abc'::text))
Rows Removed by Filter: 4752431
Planning time: 0.066 ms
Execution time: 618.217 ms
(5 rows)
I assume because the table is very large:
count
---------
4752443
(1 row)
This is the table and relevant column:
Table "public.teammgr_team"
Column | Type | Modifiers
----------------------+-----------------------+-----------------------------------------------------------
id | integer | not null default nextval('teammgr_team_id_seq'::regclass)
name | character varying(40) | not null
Indexes:
"teammgr_team_pkey" PRIMARY KEY, btree (id)
"teammgr_team_club_id" btree (club_id)
I am not sure if adding an index to a character column is advisable. I would think so but I don't know enough about databases.
So I am thinking about adding a simple index:
CREATE INDEX teammgr_team_name ON teammgr_team (name);
Just having in mind it shouldn't be UNIQUE
because the team name is not unique.
Would adding this index help improve execution time?
I've gone through the docs but is there any option that would be benefitial to my goal?