简体   繁体   English

SELECT * FROM 的小数据库表 (Oracle) 的执行时间仍然很长

[英]Small DB table (Oracle) still huge execution time for SELECT * FROM

I have a quite small table in Oracle 12c .我在Oracle 12c有一个很小的表。 It has around 45K records and size is 12MB (after recent gather stats).它有大约 45K 条记录,大小为 12MB(在最近收集统计数据之后)。 But it takes 30 sec to 1 min 30 secs to run但是运行需要 30 秒到 1 分 30 秒

SELECT * FROM table_name;

Additionally if I run另外如果我跑

SELECT * FROM TABLE WHERE ID = 123

(where id is an indexed column) it too takes around 45 secs. (其中 id 是索引列)它也需要大约 45 秒。

What could be wrong?可能有什么问题?

UPDATE: Explain plan as requested.更新:按要求解释计划。

SELECT * FROM {table_name}

SELECT STATEMENT  ALL_ROWSCost: 410  Bytes: 14,733,600  Cardinality: 43,850      
    1 TABLE ACCESS FULL TABLE {table_name} Cost: 410  Bytes: 14,733,600  Cardinality: 43,850  


SELECT * FROM {table_name} WHERE id = 123

SELECT STATEMENT  ALL_ROWSCost: 2  Bytes: 672  Cardinality: 2          
    2 TABLE ACCESS BY INDEX ROWID BATCHED TABLE {table_name} Cost: 2  Bytes: 672  Cardinality: 2      
        1 INDEX RANGE SCAN INDEX {index_name} Cost: 1  Cardinality: 2 

Sorry for hiding the object name to comply with organization policy抱歉隐藏对象名称以符合组织政策

After liaising with DBA, we found above query was mostly waiting on library cache lock and library cache pin wait events.联系DBA后,我们发现上面的查询主要是在等待library cache lock和library cache pin等待事件。 The table had more than one hundred thousand views (with dynamic names like VW_TABLE_12345, VW_TABLE_12346 etc.) created on it as dependent objects which was apparently a very bad design.该表有超过 10 万个视图(具有 VW_TABLE_12345、VW_TABLE_12346 等动态名称)作为依赖对象在其上创建,这显然是一个非常糟糕的设计。 When we changed code and cleared all the views, the table was as fast as it should be当我们更改代码并清除所有视图时,该表已达到应有的速度

Multiple things can be wrong but two come to mind.很多事情可能是错误的,但有两个是我想到的。

First is that your query with WHERE ID = 123 is returning a significant number of rows.首先是WHERE ID = 123查询返回了大量行。 Beyond a certain threshold, the database will decide that a full table scan is more efficient than using the index.超过某个阈值,数据库将决定全表扫描比使用索引更有效。 This is related to the idea of cardinality of the values in the index.这与索引中值的基数的想法有关。

Second, is that ID is not a number.其次, ID不是数字。 If it is a string, then the column is being converted to a number and that could preclude the use of an index.如果它是一个字符串,那么该列将被转换为一个数字,这可能会妨碍使用索引。

There are other possibilities, but these seem like the most likely.还有其他可能性,但这些似乎是最有可能的。

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

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