我有一张大约有246k记录的表格。它有大约25列,除了一个小blob外都是整数。
如果我在所有字段上查询表
select a.recordid, a.editcount, ect.. from ARTrans a
它在一秒钟内执行。但是,如果我只包括记录ID
select a.recordID from ARTrans a
执行需要20多秒。大部分时间都花在计划(自然)上,这似乎很奇怪,因为在大多数情况下,我有一个关于recordid的索引。
我收集了垃圾,重新创建了索引,删除了索引,在RecordID上只添加了一个索引,但它仍然超级慢。
非常感谢任何帮助。
已修改以提供更多信息:
Firebird:2.5.3.26778
fbclient.dll:2.5.1.26351
数据库中没有其他人,我已将其移至本地。
这是表def
CREATE TABLE ARTRANS
(
RECORDID Integer NOT NULL,
EDITCOUNT Smallint,
CLASSIFICATION Smallint,
TRANSID Integer,
DATEENTERED Integer,
CLIENTID Integer,
TRANSTYPE Smallint,
BILLED Smallint,
FINALIZEID Smallint,
INVOICEID Integer,
INVOICENUM Integer,
INVOICEDATE Integer,
GROUPID Smallint,
EXPORTED Char(1),
TRANSVALUE Decimal(18,4),
DESCRIPTION Blob sub_type 0,
POSTPERIOD Smallint,
LINKEDTRANSID Integer,
LINKEDINVID Integer,
LINKEDFUNDSID Integer,
INFOONLY Smallint,
NEEDTRANSFER Char(1),
DESTTRANSID Integer,
LSTTKREDIT Integer,
SPELLNGRAMMARCHECKSTATUS Smallint
);
索引
CREATE UNIQUE INDEX IDX_ARTRANSRecID ON ARTRANS (RECORDID);
SQL语句:
SELECT a.RECORDID FROM ARTRANS a
计划(从flamerobin输出)
Preparing query:
SELECT a.RECORDID
FROM ARTRANS a
Prepare time: 20.008s
Field #01: ARTRANS.RECORDID Alias:RECORDID Type:INTEGER
PLAN (A NATURAL)
Executing...
Done.
13257 fetches, 0 marks, 76 reads, 0 writes.
0 inserts, 0 updates, 0 deletes, 0 index, 6552 seq.
Delta memory: -19204 bytes.
Total execution time: 20.025s
Script execution finished.
此SQL语句可以正常工作:
Preparing query:
SELECT a.RECORDID, a.EDITCOUNT, a.CLASSIFICATION,
a.TRANSID, a.DATEENTERED, a.CLIENTID, a.TRANSTYPE, a.BILLED, a.FINALIZEID,
a.INVOICEID, a.INVOICENUM, a.INVOICEDATE, a.GROUPID, a.EXPORTED,
a.TRANSVALUE, a.DESCRIPTION, a.POSTPERIOD, a.LINKEDTRANSID, a.LINKEDINVID,
a.LINKEDFUNDSID, a.INFOONLY, a.NEEDTRANSFER, a.DESTTRANSID, a.LSTTKREDIT,
a.SPELLNGRAMMARCHECKSTATUS, a.RDB$DB_KEY
FROM ARTRANS a
Prepare time: 0.013s
Field #01: ARTRANS.RECORDID Alias:RECORDID Type:INTEGER
Field #02: ARTRANS.EDITCOUNT Alias:EDITCOUNT Type:SMALLINT
Field #03: ARTRANS.CLASSIFICATION Alias:CLASSIFICATION Type:SMALLINT
Field #04: ARTRANS.TRANSID Alias:TRANSID Type:INTEGER
Field #05: ARTRANS.DATEENTERED Alias:DATEENTERED Type:INTEGER
Field #06: ARTRANS.CLIENTID Alias:CLIENTID Type:INTEGER
Field #07: ARTRANS.TRANSTYPE Alias:TRANSTYPE Type:SMALLINT
Field #08: ARTRANS.BILLED Alias:BILLED Type:SMALLINT
Field #09: ARTRANS.FINALIZEID Alias:FINALIZEID Type:SMALLINT
Field #10: ARTRANS.INVOICEID Alias:INVOICEID Type:INTEGER
Field #11: ARTRANS.INVOICENUM Alias:INVOICENUM Type:INTEGER
Field #12: ARTRANS.INVOICEDATE Alias:INVOICEDATE Type:INTEGER
Field #13: ARTRANS.GROUPID Alias:GROUPID Type:SMALLINT
Field #14: ARTRANS.EXPORTED Alias:EXPORTED Type:STRING(1)
Field #15: ARTRANS.TRANSVALUE Alias:TRANSVALUE Type:NUMERIC(18,4)
Field #16: ARTRANS.DESCRIPTION Alias:DESCRIPTION Type:BLOB SUB_TYPE 0
Field #17: ARTRANS.POSTPERIOD Alias:POSTPERIOD Type:SMALLINT
Field #18: ARTRANS.LINKEDTRANSID Alias:LINKEDTRANSID Type:INTEGER
Field #19: ARTRANS.LINKEDINVID Alias:LINKEDINVID Type:INTEGER
Field #20: ARTRANS.LINKEDFUNDSID Alias:LINKEDFUNDSID Type:INTEGER
Field #21: ARTRANS.INFOONLY Alias:INFOONLY Type:SMALLINT
Field #22: ARTRANS.NEEDTRANSFER Alias:NEEDTRANSFER Type:STRING(1)
Field #23: ARTRANS.DESTTRANSID Alias:DESTTRANSID Type:INTEGER
Field #24: ARTRANS.LSTTKREDIT Alias:LSTTKREDIT Type:INTEGER
Field #25: ARTRANS.SPELLNGRAMMARCHECKSTATUS Alias:SPELLNGRAMMARCHECKSTATUS Type:SMALLINT
Field #26: ARTRANS.DB_KEY Alias:DB_KEY Type:STRING(8)
PLAN (A NATURAL)
Executing...
Done.
1135 fetches, 0 marks, 7 reads, 0 writes.
0 inserts, 0 updates, 0 deletes, 0 index, 560 seq.
Delta memory: 25852 bytes.
Total execution time: 0.047s
Script execution finished.
此外,我应该补充一点,有246804条记录,差不多花了一分钟才算得上。
Preparing query: SELECT count(*) FROM ARTRANS a
Prepare time: 52.614s
Field #01: .COUNT Alias:COUNT Type:INTEGER
PLAN (A NATURAL)
Executing...
Done.
499643 fetches, 0 marks, 3016 reads, 0 writes.
0 inserts, 0 updates, 0 deletes, 0 index, 246804 seq.
Delta memory: -18576 bytes.
Total execution time: 52.716s
Script execution finished.
更新
如果删除blob列,则返回性能。如果我保留它并将所有值都清空,性能仍然很快。如果我更新每条记录上的blob字段以包含20字节的流,则性能可以追溯到20多秒以执行简单查询
select a.RecordID from ARTrans a
我甚至更进一步删除了除了blob字段和recordID之外的所有列,我仍然变慢了。看起来像以前一样。非常奇怪。
答案 0 :(得分:1)
要提高性能,请创建仅包含id和blob字段的单独表,并仅在需要此blob字段时才将其与表连接。 如果需要计数行,则使用count(RECORDID)(因为它被索引并且对于所有行都不为null)。