我在 pgAdmin 上进行查询时偶然发现了这种奇怪的行为。
我已连接到运行 PostgreSQL 9.1.9 的服务器。
我有一个名为 messages 的表,其定义如下:
ghareh@godot:~$ psql
psql (9.1.9)
Type "help" for help.
ghareh=# \d messages
Table "public.messages"
Column | Type | Modifiers
---------------+-----------------------------+---------------------------------
messageid | character varying(200) | not null
senderaliasid | integer | not null
referenceid | character varying(200) | default NULL::character varying
recipaliasid | integer |
datetime | timestamp(2) with time zone | not null
subject | character varying(512) | not null
body | text | not null
listid | integer |
Indexes:
"messages_pkey" PRIMARY KEY, btree (messageid)
"messages_datetime_idx" btree (datetime)
"recipaliasid_idx" btree (recipaliasid)
"referenceid_idx" btree (referenceid)
"senderaliasid_idx" btree (senderaliasid)
Foreign-key constraints:
"messages_listid_fkey" FOREIGN KEY (listid) REFERENCES lists(listid)
"messages_recip_fkey" FOREIGN KEY (recipaliasid, listid) REFERENCES aliases(aliasid, listid)
"messages_sender_fkey" FOREIGN KEY (senderaliasid, listid) REFERENCES aliases(aliasid, listid)
Referenced by:
TABLE "messages_attachments" CONSTRAINT "pkfkmid" FOREIGN KEY (messageid) REFERENCES messages(messageid)
我的问题涉及列,body
和 subject
。
我有一个生成一组结果的查询。然后,为了优化我的查询,我添加了术语:where body like '%JSON%'
即,body 包含字符串“JSON”的结果子集。
我得到了一些包含这个词的结果,还有一些不包含这个词!但如果我搜索任意字符串,结果就可以了。我检查了一下,发现查询不仅搜索正文列,还搜索主题列,这太疯狂了。
这是我最初的查询:
select * from messages where messageid = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6155552322525051274f5051585152515421150e1313041b4f1412" rel="noreferrer noopener nofollow">[email protected]</a>'
返回 1 行:
messageid: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e7d3d3a5a4d4d6d7a1c9d6d7ded7d4d7d2a793889595829dc99294" rel="noreferrer noopener nofollow">[email protected]</a>";
senderaliasid: 13777;
referenceid: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2512404143404040431513151214121412111357124112151d151312421410461212461644441540431c14101d6548444c490b4248444c490b464a48" rel="noreferrer noopener nofollow">[email protected]</a>";
recipaliasid: ;
datetime: "2006-07-17 20:53:35-07";
listid: 251;
subject: "Re: svn commit: r422930 - /incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java";
body: "busted! thanks for the thorough review.
-Elias
Garrett Rooney wrote:
> On 7/17/06, <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d6dfdad2c0c7f3d2c3d2d0dbd69ddcc1d4" rel="noreferrer noopener nofollow">[email protected]</a> <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="02676e6b63717642637263616a672c6d7065" rel="noreferrer noopener nofollow">[email protected]</a>> wrote:
>> Author: eliast
>> Date: Mon Jul 17 17:44:10 2006
>> New Revision: 422930
>>
>> URL: http://svn.apache.org/viewvc?rev=422930 (...)"
如果我搜索:
select * from messages
where messageid = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f2b2b5d5c2c2e2f59312e2f262f2c2f2a5f6b706d6d7a65316a6c" rel="noreferrer noopener nofollow">[email protected]</a>'
and body like '%JSON%'
我不应该得到任何结果,因为体内没有任何结果。但我仍然得到相同的行返回 - 似乎是因为“JSON”位于 subject
中?
我什至尝试过这个:
select * from messages
where messageid = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a89c9ceaeb9b9998ee86999891989b989de8dcc7dadacdd286dddb" rel="noreferrer noopener nofollow">[email protected]</a>'
and body like '%incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java%'
我仍然得到相同的行。我很困惑。
我尝试在 sqlfiddle.com 上重现结果,但没有成功。在那里,我得到了 sql select 查询的预期结果:
http://sqlfiddle.com/#!1/ec74c/4
最佳答案
您无法在 SQL Fiddle 上重现相同的效果。
我在 Postgres 9.1.13 ( always upgrade to the latest point release! ) 中重新创建了您的表,并在 pgAdmin(当前版本 1.18.1)中运行查询。 我无法重现该问题。
pgAdmin?
我不明白 pgAdmin 如何在其中发挥作用 - 除非您只选择了查询的一部分,没有意识到这种效果:
pgAdmin shortcuts to execute scripts
或者您可能会被“每列最大字符数”设置所欺骗,该设置会截断显示中的长值,隐藏截断部分中的匹配项,例如 @IMSoP suggested in his comment 。检查文件 -> 选项...
如果不是这样,除非我们正在处理您问题中不存在的拼写错误或情况,否则这表明您的数据库中存在某些问题。
腐败?
在索引损坏的简单情况下,REINDEX TABLE
可能会成功:
REINDEX TABLE messages;
但是,仔细观察后,我没有发现可能是罪魁祸首的索引。
系统目录损坏? 先阅读此内容:
http://wiki.postgresql.org/wiki/Corruption
然后阅读Notes section for REINDEX
并从 shell 运行:
$ export PGOPTIONS="-P"
$ psql broken_db
...
broken_db=> REINDEX DATABASE broken_db;
broken_db=> \q
损坏通常表明您的硬件存在问题。出现故障的磁盘或其他东西。跟进...
关于sql - pgAdmin III 的错误行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23553887/