SQL,执行速度比哪里更快?

时间:2016-06-14 14:02:23

标签: sql select where

我是SQL的新手,我正在寻找一种更快的方式来执行一个非常简单的语句。表中有多个值应匹配以返回行。有没有更快的方法来进行?该表是我没有管理员权限进行任何更改的视图。

select * from TABLE.View_xy 
where location=1 
and b=2
and c=3
and d=4
and e=5
and f=6
and manDate between (TIMESTAMP '2016-06-01 00:00:00') and (TIMESTAMP '2016-06-02 23:59:59') 
and g=7

1 个答案:

答案 0 :(得分:1)

好吧,如果它是一个视图并且你无法调整它,那么你可以用这个查询做更多的事情来提高效率(每个想法只是基本的过滤条件)。

我可以建议的是:

创建包含视图数据的临时表,索引此表,然后在表上运行select。您可以根据需要多次执行此操作。

CREATE TABLE tmp_for_select AS
SELECT * FROM TABLE.View_xy;

CREATE INDEX IND_NAME ON tmp_for_select(location,b,c,d,e,f,g,manDate) ;

select * from tmp_for_select
where location=1 and b=2 and c=3
  and d=4 and e=5 and f=6
  and manDate between (TIMESTAMP '2016-06-01 00:00:00') and (TIMESTAMP '2016-06-02 23:59:59') 
  and g=7;

DROP TABLE tmp_for_select;