Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index on "user".id::text #473

Merged
merged 1 commit into from
Oct 4, 2022
Merged

Add index on "user".id::text #473

merged 1 commit into from
Oct 4, 2022

Conversation

amCap1712
Copy link
Member

@amCap1712 amCap1712 commented Oct 4, 2022

With #403, we perform a check as "user".id::text = :user_ref. :user_ref can be a username or a uuid, therefore we need to cast "user".id to text so that the comparison succeeds without error in case :user_ref is a username. Casting to text, however disables the use of any uuid indexes we may have on "user".id so add a new index on "user".id::text.

critiquebrainz_db=> begin;
BEGIN
critiquebrainz_db=> explain analyze select * from "user" where id::text = 'amCap1712';
                                            QUERY PLAN
---------------------------------------------------------------------------------------------------
 Seq Scan on "user"  (cost=0.00..77.18 rows=13 width=78) (actual time=2.317..2.317 rows=0 loops=1)
   Filter: ((id)::text = 'amCap1712'::text)
   Rows Removed by Filter: 2631
 Planning Time: 0.069 ms
 Execution Time: 2.336 ms
(5 rows)

critiquebrainz_db=> CREATE INDEX ix_user_id ON "user" USING btree ((id::text)); 
CREATE INDEX
critiquebrainz_db=> explain analyze select * from "user" where id::text = 'amCap1712';
                                                      QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
 Index Scan using ix_user_id on "user"  (cost=0.28..12.51 rows=13 width=78) (actual time=0.128..0.128 rows=0 loops=1)
   Index Cond: ((id)::text = 'amCap1712'::text)
 Planning Time: 0.328 ms
 Execution Time: 0.158 ms
(4 rows)

critiquebrainz_db=> rollback;
ROLLBACK

With #403, we perform a check as `"user".id::text = :user_ref`. `:user_ref`
can be a username or a uuid, therefore we need to cast "user".id to text
so that the comparison succeeds without error in case :user_ref is a username.
Casting to text, however disables the use of any uuid indexes we may have on
"user".id so add a new index on "user".id::text.

critiquebrainz_db=> begin;
BEGIN
critiquebrainz_db=> explain analyze select * from "user" where id::text = 'amCap1712';
                                            QUERY PLAN
---------------------------------------------------------------------------------------------------
 Seq Scan on "user"  (cost=0.00..77.18 rows=13 width=78) (actual time=2.317..2.317 rows=0 loops=1)
   Filter: ((id)::text = 'amCap1712'::text)
   Rows Removed by Filter: 2631
 Planning Time: 0.069 ms
 Execution Time: 2.336 ms
(5 rows)

critiquebrainz_db=> CREATE INDEX ix_user_id ON "user" USING btree ((id::text));
CREATE INDEX
critiquebrainz_db=> explain analyze select * from "user" where id::text = 'amCap1712';
                                                      QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
 Index Scan using ix_user_id on "user"  (cost=0.28..12.51 rows=13 width=78) (actual time=0.128..0.128 rows=0 loops=1)
   Index Cond: ((id)::text = 'amCap1712'::text)
 Planning Time: 0.328 ms
 Execution Time: 0.158 ms
(4 rows)

critiquebrainz_db=> rollback;
ROLLBACK
@github-actions

This comment has been minimized.

@alastair alastair merged commit 89d9351 into master Oct 4, 2022
@alastair alastair deleted the add-text-index branch October 4, 2022 14:56
@github-actions
Copy link

github-actions bot commented Oct 4, 2022

Unit Test Results

    1 files  ±0      1 suites  ±0   1m 35s ⏱️ ±0s
198 tests ±0  198 ✔️ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 89d9351. ± Comparison against base commit 89d9351.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants