Comments (7)
Hi @pirj ,
I have finally a PR for this (#147) to be fixed but I tested the planner and I think we still would better have a proper (lower) index instead.
The reason is that ActiveRecord doesn't recognize citext
and apply lower
anyway:
Entity.new(email: 'TMPTMP').valid?
Entity Exists? (0.3ms) SELECT 1 AS one FROM "entities" WHERE LOWER("entities"."email") = LOWER($1) LIMIT $2 [["email", "TMPTMP"], ["LIMIT", 1]]
database_consistency_test=# \d tmp;
Table "public.tmp"
Column | Type | Collation | Nullable | Default
--------+--------+-----------+----------+---------
email | citext | | |
Indexes:
"tmp_idx" btree (email)
database_consistency_test=# explain analyze select * from tmp where lower(email) = lower('asdfasdf');
QUERY PLAN
----------------------------------------------------------------------------------------------------
Seq Scan on tmp (cost=0.00..2040.00 rows=500 width=9) (actual time=26.415..26.415 rows=0 loops=1)
Filter: (lower((email)::text) = 'asdfasdf'::text)
Rows Removed by Filter: 100000
Planning Time: 0.296 ms
Execution Time: 26.429 ms
(5 rows)
database_consistency_test=# explain analyze select * from tmp where email = 'asdfasdf';
QUERY PLAN
------------------------------------------------------------------------------------------------------------------
Index Only Scan using tmp_idx on tmp (cost=0.42..4.44 rows=1 width=9) (actual time=0.044..0.045 rows=0 loops=1)
Index Cond: (email = 'asdfasdf'::citext)
Heap Fetches: 0
Planning Time: 0.047 ms
Execution Time: 0.057 ms
(5 rows)
And after adding a lower index:
database_consistency_test=# \d tmp;
Table "public.tmp"
Column | Type | Collation | Nullable | Default
--------+--------+-----------+----------+---------
email | citext | | |
Indexes:
"tmp_idx" btree (lower(email::text))
database_consistency_test=# explain analyze select * from tmp where email = 'asdfasdf';
QUERY PLAN
--------------------------------------------------------------------------------------------------
Seq Scan on tmp (cost=0.00..1790.00 rows=1 width=9) (actual time=55.766..55.767 rows=0 loops=1)
Filter: (email = 'asdfasdf'::citext)
Rows Removed by Filter: 100000
Planning Time: 0.134 ms
Execution Time: 55.777 ms
(5 rows)
database_consistency_test=# explain analyze select * from tmp where lower(email) = lower('asdfasdf');
QUERY PLAN
-------------------------------------------------------------------------------------------------------------
Index Scan using tmp_idx on tmp (cost=0.42..8.44 rows=1 width=9) (actual time=0.040..0.041 rows=0 loops=1)
Index Cond: (lower((email)::text) = 'asdfasdf'::text)
Planning Time: 0.086 ms
Execution Time: 0.054 ms
(4 rows)
With that said, I will abandon the PR and close the issue.
Please feel free to reopen it.
from database_consistency.
Wow! Well done! Unfortunately, my past experience contributing to ROR wasn't great so I didn't expect it's actually possible
from database_consistency.
Wow, you do the magic! Thank you for improving Rails experience for all of us!
from database_consistency.
Hi @pirj ,
It makes total sense for me
from database_consistency.
Hey @djezzzl 👋
Thanks for giving it a shot.
There's this deficiency with a redundant lower
that needs to be fixed on the Rails side.
At this stage I don't see how database_consistency
could handle it.
from database_consistency.
rails/rails#46568 one done, one to go
from database_consistency.
from database_consistency.
Related Issues (20)
- `ColumnPresenceChecker` and polymorphic association HOT 1
- `ColumnPresenceChecker`, `belongs_to `associations, and `optional`, `required` options HOT 3
- Checker that makes sure that enum type is used to store ActiveRecord's enums HOT 14
- EnumValueChecker doesn't work with ActiveRecord below 7 HOT 4
- Add ability to ignore certain namespaces HOT 5
- Does this gem recognize a find on a column without index? HOT 4
- database_consistency -f generates duplicate migrations HOT 3
- Inconsistency found in NullConstaintChecker HOT 3
- [Feature] Check constraint checker HOT 2
- Excluding Database Connections to Non Primary Databases Including sqlserver HOT 2
- Ensure that generated todo files are created with consistent ordering HOT 6
- MissingIndexChecker violation on polymorphic association HOT 3
- ForeignKeyCascadeChecker default mapping HOT 3
- "undefined method `null' for nil:NilClass" in column_presence_checker.rb:54 HOT 7
- UniqueIndexChecker and postgresql partial indexes HOT 1
- Extendable database_consistency HOT 5
- Checker for implicit_order_column for table with uuid as primary key HOT 8
- False-positive when index columns are defined as a string and not array HOT 4
- ActiveRecord 7.0.5 internal error HOT 3
- MissingAssociationClassChecker fails with `undefined method klass for nil:NilClass` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from database_consistency.