dreamontheway的技术之路

向着光明,我要一步一步往上爬。

导航

SQL 查询优化

1 有两个表,A表800多万,B表900多万,这两个表要做关联查询,没有GROUP BY等操作,只是普通的查询,如何提高速度?
[1]把能限制范围最小的条件放在最前面,此条件对应的列加一下索引
[2]尽量把Where后面给出的条件所要用的字段建立索引,
避免使用like, 避免在where中使用is null或者is not null,
另外,先要限制从两张表取的数据,最后才写两表关联的字段(两表连接的条件)
[3]索引是提升速度的关键。可以考虑分成几个查询,每个查询对应一个最佳的索引
[4]强制索引;少用IN,用EXISTS;可以的话,多建临时表;
[5]技术上提高查询办法:建立合适的索引,避免用影响查询速度的条件,比如in、%等,如果可以作partition的,就作partition,尽量分段查询。
两个大表关联查询,属于业务问题:数据量巨大,本身就是很费时间的事。像移动、电信,基本上不给打印1年的帐单,就是避免数度太慢影响业务。
[6]关键看你做什么样的关联操作,where条件很重要(是否用了in/not in,like等条件),还有是否用了union(因为union all 的执行要比union快很多)
[7]关键字建立索引
ID采用LONG型而不采用varchar,对于海量数据的查询可以提速很多
其它的,有待楼下进一步提供
[8]表空间分配合理,回滚段空间分配250M左右
[9]建立索引之后 分批查询
[10]优化查询条件,应该是比较实际的办法,将返回的结果缩小在最小的范围内的查询放到前面,依次
[11]应先进行表空间的优化,回滚段的优化,索引优化,再进行查询语句的优化
如果还是不行可试试建个视图,建立视图快照
2 Transitive predicate generation
如果想查出某一段时间的有效的单据中(或者某些产品)的明细资料,一般的做法:
     select 所需字段
      from detail_tab a,main_tab b
      where a.prod_id = b.prod_id
       and a.yyyymmdd = b.yyyymmdd
       and a.paper_no = b.paper_no
       and a.io = b.io
       and b.yyyymmdd = 指定日期
       and a.io = 进仓
       and b.code = 有效的;
  这个查询的速度会因为这两个表过于巨大而非常的缓慢,    
  现在有一个解决办法,就是给两个表都加上条件,即使是两个表关联的字段也加:
     select 所需字段
      from detail_tab a,main_tab b
      where a.prod_id = b.prod_id
       and a.yyyymmdd = b.yyyymmdd
       and a.paper_no = b.paper_no
       and a.io = b.io
       and b.yyyymmdd = 指定日期
       and a.yyyymmdd = 指定日期
       and a.io = 进仓
       and b.io = 进仓
       and b.code = 有效的;
       
  这样的速度要比前一种快了至少10倍以上。
人工调优,特别对于索引,条件顺序。
3
http://www.componentace.com/help/absdb_manual/increasesqlperformance.htm
Absolute Database search and filter optimizations are based on the use of available indexes.
Always create unique (primary) indexes when possible, as the unique index allow much better optimization then non-unique index.
To improve peformance of a LIKE condition, you can create an appropriate index, but note that this index can improve performance for patterns like 'str%' only。
Available indexes for JOIN conditions。To improve a JOIN query, please check that each field from JOIN conditions has an index.
Rewriting query with OR conditions as a UNION
Available indexes for ORDER BY clause If you want to speed up a "live" SELECT from a single table with ORDER BY clause, you can create a compound index for ORDER BY fields.
Available indexes for GROUP BY clause。To get a better performance for SELECT from a single table with GROUP BY clause, you can create a compound index for GROUP BY fields.
Select from in-memory tables。Your query perofrmance could be increased also if you will move all data from disk tables to in-memory tables and you will perform a query using in-memory copies of the disk tables
4
The fastest way of inserting and updating
The fastest way of batch inserting / updating / deleting is a buffered transaction.
We recommend to call TABSDatabase.StartTransaction before bulk inserts and
TABSDatabase.Commit(False) after the end of the batch operation. The use of transaction can significantly increase performance of the batch operation
How to speed up an UPDATE query
If you are using several subqueries in an UPDATE query, you could try to transform your query like pair values, not separated.
5
Common subexpression elimination
Subquery “flattening”
Complex/simple view merging
Predicate pushdown and pullup
OR-expansion 等等。这些参考http://www.oracle.com/technology/products/bi/db/10g/pdf/twp_general_query_optimization_10gr2_0605.pdf
总之查询首先考虑查询自身的优化,将记录数尽可能减少,即where条件要尽可能充分(特别注意可推导和重复的区别),投影优先于联接,时刻以减少联接数为分析的主要原则。然后是对索引的分析。至于量化分析,则是对多种策略的评价的直接方法。
主要摘自
http://www.componentace.com/help/absdb_manual/improvingoverallperformance.htm前后网页
http://www.oracle.com/
http://topic.csdn.net/t/20050514/21/4007292.html
阅读全文
类别:sql ado 存储过程 查看评论

posted on 2010-08-19 15:16  dreamontheway  阅读(241)  评论(0编辑  收藏  举报