如何优化Firebird SQL查询?

时间:2018-03-23 18:57:27

标签: sql firebird firebird-3.0

数据库:Firebird 3.0

此查询用于选择符合特定条件的记录,以便检测哪些DEVICEID的{​​{1}}比LAST_TICK知道的其他DEVICEID={INTEGER VALUE}DEVICEID select a.TABELA, a.DEVICEID, (select LAST_TICK from KNOWLEDGE b where b.DEVICEID={INTEGER VALUE} and b.CROSSID=a.DEVICEID and b.TABELA=a.TABELA) as MEU_TICK, max(LAST_TICK) as MAX_TICK from KNOWLEDGE a where a.DEVICEID=a.CROSSID group by a.TABELA, a.DEVICEID having max(LAST_TICK) > (select LAST_TICK from KNOWLEDGE b where b.DEVICEID={INTEGER VALUE} and b.CROSSID=a.DEVICEID and b.TABELA=a.TABELA) order by a.TABELA 1}}的

这是SQL查询:

Plan
PLAN (B INDEX (PK_KNOWLEDGE))
PLAN (B INDEX (PK_KNOWLEDGE))
PLAN SORT (A NATURAL)

------ Performance info ------
Prepare time = 0ms
Execute time = 0ms
Current memory = 36.473.632
Max memory = 36.729.200
Memory buffers = 2.048
Reads from disk to cache = 0
Writes from cache to disk = 0
Fetches from cache = 65

Select Expression
-> Singularity Check
    -> Filter
        -> Table "KNOWLEDGE" as "B" Access By ID
            -> Bitmap
                -> Index "PK_KNOWLEDGE" Unique Scan
Select Expression
-> Singularity Check
    -> Filter
        -> Table "KNOWLEDGE" as "B" Access By ID
            -> Bitmap
                -> Index "PK_KNOWLEDGE" Unique Scan
Select Expression
-> Filter
    -> Aggregate
        -> Sort (record length: 214, key length: 176)
            -> Filter
                -> Table "KNOWLEDGE" as "A" Full Scan

仅有11条记录的绩效统计数据;

CREATE TABLE KNOWLEDGE (
DEVICEID   "INTEGER" NOT NULL /* "INTEGER" = INTEGER */,
TABELA     VARCHAR40 NOT NULL /* VARCHAR40 = VARCHAR(40) */,
CROSSID    "INTEGER" NOT NULL /* "INTEGER" = INTEGER */,
LAST_TICK  "INTEGER" NOT NULL /* "INTEGER" = INTEGER */
);
ALTER TABLE KNOWLEDGE ADD CONSTRAINT PK_KNOWLEDGE PRIMARY KEY (DEVICEID, TABELA, CROSSID);
CREATE INDEX KNOWLEDGE_IDX1 ON KNOWLEDGE (TABELA);
CREATE INDEX KNOWLEDGE_IDX2 ON KNOWLEDGE (DEVICEID, CROSSID);

***编辑1: DDL:

INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (4, 'PESSOAS', 4, 11);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (4, 'PESSOAS', 5, 47);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (4, 'PESSOAS', 6, 118);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (4, 'PESSOAS', 7, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (4, 'PESSOAS', 8, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (4, 'PESSOAS', 9, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (4, 'PESSOAS', 10, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (4, 'PESSOAS', 11, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (4, 'PESSOAS', 12, 5);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (5, 'PESSOAS', 4, 11);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (5, 'PESSOAS', 5, 47);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (5, 'PESSOAS', 6, 118);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (5, 'PESSOAS', 7, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (5, 'PESSOAS', 8, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (5, 'PESSOAS', 9, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (5, 'PESSOAS', 10, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (5, 'PESSOAS', 11, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (5, 'PESSOAS', 12, 5);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (6, 'PESSOAS', 4, 11);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (6, 'PESSOAS', 5, 47);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (6, 'PESSOAS', 6, 118);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (6, 'PESSOAS', 7, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (6, 'PESSOAS', 8, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (6, 'PESSOAS', 9, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (6, 'PESSOAS', 10, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (6, 'PESSOAS', 11, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (6, 'PESSOAS', 12, 5);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (7, 'PESSOAS', 4, 11);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (7, 'PESSOAS', 5, 47);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (7, 'PESSOAS', 6, 118);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (7, 'PESSOAS', 7, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (7, 'PESSOAS', 8, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (7, 'PESSOAS', 9, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (7, 'PESSOAS', 10, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (7, 'PESSOAS', 11, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (7, 'PESSOAS', 12, 5);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (8, 'PESSOAS', 4, 11);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (8, 'PESSOAS', 5, 47);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (8, 'PESSOAS', 6, 118);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (8, 'PESSOAS', 7, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (8, 'PESSOAS', 8, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (8, 'PESSOAS', 9, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (8, 'PESSOAS', 10, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (8, 'PESSOAS', 11, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (8, 'PESSOAS', 12, 5);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (9, 'PESSOAS', 4, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (9, 'PESSOAS', 5, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (9, 'PESSOAS', 6, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (9, 'PESSOAS', 7, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (9, 'PESSOAS', 8, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (9, 'PESSOAS', 9, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (9, 'PESSOAS', 10, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (9, 'PESSOAS', 11, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (9, 'PESSOAS', 12, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (10, 'PESSOAS', 4, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (10, 'PESSOAS', 5, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (10, 'PESSOAS', 6, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (10, 'PESSOAS', 7, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (10, 'PESSOAS', 8, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (10, 'PESSOAS', 9, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (10, 'PESSOAS', 10, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (10, 'PESSOAS', 11, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (10, 'PESSOAS', 12, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (11, 'PESSOAS', 4, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (11, 'PESSOAS', 5, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (11, 'PESSOAS', 6, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (11, 'PESSOAS', 7, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (11, 'PESSOAS', 8, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (11, 'PESSOAS', 9, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (11, 'PESSOAS', 10, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (11, 'PESSOAS', 11, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (11, 'PESSOAS', 12, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (12, 'PESSOAS', 4, 11);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (12, 'PESSOAS', 5, 47);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (12, 'PESSOAS', 6, 118);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (12, 'PESSOAS', 7, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (12, 'PESSOAS', 8, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (12, 'PESSOAS', 9, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (12, 'PESSOAS', 10, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (12, 'PESSOAS', 11, 0);
INSERT INTO KNOWLEDGE (DEVICEID, TABELA, CROSSID, LAST_TICK)
           VALUES (12, 'PESSOAS', 12, 10);

COMMIT WORK;

示例数据:

{{1}}

逻辑细节:

用于数据同步算法。该知识表保存来自系统中注册的每个设备的知识(Last Update Tick),并将其存储在服务器中。客户端设备连接到服务器并发送更新的记录,服务器更新其知识表以反映这些更改。然后,其他客户端设备连接到服务器以获取这些更改并更新其本地数据库。

知识表的逻辑是这样的:

D-Hey服务器,我是设备4,我需要知道我有什么新东西吗?

S-K,等一下,让我检查一下。嘿数据库服务器,从知识表中获取DEVICEID的LAST_TICK高于我自己的知识(CROSSID = DEVICEID)的所有记录,还在该查询中输入设备自己的知识,以便服务器更容易构建查询对于那些记录。

DB-当然,这是列表

S-很好,现在告诉我这些表中的所有记录,即update_tick高于设备4的知识。

Db-是的,就是这样。

S-hey设备,获取这些记录并进行更新

d-np,完成。谢谢你......

使用上面提供的测试数据,结果应该是带有值的单行,无论我们在{INTEGER VALUE}中提供什么设备ID: {TABELA:“PESSOAS”,DEVICEID:12,MEU_TICK:5,MAX_TICK:12}

2 个答案:

答案 0 :(得分:0)

你可以像这样创建一个cte:

With cte_base AS (
select distinct LAST_TICK 
from KNOWLEDGE b 
where b.DEVICEID={INTEGER VALUE} and b.CROSSID=a.DEVICEID 
and b.TABELA=a.TABELA
)

select 
    a.TABELA, 
    a.DEVICEID,
    cte_base.LAST_TICK as MEU_TICK,
    max(LAST_TICK) as MAX_TICK 
from 
    KNOWLEDGE a 
where 
    a.DEVICEID=a.CROSSID 
group by
    a.TABELA, a.DEVICEID 
having 
    max(LAST_TICK) > (cte_base.LAST_TICK) 
order by 
    a.TABELA

答案 1 :(得分:0)

可能会改善性能的非常简单的改变正在改变

group by
    a.TABELA, a.DEVICEID 

group by
    a.DEVICEID, a.TABELA

这将允许优化器使用PK_KNOWLEDGE进行聚合所需的排序。

通过将您的查询重写为:

,可以实现进一步的优化
with max_tick as (
    select 
       TABELA,
       DEVICEID,
       MAX(LAST_TICK) as MAX_TICK
    from KNOWLEDGE
    where DEVICEID = CROSSID
    group by DEVICEID, TABELA
)
select 
   a.TABELA,
   a.DEVICEID,
   a.MAX_TICK,
   b.LAST_TICK AS MEU_TICK
from max_tick a
inner join KNOWLEDGE b 
   on a.DEVICEID = b.CROSSID AND a.TABELA = b.TABELA and a.MAX_TICK > b.LAST_TICK
where b.DEVICEID = {INTEGER VALUE}
order by a.TABELA

但我不能100%确定这个查询是否相同,因为您的示例数据没有提供足够的变体来测试它。