更新时间:2022-12-18 13:27:47
将原始大小写混合的字符串存储在纯文本列中.使用没有长度修饰符的数据类型 text
或 varchar
而不是 varchar(n)
.它们本质上是相同的,但是使用 varchar(n) 您必须设置任意长度限制,如果您以后想更改,这可能会很痛苦.在在手册或在这个Peter Eisentraut @serverfault.SE 的相关回答.
Store the original mixed-case string in a plain text column. Use the data type text
or varchar
without length modifier rather than varchar(n)
. They are essentially the same, but with varchar(n) you have to set an arbitrary length limit, that can be a pain if you want to change later. Read more about that in the manual or in this related answer by Peter Eisentraut @serverfault.SE.
创建功能唯一索引 在 lower(string)
上.这是这里的主要观点:
Create a functional unique index on lower(string)
. That's the major point here:
CREATE UNIQUE INDEX my_idx ON mytbl(lower(name));
如果您尝试INSERT
一个已经存在的小写混合大小写名称,则会出现唯一键违规错误.
对于快速相等搜索,请使用如下查询:
If you try to INSERT
a mixed case name that's already there in lower case you get a unique key violation error.
For fast equality searches use a query like this:
SELECT * FROM mytbl WHERE lower(name) = 'foo' --'foo' is lower case, of course.
使用与索引中相同的表达式(以便查询规划器识别兼容性),这将非常快.
Use the same expression you have in the index (so the query planner recognizes the compatibility) and this will be very fast.
顺便说一句:您可能想要升级到更新版本的 PostgreSQL.自 8.4.2 以来,有很多重要修复.有关官方 Postgres 版本控制站点的更多信息.
As an aside: you may want to upgrade to a more recent version of PostgreSQL. There have been lots of important fixes since 8.4.2. More on the official Postgres versioning site.