﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Java进行时-文章分类-数据库</title><link>http://www.blogjava.net/davma/category/36833.html</link><description>程序人生</description><language>zh-cn</language><lastBuildDate>Fri, 19 Dec 2008 19:58:09 GMT</lastBuildDate><pubDate>Fri, 19 Dec 2008 19:58:09 GMT</pubDate><ttl>60</ttl><item><title>关于SQL调优的一些途径</title><link>http://www.blogjava.net/davma/articles/247373.html</link><dc:creator>davma</dc:creator><author>davma</author><pubDate>Fri, 19 Dec 2008 09:26:00 GMT</pubDate><guid>http://www.blogjava.net/davma/articles/247373.html</guid><wfw:comment>http://www.blogjava.net/davma/comments/247373.html</wfw:comment><comments>http://www.blogjava.net/davma/articles/247373.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/davma/comments/commentRss/247373.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/davma/services/trackbacks/247373.html</trackback:ping><description><![CDATA[<span style="color: red;">1 有两个表，A表800多万，B表900多万，这两个表要做关联查询，没有GROUP BY等操作，只是普通的查询，如何提高速度？</span><br />
[1]把能限制范围最小的条件放在最前面,此条件对应的列加一下索引<br />
[2]尽量把Where后面给出的条件所要用的字段建立索引,<br />
避免使用like, 避免在where中使用is null或者is not null,<br />
另外,先要限制从两张表取的数据,最后才写两表关联的字段(两表连接的条件)<br />
[3]索引是提升速度的关键。可以考虑分成几个查询，每个查询对应一个最佳的索引<br />
[4]强制索引；少用IN，用EXISTS；可以的话，多建临时表；<br />
[5]技术上提高查询办法：建立合适的索引，避免用影响查询速度的条件,比如in、%等，如果可以作partition的，就作partition，尽量分段查询。<br />
两个大表关联查询，属于业务问题：数据量巨大，本身就是很费时间的事。像移动、电信，基本上不给打印1年的帐单，就是避免数度太慢影响业务。<br />
[6]关键看你做什么样的关联操作，where条件很重要（是否用了in/not in,like等条件），还有是否用了union（因为union all 的执行要比union快很多）<br />
[7]关键字建立索引<br />
ID采用LONG型而不采用varchar,对于海量数据的查询可以提速很多<br />
其它的,有待楼下进一步提供<br />
[8]表空间分配合理,回滚段空间分配250M左右<br />
[9]建立索引之后 分批查询<br />
[10]优化查询条件,应该是比较实际的办法,将返回的结果缩小在最小的范围内的查询放到前面,依次<br />
[11]应先进行表空间的优化，回滚段的优化，索引优化，再进行查询语句的优化<br />
如果还是不行可试试建个视图，建立视图快照<br />
<span style="color: red;">2 Transitive predicate generation</span><br />
如果想查出某一段时间的有效的单据中（或者某些产品）的明细资料，一般的做法：<br />
select 所需字段<br />
from detail_tab a,main_tab b<br />
where a.prod_id = b.prod_id<br />
and a.yyyymmdd = b.yyyymmdd<br />
and a.paper_no = b.paper_no<br />
and a.io = b.io<br />
and b.yyyymmdd = 指定日期<br />
and a.io = 进仓<br />
and b.code = 有效的;<br />
这个查询的速度会因为这两个表过于巨大而非常的缓慢， 　　　<br />
现在有一个解决办法，就是给两个表都加上条件，即使是两个表关联的字段也加：<br />
select 所需字段<br />
from detail_tab a,main_tab b<br />
where a.prod_id = b.prod_id<br />
and a.yyyymmdd = b.yyyymmdd<br />
and a.paper_no = b.paper_no<br />
and a.io = b.io<br />
and b.yyyymmdd = 指定日期<br />
and a.yyyymmdd = 指定日期<br />
and a.io = 进仓<br />
and b.io = 进仓<br />
and b.code = 有效的;<br />
<br />
这样的速度要比前一种快了至少10倍以上。<br />
人工调优，特别对于索引，条件顺序。<br />
<span style="color: red;">3</span><br />
http://www.componentace.com/help/absdb_manual/increasesqlperformance.htm<br />
Absolute Database search and filter optimizations are based on the use of available indexes.<br />
&nbsp;<br />
Always create unique (primary) indexes when possible, as the unique index allow much better optimization then non-unique index.<br />
&nbsp;<br />
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。<br />
&nbsp;<br />
Available indexes for JOIN conditions。To improve a JOIN query, please check that each field from JOIN conditions has an index.<br />
&nbsp;<br />
Rewriting query with OR conditions as a UNION<br />
&nbsp;<br />
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.<br />
&nbsp;<br />
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.<br />
&nbsp;<br />
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<br />
&nbsp;<br />
<span style="color: red;">4</span><br />
The fastest way of inserting and updating<br />
The fastest way of batch inserting / updating / deleting is a buffered transaction.<br />
We recommend to call TABSDatabase.StartTransaction before bulk inserts and<br />
TABSDatabase.Commit(False) after the end of the batch operation. The use of transaction can significantly increase performance of the batch operation<br />
&nbsp;<br />
How to speed up an UPDATE query<br />
If you are using several subqueries in an UPDATE query, you could try to transform your query like pair values, not separated.<br />
&nbsp;<br />
<span style="color: red;">5</span><br />
Common subexpression elimination<br />
&nbsp;Subquery &#8220;flattening&#8221;<br />
&nbsp;Complex/simple view merging<br />
&nbsp;Predicate pushdown and pullup<br />
OR-expansion 等等。这些参考http://www.oracle.com/technology/products/bi/db/10g/pdf/twp_general_query_optimization_10gr2_0605.pdf<br />
总之查询首先考虑查询自身的优化，将记录数尽可能减少，即where条件要尽可能充分（特别注意可推导和重复的区别），投影优先于联接，时刻以减少联接数为分析的主要原则。然后是对索引的分析。至于量化分析，则是对多种策略的评价的直接方法。
<img src ="http://www.blogjava.net/davma/aggbug/247373.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/davma/" target="_blank">davma</a> 2008-12-19 17:26 <a href="http://www.blogjava.net/davma/articles/247373.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>