查询视图花费了太多时间 [英] querying view is taking too much time

查看:113
本文介绍了查询视图花费了太多时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个视图表,从视图中选择通常需要花费太多时间。例如:从view1中选择x,y,z需要花费太多时间来加载。这个是好的。



如果你查询:

 选择 x, y,z 来自 view1 其中 x  '  abc'



查询秒。



如果您查询:

 选择 x,y,z 来自 view1 其中 x  选择 '  abc'  from  table1 其中​​ y = '  1234'



查询秒数。



但如果查询:< pre lang =SQL> 选择 x,y,z 来自 view1 其中 x 选择 x 来自 table1 其中 y = ' 1234'



花费太多时间查询和这是我们想要解决的问题。



您可以这样认为:从table1中选择x,其中y =''1234''返回''abc' '有一行。



情景如上所述,您认为可能需要花费大量时间查询第三个查询的原因。我们已尝试加入,但它没有用。

解决方案

尝试使用桌面上的索引。



使用 JOIN 而不是 IN 应加快流程 - http://www.bennadel.com/blog/940-SQL-Optimization-Case-Study -JOIN-Clause-vs-IN-Clause.htm [ ^ ]。


这个SQL应该适合你。



 选择 x,y,z 来自 view1 
inner join table1 on ((view1.x = table1.x)(table1.y = ' 1234'))


试试这个...

1.

 选择 x,y,z 来自 view1  WITH 索引(Index_x))其中中的选择 x 来自 table1  WITH 索引(Index_y))其中 y = '  1234'





2.

 选择 x,y,z 来自 view1 
其中​​ 存在选择 * 来自 table1 其中 y = ' 1234' table1.x = view1.x)



快乐编码!

:)


We have a view table and selecting from view is normally taking too much time. for example: select x,y,z from view1 is taking too much time to load. This one is ok.

if you query:

select x,y,z from view1 where x in ('abc')


queries in seconds.

if you query:

select x,y,z from view1 where x in (select 'abc' from table1 where y='1234') 


queries in seconds.

but if you query:

select x,y,z from view1 where x in (select x from table1 where y='1234') 


is taking too much time to query and this is the problem we want to solve.

by the way you can think that : select x from table1 where y=''1234'' returns ''abc'' with one row.

The scenario is decsribed above , what do you think could be the reason to take so much time to query on the third query. We have tried joining but it didn''t work.

解决方案

Try using indexes on your table.

Using JOIN instead of IN should speed up the process - http://www.bennadel.com/blog/940-SQL-Optimization-Case-Study-JOIN-Clause-vs-IN-Clause.htm[^].


This SQL should work for you.

select x,y,z from view1 
inner join table1 on ((view1.x = table1.x) and (table1.y='1234'))


Try this...
1.

select x,y,z from view1 WITH (Index(Index_x)) where x in (select x from table1  WITH (Index(Index_y)) where y='1234')



2.

select x,y,z from view1
where exists (select * from table1 where y='1234' and table1.x=view1.x)


Happy Coding!
:)


这篇关于查询视图花费了太多时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆