简体   繁体   English

如何在oracle中提高简单选择查询的性能

[英]How to improve performance of a simple select query in oracle

I recently got into an interview and I was asked a question 我最近接受了采访,我被问到一个问题

We have a table employee(id, name) . 我们有一个表employee(id, name) And in our java code, we are writing a logic to fetch data from this table and display it in UI. 在我们的java代码中,我们正在编写一个逻辑来从该表中获取数据并在UI中显示它。 The query is 查询是

Select id,name from employee

Query was that during debugging, we found that this jdbc call to fire the query and get the output is taking say 20 secs and we want to reduce this to say 5 seconds or to the optimal time. 查询是在调试期间,我们发现这个jdbc调用触发查询并获得输出,比如20秒,我们希望将此减少为5秒或最佳时间。 How can we you do that, or how will I tackle this problem? 我们怎么能这样做,或者我将如何解决这个问题?

As there is no where clause in the query, I didn't suggest to index the column. 由于查询中没有where clause ,我没有建议索引列。 As this logic is taking 20 secs every time, so, some other code getting a lock on this table is also out of question. 由于这个逻辑每次需要20秒,所以,其他一些代码锁定这个表也是不可能的。 I suggested that limiting the number of records fetched from the table should help but the interviewer didn't look convinced 我建议限制从表中提取的记录数量应该会有所帮助,但面试官看起来并不相信

Is there anything else we can do as a developer to optimize the call. 作为开发者,我们还能做些什么来优化呼叫。 I guess DBA might tune database setting to improve the performance of this query, but is there any other way 我想DBA可能会调整数据库设置以提高此查询的性能,但还有其他方法

OK, so this is an interview question, so both the problem and the solutions are hypothetical. 好的,所以这是一个面试问题,所以问题和解决方案都是假设的。 The interviewer is asking for possible optimizations and / or approaches. 面试官要求可能的优化和/或方法。 Here are some that are most likely to help: 以下是一些最有可能帮助的方法:

  • Modify the query to page the data rather than fetching the whole lot. 修改查询以分页数据而不是获取整批数据。 This looks applicable for the example query. 这看起来适用于示例查询。 Note that this is not just "limiting the number of rows selected from the table" ... which is probably why the interviewer looked doubtful when you said that! 请注意,这不仅仅是“限制从表中选择的行数” ......这可能就是为什么面试官在你这么说时看起来很怀疑!

  • If you do need to display the entire selected record set but in a reduced form (eg summed, averaged, sorted, collated etc), do the reduction in the query rather than by fetching the records and doing it in the client. 如果确实需要以缩小的形式显示整个选定的记录集(例如求和,平均,排序,整理等),请减少查询,而不是通过获取记录并在客户端中执行。

  • Tune the fetchSize() as suggested by Ivan. 按照Ivan的建议调整fetchSize()

Here are some other ideas that are less likely to help and / or will require extensive reworking. 以下是一些不太可能有帮助和/或需要大量改造的其他想法。

  • Look at the network configs. 看看网络配置。 For example you may be able to get better throughput by OS-level tuning TCP buffer, or optimizing physical or virtual network paths. 例如,您可以通过操作系统级调优TCP缓冲区或优化物理或虚拟网络路径来获得更好的吞吐量。
  • Run the query on the database server itself (to eliminate network overheads) 在数据库服务器本身上运行查询(以消除网络开销)
  • Use an in-memory table 使用内存表
  • Query a secondary database server; 查询辅助数据库服务器; eg a readonly snapshot or a slave 例如,只读快照或从属

You can try to increase fetchSize() for Statement/PreparedStatement to decrease number of network roundtrips between application server/desktop and database server. 您可以尝试增加Statement/PreparedStatement fetchSize() ,以减少应用程序服务器/桌面和数据库服务器之间的网络往返次数。 You can start several threads that will query some piece of data and then merge all data from several threads. 您可以启动多个线程来查询某些数据,然后合并来自多个线程的所有数据。

EDIT: doesn't apply to this situation because id and name are the only columns on this table, but still useful for other readers to note. 编辑:不适用于这种情况,因为idname是此表中唯一的列,但仍然有用于其他读者注意。


If you create an index covering both id and name , then the database can use that index to read the data faster since it wont even have to even read the table. 如果您创建一个涵盖idname的索引,那么数据库可以使用该索引更快地读取数据,因为它甚至不必读取表。

See this link for a more thorough explanation. 有关更详细的说明,请参阅此链接

if the index contains all the columns you're requesting it doesn't even need to look in the table. 如果索引包含您请求的所有列,则甚至不需要查看表。 That concept is known as index coverage. 这个概念被称为索引覆盖。

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

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