我执行的SQL查询如下:
update elements E
set E.END_I = (select n.node_num
from nodes N
where abs(E.X_I - N.XI) < 0.001 and
abs(E.Y_I - N.YI) < 0.001 and
abs(E.Z_I - N.ZI) < 0.001
)
完成需要大约24秒,我读到关于firebird故障排除Why is my database query slow?它指示为表中的相关字段创建索引,并且我在两个节点和元素中添加了XI,YI,ZI字段的递减/递增索引表.但是性能仍然很慢,数据库中有6677行,我使用FlameRobin作为SQL编辑器.
有趣的是:正如Firebird故障排除指南所描述的那样
If you see a NATURAL plan going against a big table, you’ve found the
problem
这个错误被描述为坏情况和减速源,推荐的解决方案是,为相关字段创建递减指数.但在我的情况下,即使在定义指数之后,似乎我仍然受到计划(N NATURAL),PLAN(E NATURAL)的影响,这在Flamerobin输出中报告如下所示.
我怎么能消除那个?
Preparing query: update elements E set E.END_I = (select n.node_num from nodes N
where abs(E.X_I-N.XI)<0.001 and abs(E.Y_I - N.YI)<0.001 and abs(E.Z_I-N.ZI)<0.001 )
Prepare time: 0.004s
PLAN (N NATURAL)
PLAN (E NATURAL)
Executing...
Done.
108818273 fetches, 79227 marks, 4050 reads, 9380 writes.
0 inserts, 6677 updates, 0 deletes, 0 index, 14549183 seq.
Delta memory: 212 bytes.
ELEMENTS: 6677 updates.
6677 rows affected directly.
Total execution time: 24.038s
Script execution finished.
CREATE DESCENDING INDEX IDX_ELEMENTS1 ON ELEMENTS (Z_I);
CREATE DESCENDING INDEX IDX_XI ON ELEMENTS (X_I);
CREATE DESCENDING INDEX IDX_YI ON ELEMENTS (Y_I);
GRANT DELETE, INSERT, REFERENCES, SELECT, UPDATE
ON ELEMENTS TO SYSDBA WITH GRANT OPTION;
CREATE DESCENDING INDEX IDX_NODES1_XI ON NODES (XI);
CREATE DESCENDING INDEX IDX_NODES1_YI ON NODES (YI);
CREATE DESCENDING INDEX IDX_NODES1_ZI ON NODES (ZI);
GRANT DELETE, INSERT, REFERENCES, SELECT, UPDATE
ON NODES TO SYSDBA WITH GRANT OPTION;
最佳答案 abs()函数会减慢您的查询,因为裸列上的索引不能与表达式一起使用.
尝试更改查询以至少为db提供使用索引的机会
update elements E
set E.END_I = (select n.node_num
from nodes N
where N.XI < E.X_I + 0.001 AND N.XI > E.X_I - 0.001
AND N.YI < E.Y_I + 0.001 AND N.YI > E.Y_I - 0.001
AND N.ZI < E.Z_I + 0.001 AND N.ZI > E.Z_I - 0.001
)