简体   繁体   English

Java SQLite JDBC“ SELECT”语句非常慢

[英]Java SQLite JDBC “SELECT” statement very slow

From an API i get ~9000 entries which i have to insert into a table. 从一个API我得到〜9000项,我必须将它们插入表中。 Detailed data for every of these entries is stored in another table with ~45000 entries 这些条目中每一个的详细数据都存储在另一个表中,其中包含约45000个条目

Now i am trying to insert these 9000 entries into a new table and getting the needed information about these entries from the other table. 现在,我试图将这9000个条目插入到新表中,并从另一个表中获取有关这些条目的所需信息。

The SELECT statement for the data in the table with 45000 entries takes something between 30-90ms each time. 带有45000个条目的表中数据的SELECT语句每次花费30-90ms。

The code for the select is as follows (Repeated 9000 times): 选择的代码如下(重复9000次):

                long startTime = System.nanoTime();

                PreparedStatement selectDB = cStatic.prepareStatement("SELECT column1, column2, column3, column4 FROM DB WHERE ID = ?");
                selectDB.setInt(1, rsIDs.getInt(3));

                ResultSet rsDB = selectDB.executeQuery();

                long endTime = System.nanoTime();

                long duration = (startTime - endTime) / 1000000;

From the whole process (collecting information, iserting) the SELECT + ResultSet alone take 95% of the processing time, i'd like to know how i can speed this up 从整个过程(收集信息,分配信息)中,仅SELECT + ResultSet占用了95%的处理时间,我想知道如何才能加快处理速度

Since i already searched for solutions i am already using indexes for "getInt/getString" from result set and also only call the needed columns of the table instead of the full row, but only a very minor improvement. 因为我已经在寻找解决方案,所以我已经在结果集中使用索引“ getInt / getString”,并且只调用表中所需的列而不是整个行,但这只是一个很小的改进。

Every other statement (UPDATE , INSERT, DELETE) hardly even uses 1ms, why is SELECT needing so much time? 其他每条语句(UPDATE,INSERT,DELETE)几乎都不使用1ms,为什么SELECT需要那么多时间?

EDIT: 编辑:

Didn't really speed up the SELECT , but i was rebuilding the code to only use the SELECT once and store every important data into an array , now it's done in 2-3 seconds instead of 5 minutes. 并没有真正加快SELECT的速度,但是我正在重建代码以仅使用SELECT一次,并将所有重要数据存储到数组中,现在只需2-3秒即可完成,而不是5分钟。

Thanks to Rahul i tried giving the columns an index and it helped a bit too: 感谢Rahul,我尝试为列提供索引,它也有所帮助:

ALTER TABLE table_name ADD INDEX idx_id ( ID ); ALTER TABLE table_name添加索引idx_idID );

30ms shouldn't be counted as slow though, you may want to make sure that you have a index created or index in place for the ID column since you are using that column in your WHERE condition as WHERE ID = ? 但是30ms不应算慢,您可能要确保为ID列创建了索引或索引,因为您在WHERE条件下使用该列作为WHERE ID = ?

No I mean create an unique index on ID column if you don't have already by saying below which will eventually speed up your query by performing an index scan. 不,我的意思是如果您还没有在ID列上创建唯一索引,请在下面说一遍,这将最终通过执行索引扫描来加快查询速度。

ALTER TABLE table_name ADD INDEX `idx_id` (`ID`);

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

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