简体   繁体   English

Oracle SQL-避免全表扫描(在哪里)

[英]Oracle SQL - Avoid full table scan (where)

I have a statement (in simple we could see it like this: ) 我有一条声明(简单来说,我们可以这样看:)

Select * from VIEW where View.target='alpha'

The view is using multiple tables - table X, Y, Z for example, and the target will be in X. The view returns a dataset of 2 billion rows and with this query the database will load all of them and search where target equals 'alpha'. 该视图使用多个表-例如,表X,Y,Z,目标将在X中。该视图返回20亿行的数据集,通过该查询,数据库将加载所有表并搜索目标等于' α'。 Now is there any possibility to make this query faster? 现在有可能使此查询更快吗?

Maybe there is a possibility to make the view from which I load a little bit smaller (?) I think when I could make the 'target='alpha' statement within the view and the view would then be smaller this would really help... but I think when I do this statement within the view the problem would be the same because then the view would do the same (Am I right?) 也许有可能使加载的视图更小(?),我认为当我可以在视图内制作'target ='alpha'语句时,然后视图会更小,这确实很有帮助。但是,我认为当我在视图中执行此语句时,问题将是相同的,因为然后视图将执行相同的操作(对吗?)

At the end it would be better when I could have the view as it is and do the work within the new statement but if someone would have an idea that would work when I change the view this could be done also. 最后,最好是保留原样的视图并在新语句中进行工作,但是如果有人在我更改视图时有可行的想法,也可以这样做。

Thank you! 谢谢!

A view is a stored SQL statement, and to make a view faster you will have to make the stored SQL statement faster. 视图是存储的SQL语句,要使视图更快,您必须使存储的SQL语句更快。

The SQL statement depends on tables and to speed up queries against tables you use index . SQL语句取决于表,并可以加快对使用index的表的查询速度。 The benefits with index are plenty, read this great answer to a SO question here . 使用索引的好处很多,请在此处阅读SO问题的绝佳答案。 It explains what index is, how it works and why its needed. 它解释了什么是索引,如何工作以及为什么需要索引。

So to prevent a full table scan you add indexes to make the query faster. 因此,为防止全表扫描,您可以添加索引以加快查询速度。 In your case you need to identify the columns in the SQL statement that make a good fit for an index. 在您的情况下,您需要确定SQL语句中最适合索引的列。 Usually columns that are commonly used in WHERE, ORDER BY, JOIN and GROUP BY clauses make a good fit to be included in an index. 通常,WHERE,ORDER BY,JOIN和GROUP BY子句中常用的列非常适合包含在索引中。 In your case, start looking at the table where the target column exists. 在您的情况下,开始查看目标列所在的表。 Start by adding an index there and then continue with its relations to other tables in the query. 首先在此处添加索引,然后继续其与查询中其他表的关系。 This will eventually result in a faster response time when using your view. 使用视图时,这最终将导致更快的响应时间。

To create index on your Oracle table. 在您的Oracle表上创建索引。 Read the Oracle docs here 此处阅读Oracle文档

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

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