如何在Firebird 2.5.1中加速简单的连接查询

时间:2012-11-05 13:59:08

标签: sql firebird

在两个大型表上执行简单的内部联接查询时,在Firebird 2.5.1中几秒后返回结果。在其他数据库中,结果将在几毫秒后返回。问题是如何加快这个查询,为什么Firebird使用A NATURAL而不是索引?

查询是:

SELECT FIRST 20 * FROM A INNER JOIN B ON A.ID = B.AID;

结果计划是:

PLAN JOIN (A NATURAL, B INDEX (IDX_B_1))

统计:

Statistics

表和索引元数据:

CREATE TABLE "A" 
(
  "ID"   INTEGER NOT NULL,
CONSTRAINT "PK_A" PRIMARY KEY ("ID")
);


CREATE TABLE "B" 
(
  "ID"   INTEGER NOT NULL,
  "AID"  INTEGER,
CONSTRAINT "PK_B" PRIMARY KEY ("ID")
);


CREATE INDEX "IDX_B_1" ON "B"("AID");

要填充,您可以使用此程序:

declare variable nba integer;
declare variable nbb integer;
declare variable nbc integer;

begin
  nba = 0;
  nbb = 0;
  nbc = 0;
  while (nba < 100000) do begin
    insert into A (ID) values (:nba);
    nbc = 0;
    while (nbc < 50) do begin
      insert into B (ID, AID)
      values (:nbb, :nba);
      nbb = nbb + 1;
      nbc = nbc + 1;
    end
    nba = nba + 1;
  end
end

0 个答案:

没有答案