简体   繁体   English

提高 JDBC 查询的性能 - ArrayList 比较

[英]Improve performance in JDBC query - ArrayList comparison

I've been trying to get the most efficient way to get a set of values from a DB and comparing them with their respective sub-string to save specific values... Here is an example:我一直在尝试以最有效的方式从数据库中获取一组值,并将它们与各自的子字符串进行比较以保存特定值……这是一个示例:
I have this table which contains a PRODUCT(ID), from the ones in LVL - 7 I have to substring the values until I get to LVL1我有一张包含 PRODUCT(ID) 的表,来自 LVL - 7 我必须对值进行子串,直到到达 LVL1

LVL     PRODUCT          DESCRIPTION
5       01AR2TGELL       DESLVL5
6       01AR2TGELLGS     DESLVL6
7       01AR2TGELLGSQKA  DESLVL7
6       01AR2TGELLDE     DESLVL6
7       01AR2TGELLDEUDP  DESLVL7
7       01AR2TGELLDEUZN  DESLVL7

So I can get something like this:所以我可以得到这样的东西:

LVL   PRODUCT          DESCRIPTION  | LVL   PRODUCT        DESCRIPTION  | LVL  PRODUCT     DESCRIPTION
7     01AR2TGELLGSQKA  DESLVL7      | 6     01AR2TGELLGS   DESLVL6      | 5    01AR2TGELL  DESLVL5
7     01AR2TGELLDEUDP  DESLVL7      | 6     01AR2TGELLDE   DESLVL6      | 5    01AR2TGELL  DESLVL5
7     01AR2TGELLDEUZN  DESLVL7      | 6     01AR2TGELLDE   DESLVL6      | 5    01AR2TGELL  DESLVL5

I'm working with Java (Requirement) and also I'm using JDBC to connect to the Oracle DB.我正在使用 Java(要求),并且我正在使用 JDBC 连接到 Oracle DB。 What I did is to get all the values from LVL7 with a query and save all those values in an ArrayList then, with a LVL and a Value of the PRODUCT Substring I compare the values, like this:我所做的是通过查询从 LVL7 获取所有值,然后将所有这些值保存在 ArrayList 中,然后使用 LVL 和 PRODUCT 子字符串的值比较这些值,如下所示:

"select * from PRODHIERARCHY where LVL=7"
"select * from PRODHIERARCHY WHERE LVL= 6 AND PRODUCT_HIERARCHY="+"'"+descLv7.get(j).substring(0, descLv7.get(j).length() - 4)+"'"

My problem is, there is approx.我的问题是,大约有。 35 thousand LVL7 values, and in total around 65 thousand so this is very inefficient. 3.5 万个 LVL7 值,总共大约 6.5 万个,所以这是非常低效的。 (To get the 35 thousand LVL7 values takes around 3 minutes since the DB is processing everything, when it gets to the comparison with the ArrayList it takes around 30 minutes per LVL). (要获得 35,000 LVL7 值大约需要 3 分钟,因为 DB 正在处理所有内容,当它与 ArrayList 进行比较时,每个 LVL 大约需要 30 分钟)。

Can anyone please recommend me a way to modify either the query or my comparison process so it takes about the same time as the first query?任何人都可以向我推荐一种修改查询或我的比较过程的方法,以便它与第一次查询的时间大致相同吗? Thanks!谢谢!

Note: I don't have Oracle, so I made it with MSSQL, just use substr instead of left for the artificial keys.注意:我没有 Oracle,所以我用 MSSQL 制作了它,只需使用substr而不是left作为人工键。

Here's an static idea.这是一个静态的想法。 Lets see, first let us create a view with some computed columns (artificial keys) (or you could add them to your product table and index them)让我们看看,首先让我们创建一个带有一些计算列(人工键)的视图(或者您可以将它们添加到您的产品表中并索引它们)

create view vw_product_computed as
select
    a.lvl,
    a.Product,
    left(a.product, 12) as a12, -- your first lelvel down (artificial key) 
    left(a.product, 10) as a10  -- your seccond level down (artificial key) 
from
    products a;

Then is just a matter of joining your product table with your view using your artificial keys:然后只需使用您的人工键将您的产品表与您的视图连接起来:

select
    *
from
    desarrollo.vw_product_computed a
join dbo.products b on b.product = a.a12
join dbo.products c on c.product = a.a10
where
    a.lvl = 7;

And off you go, you have your result set as you need it:然后你就可以得到你需要的结果集: 加入结果

If you would like to make it a dynamic thing I guess a pivot table may come in handy.如果你想让它成为一个动态的东西,我想一个数据透视表可能会派上用场。

Hope this helps mate.希望这有助于队友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM