我的表格名为ATTACHMENT,后面有以下栏目:
COLUMN_NAME TYPE_NAME COLUMN_SIZE
---------------------------------------------------
DTYPE VARCHAR 31
ID VARCHAR 36
VERSION BIGINT 19
TYPE INTEGER 10
FILENAME VARCHAR 100
DATA BLOB 9437211
SIZE INTEGER 10
CHECKSUM BIGINT 19
AUTHOR VARCHAR 36
FILEDATE DATE 10
FILETIME TIME 8
CREATIONDATE DATE 10
CREATIONTIME TIME 8
FILETYPE INTEGER 10
SYSTEM SMALLINT 5
ORIGINALPICTUREID VARCHAR 36
COMPRESSEDPICTUREID VARCHAR 36
FIRSTUSE VARCHAR 120
当我运行简单的测试SQL查询时:
SELECT ID FROM ATTACHMENT WHERE ORIGINALPICTUREID IS NOT NULL;
此查询执行很长时间(30秒)
但是当我在没有IS NOT NULL表达式的情况下运行下一个测试SQL查询时:
SELECT ID FROM ATTACHMENT WHERE ORIGINALPICTUREID IS NULL;
此查询仅执行2秒。
在实际系统中我有脚本:
select ATTACHMENT.ID,
ATTACHMENT.SIZE,
ATTACHMENT.AUTHOR,
ATTACHMENT.FILENAME,
ATTACHMENT.FILETIME,
ATTACHMENT.FILEDATE,
ATTACHMENT.CREATIONDATE,
ATTACHMENT.CREATIONTIME,
ATTACHMENT.FILETYPE,
ATTACHMENT.COMPRESSEDPICTUREID,
ATTACHMENT.ORIGINALPICTUREID,
ATTACHMENT.FIRSTUSE
from ATTACHMENT,
MESSAGECONTENT_ATTACHMENT,
MESSAGECONTENT
where ATTACHMENT.ID not in (select distinct ATTACHMENT.ORIGINALPICTUREID
from ATTACHMENT
where ATTACHMENT.ORIGINALPICTUREID is not null)
and ATTACHMENT.ID not in (select distinct COMPRESSEDPICTUREID
from ATTACHMENT
where ORIGINALPICTUREID is not null)
and MESSAGECONTENT_ATTACHMENT.MESSAGECONTENT_ID = MESSAGECONTENT.ID
and MESSAGECONTENT_ATTACHMENT.ATTACHMENTS_ID = ATTACHMENT.ID
and ATTACHMENT.DTYPE = 'P'
and MESSAGECONTENT.PERSONIDPATIENT = '0584393a-0955-4c9b-98f7-d31c991d22a3'
and (ATTACHMENT.FILENAME like '%jpeg'
or ATTACHMENT.FILENAME like '%jpg'
or ATTACHMENT.FILENAME like '%tiff'
or ATTACHMENT.FILENAME like '%tif'
or ATTACHMENT.FILENAME like '%bmp'
or ATTACHMENT.FILENAME like '%gif'
or ATTACHMENT.FILENAME like '%png'
or ATTACHMENT.FILENAME like '%ser')
这个脚本执行的时间非常长。
你能帮我解决一下如何在我的Derby DB的SQL查询中解决IS NOT NULL表达式的问题吗?
非常感谢!
答案 0 :(得分:1)
你在这个问题上自杀了,主要是因为你没有空的...你分别在原始和压缩的情况下吹过所有附件,但你只对一个病人感兴趣。我已经将查询重新构建为与您想要的WHO一起开始......患者角色ID。从那里,加入到邮件附件。你只关心这个ONE PERSON附带的任何东西。这应该导致一组非常小的记录。在那些记录中,只有那些你关心查看附件表本身,看看是否有资格获得你的DPTYPE,如条件和IS NULL。
我会确保你的消息内容表上最少有一个索引(PersonIDPatient),如果在第一个位置之后有任何其他列,没问题。与其他表的连接似乎位于各自的主ID列上,并假设您有这些表的索引。
SELECT
atch.ID,
atch.SIZE,
atch.AUTHOR,
atch.FILENAME,
atch.FILETIME,
atch.FILEDATE,
atch.CREATIONDATE,
atch.CREATIONTIME,
atch.FILETYPE,
atch.COMPRESSEDPICTUREID,
atch.ORIGINALPICTUREID,
atch.FIRSTUSE
FROM
MESSAGECONTENT msgCont
JOIN MESSAGECONTENT_ATTACHMENT msgAtt
ON msgCont.ID = msgAtt.MESSAGECONTENT_ID
JOIN ATTACHMENT atch
ON msgAtt.ATTACHMENTS_ID = atch.ID
AND atch.DTYPE = 'P'
AND atch.ORIGINALPICTUREID IS NOT NULL
AND atch.CompressedPictureID IS NOT NULL
AND ( atch.FILENAME LIKE '%jpeg'
OR atch.FILENAME LIKE '%jpg'
OR atch.FILENAME LIKE '%tiff'
OR atch.FILENAME LIKE '%tif'
OR atch.FILENAME LIKE '%bmp'
OR atch.FILENAME LIKE '%gif'
OR atch.FILENAME LIKE '%png'
OR atch.FILENAME LIKE '%ser')
WHERE
msgCont.PersonIDPatient = '0584393a-0955-4c9b-98f7-d31c991d22a3'
答案 1 :(得分:0)
查询中的NOT IN运算符不使用任何索引。 -
避免在查询中使用NOT IN运算符。
为了找到不符合某个标准的结果,它必须根据条件检查所有记录,这使得索引的存在无关紧要。
此外,不使用通配符%,而是尝试使用全文索引并查询数据库,如
选择Col1,Col2,.......
表中的
Col1 CONTAINS(Col1,'Search')和Col1 CONTAINS(Col1,'Search2').........