【问题标题】:Cache Table data and performe search on it缓存表数据并对其执行搜索
【发布时间】:2014-08-12 08:36:55
【问题描述】:

我正在编写一个 SQL 过程来执行以下任务。

  1. 从表 A 中读取数据。其中的列是 Col1、Col2、Col3、Col4、Col5、Col6。
  2. 我必须在表 B 中搜索主键(数字)并归档 Col1、Col2、Col3。
  3. 在表6中插入主键COL4,COL5,COL6。

表 A 可以包含任意数量的 COL1、COL2 和 COL3 组合。

在这里,我正在读取表 A 中的所有数据,但我不想每次都在表 B 上触发搜索查询以找出表 B 中的键。但我喜欢在某处存储/获取表 B 的数据(地图等),然后想在那里搜索并插入表 C。在 PL/SQL 中可以吗?

简而言之,我想缓存表 B 的数据并执行搜索并获取那里的数据。

【问题讨论】:

  • 为什么不在查询中将表连接在一起——让数据库做它擅长的事情?您在这里真的需要任何 PL/SQL - 听起来您可以通过一个简单的步骤 insert ... select 吗?
  • 谢谢亚历克斯。我试过了,但问题是如果表 B 中缺少参考,那么我将无法弄清楚。
  • 这不就是外连接的用途吗?我相信缓存和 RobertK 的答案对你有用,但你可能想寻找未来的替代方案。

标签: oracle plsql plsqldeveloper


【解决方案1】:

我们经常使用数组来缓存查找值的数据,例如我们有一个 t_sites 表并且我们想要缓存整个数据集,主键是 varchar2(10) 所以我们创建一个索引为varchar2(10) ... 这是代码示例:

declare
-- type and array to hold all the data from the query
type sites_rec_t is record (
    addr_id             t_sites.addr_id%type,
    code                t_sites.code%type,
    description         t_sites.description%type,
    pm_telephone        t_sites.pm_telephone%type
);

-- array for the lookups
type sites_t is table of sites_rec_t index by varchar2(10);
a_sites sites_t;
v_site_arr_key  varchar2(10);

begin

-- populate the arrays
for r in ( select addr_id, code, description, pm_telephone from t_sites ) loop
    a_sites(r.code).addr_id := r.addr_id;
    a_sites(r.code).code := r.code;
    a_sites(r.code).description := r.description;
    a_sites(r.code).pm_telephone := r.pm_telephone;
end loop;

-- example of how to loop through the varchar indexed array as you cant do for i in 1 .. array.count loop
v_site_arr_key := a_sites.first;
while v_site_arr_key is not null loop
    dbms_output.put_line(v_site_arr_key || '=' || a_sites(v_site_arr_key).description || ', ' || a_sites(v_site_arr_key).addr_id);
    v_site_arr_key := a_sites.next(v_site_arr_key);
end loop;

end;

【讨论】:

    猜你喜欢
    • 2016-05-10
    • 2018-01-18
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多