<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DBAlife &#187; SQL</title>
	<atom:link href="http://www.dbalife.com/archives/tag/sql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.dbalife.com</link>
	<description>星光照耀前方</description>
	<lastBuildDate>Wed, 14 Jul 2010 08:13:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>用SQL处理多行汇聚问题</title>
		<link>http://www.dbalife.com/archives/379.html</link>
		<comments>http://www.dbalife.com/archives/379.html#comments</comments>
		<pubDate>Wed, 03 Feb 2010 04:50:12 +0000</pubDate>
		<dc:creator>skywalker</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Program]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[多行]]></category>
		<category><![CDATA[汇聚]]></category>

		<guid isPermaLink="false">http://www.dbalife.com/?p=379</guid>
		<description><![CDATA[表结构如下：
NAME                            Null          Type
------------------------  ---------  -----
N_SEC_CODE                NOT NULL  CHAR(6)
C_RESEARCHER_CODE  NOT NULL  VARCHAR2(20)
此表保存了“股票”与“研究员”的对应关系数据，一般而言，对于同一只股票而言，可能有多个研究员对其进行跟踪研究。所以目前遇到一个要求，需查询出每只股票和对应的研究员（研究员代码间，使用逗号分隔）。
例如有如下数据：
000297                                    chenpeng      [...]


Related posts:<ol><li><a href='http://www.dbalife.com/archives/41.html' rel='bookmark' title='Permanent Link: sqlplus下如何看执行计划'>sqlplus下如何看执行计划</a></li>
<li><a href='http://www.dbalife.com/archives/39.html' rel='bookmark' title='Permanent Link: Statistics Lock导致的IMP问题'>Statistics Lock导致的IMP问题</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>表结构如下：<br />
NAME                            Null          Type<br />
------------------------  ---------  -----<br />
N_SEC_CODE                NOT NULL  CHAR(6)<br />
C_RESEARCHER_CODE  NOT NULL  VARCHAR2(20)</p>
<p>此表保存了“股票”与“研究员”的对应关系数据，一般而言，对于同一只股票而言，可能有多个研究员对其进行跟踪研究。所以目前遇到一个要求，需查询出每只股票和对应的研究员（研究员代码间，使用逗号分隔）。</p>
<p>例如有如下数据：<br />
000297                                    chenpeng           <br />
000297                                    liusu             <br />
合并处理后需显示为：<br />
000297                                    chenpeng,liusu</p>
<p>多行数据汇总聚一行，只需要把SQL中“n_sec_code”换为你的用来汇总的列，“c_researcher_code”替换为需合并文本的列，“m_researcher_stock_rel”替换为你的表名</p>
<p> SELECT n_sec_code,<br />
  TRANSLATE (LTRIM (text, '/'), '*/', '*,') researcherList<br />
   FROM<br />
  (SELECT ROW_NUMBER () OVER (PARTITION BY n_sec_code ORDER BY n_sec_code, lvl DESC) rn,<br />
    n_sec_code                                                                         ,<br />
    text<br />
     FROM<br />
    (SELECT n_sec_code,<br />
      LEVEL lvl       ,<br />
      SYS_CONNECT_BY_PATH (c_researcher_code,'/') text<br />
       FROM<br />
      (SELECT n_sec_code                      ,<br />
        c_researcher_code AS c_researcher_code,<br />
        ROW_NUMBER () OVER (PARTITION BY n_sec_code ORDER BY n_sec_code,c_researcher_code) x<br />
         FROM m_researcher_stock_rel<br />
     ORDER BY n_sec_code,<br />
        c_researcher_code<br />
      ) a CONNECT BY n_sec_code = PRIOR n_sec_code<br />
    AND x - 1                   = PRIOR x<br />
    )<br />
  )<br />
  WHERE rn = 1<br />
ORDER BY n_sec_code;</p>


<p>Related posts:<ol><li><a href='http://www.dbalife.com/archives/41.html' rel='bookmark' title='Permanent Link: sqlplus下如何看执行计划'>sqlplus下如何看执行计划</a></li>
<li><a href='http://www.dbalife.com/archives/39.html' rel='bookmark' title='Permanent Link: Statistics Lock导致的IMP问题'>Statistics Lock导致的IMP问题</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbalife.com/archives/379.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>sqlplus增强设置</title>
		<link>http://www.dbalife.com/archives/323.html</link>
		<comments>http://www.dbalife.com/archives/323.html#comments</comments>
		<pubDate>Thu, 24 Sep 2009 04:11:05 +0000</pubDate>
		<dc:creator>skywalker</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[prompt]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sqlplus]]></category>
		<category><![CDATA[vi]]></category>

		<guid isPermaLink="false">http://www.dbalife.com/?p=323</guid>
		<description><![CDATA[更强的编辑器
sqlplus可以与vi整合，在sqlplus中调用vi编辑器。方法是在sqlplus中执行define_editor='vi'，或将define_editor='vi'添加到/product/10.2/sqlplus/admin/glogin.sql文件中。
之后再登录sqlplus，可以输入edit命令编辑上一次执行的sql。
更人性化的提示符
在glogin.sql文件中添加：
set termout off
default gname=idle
column global_name new_value gname
SELECT lower(USER) &#124;&#124; '@' &#124;&#124;upper(instance_name)&#124;&#124;'('&#124;&#124;nvl(UTL_INADDR.GET_HOST_ADDRESS, SYS_CONTEXT('userenv', 'ip_address'))&#124;&#124;')' GLOBAL_NAME FROM v$instance;
set sqlprompt '&#38;gname&#62; '
set termout on
这样每次登录SQLPLUS提示符会自动更改为“当前用户名@实例名(IP地址)&#62; ”的形式。
10g以下版本在登录后切换用户的情况下，提示符不能自动更新。


No related posts.


No related posts.]]></description>
			<content:encoded><![CDATA[<p><strong>更强的编辑器</strong></p>
<p>sqlplus可以与vi整合，在sqlplus中调用vi编辑器。方法是在sqlplus中执行define_editor='vi'，或将define_editor='vi'添加到/product/10.2/sqlplus/admin/glogin.sql文件中。</p>
<p>之后再登录sqlplus，可以输入edit命令编辑上一次执行的sql。</p>
<p><strong>更人性化的提示符</strong></p>
<p>在glogin.sql文件中添加：</p>
<blockquote><p><span style="COLOR: green">set</span><span style="COLOR: gray"> </span><span style="COLOR: blue">termout</span><span style="COLOR: gray"> </span><span style="COLOR: green">off</span><span style="COLOR: gray"><br />
</span><span style="COLOR: green">default</span><span style="COLOR: gray"> </span><span style="COLOR: blue">gname</span><span style="COLOR: gray">=</span><span style="COLOR: blue">idle</span><span style="COLOR: gray"><br />
</span><span style="COLOR: green">column</span><span style="COLOR: gray"> </span><span style="COLOR: blue">global_name</span><span style="COLOR: gray"> </span><span style="COLOR: blue">new_value</span><span style="COLOR: gray"> </span><span style="COLOR: blue">gname</span><span style="COLOR: gray"><br />
</span><span style="COLOR: green">SELECT</span><span style="COLOR: gray"> </span><span style="COLOR: #00008b">lower</span><span style="COLOR: olive">(</span><span style="COLOR: green">USER</span><span style="COLOR: olive">)</span><span style="COLOR: gray"> || </span><span style="COLOR: #8b0000">'</span><span style="COLOR: red">@</span><span style="COLOR: #8b0000">'</span><span style="COLOR: gray"> ||</span><span style="COLOR: #00008b">upper</span><span style="COLOR: olive">(</span><span style="COLOR: blue">instance_name</span><span style="COLOR: olive">)</span><span style="COLOR: gray">||</span><span style="COLOR: #8b0000">'</span><span style="COLOR: red">(</span><span style="COLOR: #8b0000">'</span><span style="COLOR: gray">||</span><span style="COLOR: blue">nvl</span><span style="COLOR: olive">(</span><span style="COLOR: blue">UTL_INADDR</span><span style="COLOR: gray">.</span><span style="COLOR: blue">GET_HOST_ADDRESS</span><span style="COLOR: gray">, </span><span style="COLOR: blue">SYS_CONTEXT</span><span style="COLOR: olive">(</span><span style="COLOR: #8b0000">'</span><span style="COLOR: red">userenv</span><span style="COLOR: #8b0000">'</span><span style="COLOR: gray">, </span><span style="COLOR: #8b0000">'</span><span style="COLOR: red">ip_address</span><span style="COLOR: #8b0000">'</span><span style="COLOR: olive">))</span><span style="COLOR: gray">||</span><span style="COLOR: #8b0000">'</span><span style="COLOR: red">)</span><span style="COLOR: #8b0000">'</span><span style="COLOR: gray"> </span><span style="COLOR: blue">GLOBAL_NAME</span><span style="COLOR: gray"> </span><span style="COLOR: green">FROM</span><span style="COLOR: gray"> </span><span style="COLOR: blue">v</span><span style="COLOR: gray">$</span><span style="COLOR: #00008b">instance</span><span style="COLOR: gray">;<br />
</span><span style="COLOR: green">set</span><span style="COLOR: gray"> </span><span style="COLOR: blue">sqlprompt</span><span style="COLOR: gray"> </span><span style="COLOR: #8b0000">'</span><span style="COLOR: red">&amp;gname&gt; </span><span style="COLOR: #8b0000">'</span><span style="COLOR: gray"><br />
</span><span style="COLOR: green">set</span><span style="COLOR: gray"> </span><span style="COLOR: blue">termout</span><span style="COLOR: gray"> </span><span style="COLOR: green">on</span></p></blockquote>
<p>这样每次登录SQLPLUS提示符会自动更改为“当前用户名@实例名(IP地址)&gt; ”的形式。</p>
<p>10g以下版本在登录后切换用户的情况下，提示符不能自动更新。</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.dbalife.com/archives/323.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>undo相关的sql</title>
		<link>http://www.dbalife.com/archives/278.html</link>
		<comments>http://www.dbalife.com/archives/278.html#comments</comments>
		<pubDate>Thu, 02 Jul 2009 03:36:41 +0000</pubDate>
		<dc:creator>skywalker</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[undo]]></category>

		<guid isPermaLink="false">http://www.dbalife.com/?p=278</guid>
		<description><![CDATA[查UNDO统计信息语句：
SELECT TO_CHAR(BEGIN_TIME,'HH24:MI:SS') BEGIN_TIME,
TO_CHAR(END_TIME,'HH24:MI:SS') END_TIME,
UNDOBLKS
FROM V$UNDOSTAT;
-------------------------------------------结果--------------------------------------------------------------
begin_time end_time undoblocks
9:48:30        9:53:07        76293
9:38:30        9:48:30        143670
9:28:30        9:38:30        130921
9:18:30        9:28:30        122500
9:08:30        9:18:30        149030
8:58:30        9:08:30        144586
8:48:30  [...]


Related posts:<ol><li><a href='http://www.dbalife.com/archives/178.html' rel='bookmark' title='Permanent Link: 表空间使用状况查询'>表空间使用状况查询</a></li>
<li><a href='http://www.dbalife.com/archives/47.html' rel='bookmark' title='Permanent Link: 用sql截取字符串'>用sql截取字符串</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>查UNDO统计信息语句：</strong><br />
SELECT TO_CHAR(BEGIN_TIME,'HH24:MI:SS') BEGIN_TIME,<br />
TO_CHAR(END_TIME,'HH24:MI:SS') END_TIME,<br />
UNDOBLKS<br />
FROM V$UNDOSTAT;<br />
-------------------------------------------结果--------------------------------------------------------------<br />
begin_time end_time undoblocks<br />
9:48:30        9:53:07        76293<br />
9:38:30        9:48:30        143670<br />
9:28:30        9:38:30        130921<br />
9:18:30        9:28:30        122500<br />
9:08:30        9:18:30        149030<br />
8:58:30        9:08:30        144586<br />
8:48:30        8:58:30        108095<br />
8:38:30        8:48:30        63533<br />
8:28:30        8:38:30        72819<br />
8:18:30        8:28:30        23321<br />
8:08:30        8:18:30        2026<br />
7:58:30        8:08:30        15480<br />
7:48:30        7:58:30        91111<br />
7:38:30        7:48:30        15810<br />
7:28:30        7:38:30        56922<br />
7:18:30        7:28:30        30449<br />
7:08:30        7:18:30        6096<br />
6:58:30        7:08:30        15022<br />
6:48:30        6:58:30        91904<br />
6:38:30        6:48:30        38794<br />
6:28:30        6:38:30        111884<br />
----------------------------------------------------------------------------------------------------------<br />
<strong>查UNDO表空间的使用情况：</strong><br />
select<br />
  a.a1 表空间名,<br />
  substr(b.b2/1024/1024/1024,1,5) 表空间大小G,<br />
  substr(a.a2/1024/1024/1024,1,5) 剩余表空间G,<br />
  substr((b.b2-a.a2)/1024/1024/1024,1,5) 实际使用表空间G,<br />
  substr((b.b2-a.a2)/b.b2*100,1,5) 利用率,<br />
  c.c2 类型,<br />
  c.c3 区管理方式<br />
  from<br />
  (select tablespace_name a1,sum(Nvl(bytes,0)) a2 from dba_free_space group by tablespace_name) a,<br />
  (select tablespace_name b1,sum(bytes) b2 from dba_data_files group by tablespace_name) b,<br />
  (select tablespace_name c1,contents c2,extent_management c3 from dba_tablespaces) c<br />
  where a.a1=b.b1 and b.b1=c.c1<br />
  order by c.c2 desc<br />
-------------------------------------------------结果--------------------------------------------------------------------<br />
表空间名        表空间大小(G)        剩余(G)        使用        利用率        表空间类型        管理方式<br />
UNDOTBS1        17.24        1.124        16.12        93.48        UNDO        LOCAL<br />
----------------------------------------------------------------------------------------------------------------------------<br />
<strong>建议UNDO峰值：</strong><br />
select ur undo_retention,dbs db_block_size, ((ur * (ups * dbs)) + (dbs * 24)) / 1024 / 1024 as "M_bytes"<br />
from (select value as ur from v$parameter where name = 'undo_retention'),<br />
       (select (undoblks / ((end_time - begin_time) * 86400)) ups from v$undostat where undoblks in (select max(undoblks) from  v$undostat ) ),<br />
       (select value as dbs from v$parameter where name = 'db_block_size');<br />
----------------------------------------------结果------------------------------------------------------------------<br />
undo_retention        db_block_size        M_bytes<br />
10800                              8192                   33,376.96875</p>


<p>Related posts:<ol><li><a href='http://www.dbalife.com/archives/178.html' rel='bookmark' title='Permanent Link: 表空间使用状况查询'>表空间使用状况查询</a></li>
<li><a href='http://www.dbalife.com/archives/47.html' rel='bookmark' title='Permanent Link: 用sql截取字符串'>用sql截取字符串</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbalife.com/archives/278.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>表空间使用状况查询</title>
		<link>http://www.dbalife.com/archives/178.html</link>
		<comments>http://www.dbalife.com/archives/178.html#comments</comments>
		<pubDate>Thu, 25 Sep 2008 01:16:25 +0000</pubDate>
		<dc:creator>skywalker</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[表空间]]></category>

		<guid isPermaLink="false">http://www.dbalife.com/?p=178</guid>
		<description><![CDATA[SELECT D.TABLESPACE_NAME                                ,
SPACE "SUM_SPACE(M)"                     [...]


Related posts:<ol><li><a href='http://www.dbalife.com/archives/278.html' rel='bookmark' title='Permanent Link: undo相关的sql'>undo相关的sql</a></li>
<li><a href='http://www.dbalife.com/archives/62.html' rel='bookmark' title='Permanent Link: 查询一天归档文件的大小'>查询一天归档文件的大小</a></li>
<li><a href='http://www.dbalife.com/archives/144.html' rel='bookmark' title='Permanent Link: 等待事件相关的SQL'>等待事件相关的SQL</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>SELECT D.TABLESPACE_NAME                                ,<br />
SPACE "SUM_SPACE(M)"                                   ,<br />
BLOCKS SUM_BLOCKS                                      ,<br />
SPACE   -NVL(FREE_SPACE,0) "USED_SPACE(M)"             ,<br />
ROUND((1-NVL(FREE_SPACE,0)/SPACE)*100,2) "USED_RATE(%)",<br />
FREE_SPACE "FREE_SPACE(M)"<br />
FROM<br />
(SELECT TABLESPACE_NAME                ,<br />
ROUND(SUM(BYTES)/(1024*1024),2) SPACE,<br />
SUM(BLOCKS) BLOCKS<br />
FROM DBA_DATA_FILES<br />
GROUP BY TABLESPACE_NAME<br />
) D                    ,<br />
(SELECT TABLESPACE_NAME,<br />
ROUND(SUM(BYTES)/(1024*1024),2) FREE_SPACE<br />
FROM DBA_FREE_SPACE<br />
GROUP BY TABLESPACE_NAME<br />
) F<br />
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)</p>
<p>UNION ALL --if have tempfile</p>
<p>SELECT D.TABLESPACE_NAME                            ,<br />
SPACE "SUM_SPACE(M)"                               ,<br />
BLOCKS SUM_BLOCKS                                  ,<br />
USED_SPACE "USED_SPACE(M)"                         ,<br />
ROUND(NVL(USED_SPACE,0)/SPACE*100,2) "USED_RATE(%)",<br />
NVL(FREE_SPACE,0) "FREE_SPACE(M)"<br />
FROM<br />
(SELECT TABLESPACE_NAME                ,<br />
ROUND(SUM(BYTES)/(1024*1024),2) SPACE,<br />
SUM(BLOCKS) BLOCKS<br />
FROM DBA_TEMP_FILES<br />
GROUP BY TABLESPACE_NAME<br />
) D                                              ,<br />
(SELECT TABLESPACE_NAME                          ,<br />
ROUND(SUM(BYTES_USED)/(1024*1024),2) USED_SPACE,<br />
ROUND(SUM(BYTES_FREE)/(1024*1024),2) FREE_SPACE<br />
FROM V$TEMP_SPACE_HEADER<br />
GROUP BY TABLESPACE_NAME<br />
) F<br />
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)</p>


<p>Related posts:<ol><li><a href='http://www.dbalife.com/archives/278.html' rel='bookmark' title='Permanent Link: undo相关的sql'>undo相关的sql</a></li>
<li><a href='http://www.dbalife.com/archives/62.html' rel='bookmark' title='Permanent Link: 查询一天归档文件的大小'>查询一天归档文件的大小</a></li>
<li><a href='http://www.dbalife.com/archives/144.html' rel='bookmark' title='Permanent Link: 等待事件相关的SQL'>等待事件相关的SQL</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbalife.com/archives/178.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>模糊查询like &#039;%XX%&#039; 的优化一例</title>
		<link>http://www.dbalife.com/archives/176.html</link>
		<comments>http://www.dbalife.com/archives/176.html#comments</comments>
		<pubDate>Fri, 19 Sep 2008 06:47:33 +0000</pubDate>
		<dc:creator>skywalker</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[like]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.dbalife.com/archives/%e6%a8%a1%e7%b3%8a%e6%9f%a5%e8%af%a2like-xx-%e7%9a%84%e4%bc%98%e5%8c%96%e4%b8%80%e4%be%8b/</guid>
		<description><![CDATA[sql中的like '%xx%'模糊查询无法走索引，影响执行速度。经测试itpub版主ifree的index_ffs+rowid方法比较有效，记录一下。
这里是示例：
 scott@ORCL&#62; CREATE INDEX SCOTT.i_dept_name
&#160;&#160;2&#160; &#160;ON SCOTT.DEPT(DNAME)
&#160;&#160;3&#160;&#160;;
Index created.
scott@ORCL&#62; Analyze Table SCOTT.DEPT Compute Statistics ;
Table analyzed.
scott@ORCL&#62; select * from scott.dept where
&#160;&#160;2&#160;&#160;rowid in (
&#160;&#160;3&#160;&#160;select /*+ index_ffs(a i_dept_dname) */
&#160;&#160;4&#160;&#160;rowid from scott.dept a where dname like '%A%')
&#160;&#160;5&#160;&#160;;

这个方法要求like查询出的记录不能太多，在我的应用中，这一方法使sql效率提高了近10倍。
Blogged with the Flock Browser

Tags: sql, 优化, turning, like, 模糊查询



Related posts:Oracle9i优化器介绍（上）
如何选择合适的DBA
如何获得廉价的metalink帐号



Related posts:<ol><li><a href='http://www.dbalife.com/archives/33.html' rel='bookmark' title='Permanent Link: Oracle9i优化器介绍（上）'>Oracle9i优化器介绍（上）</a></li>
<li><a href='http://www.dbalife.com/archives/132.html' rel='bookmark' title='Permanent Link: 如何选择合适的DBA'>如何选择合适的DBA</a></li>
<li><a href='http://www.dbalife.com/archives/186.html' rel='bookmark' title='Permanent Link: 如何获得廉价的metalink帐号'>如何获得廉价的metalink帐号</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>sql中的like '%xx%'模糊查询无法走索引，影响执行速度。经测试itpub版主ifree的index_ffs+rowid方法比较有效，记录一下。</p>
<p>这里是示例：</p>
<div style="margin-left: 40px;"> scott@ORCL&gt; CREATE INDEX SCOTT.i_dept_name<br />
&nbsp;&nbsp;2&nbsp; &nbsp;ON SCOTT.DEPT(DNAME)<br />
&nbsp;&nbsp;3&nbsp;&nbsp;;</p>
<p>Index created.</p>
<p>scott@ORCL&gt; Analyze Table SCOTT.DEPT Compute Statistics ;</p>
<p>Table analyzed.</p>
<p>scott@ORCL&gt; select * from scott.dept where<br />
&nbsp;&nbsp;2&nbsp;&nbsp;rowid in (<br />
&nbsp;&nbsp;3&nbsp;&nbsp;select /*+ index_ffs(a i_dept_dname) */<br />
&nbsp;&nbsp;4&nbsp;&nbsp;rowid from scott.dept a where dname like '%A%')<br />
&nbsp;&nbsp;5&nbsp;&nbsp;;</p>
</div>
<p>这个方法要求like查询出的记录不能太多，在我的应用中，这一方法使sql效率提高了近10倍。</p>
<div class="flockcredit" style="text-align: right; color: #CCC; font-size: x-small;">Blogged with the <a href="http://www.flock.com/blogged-with-flock" style="color: #999; font-weight: bold;" target="_new" title="Flock Browser">Flock Browser</a></div>
<p><!-- technorati tags begin -->
<p style="font-size:10px;text-align:right;">Tags: <a href="http://technorati.com/tag/sql" rel="tag">sql</a>, <a href="http://technorati.com/tag/%E4%BC%98%E5%8C%96" rel="tag">优化</a>, <a href="http://technorati.com/tag/turning" rel="tag">turning</a>, <a href="http://technorati.com/tag/like" rel="tag">like</a>, <a href="http://technorati.com/tag/%E6%A8%A1%E7%B3%8A%E6%9F%A5%E8%AF%A2" rel="tag">模糊查询</a></p>
<p><!-- technorati tags end --></p>


<p>Related posts:<ol><li><a href='http://www.dbalife.com/archives/33.html' rel='bookmark' title='Permanent Link: Oracle9i优化器介绍（上）'>Oracle9i优化器介绍（上）</a></li>
<li><a href='http://www.dbalife.com/archives/132.html' rel='bookmark' title='Permanent Link: 如何选择合适的DBA'>如何选择合适的DBA</a></li>
<li><a href='http://www.dbalife.com/archives/186.html' rel='bookmark' title='Permanent Link: 如何获得廉价的metalink帐号'>如何获得廉价的metalink帐号</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbalife.com/archives/176.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>等待事件相关的SQL</title>
		<link>http://www.dbalife.com/archives/144.html</link>
		<comments>http://www.dbalife.com/archives/144.html#comments</comments>
		<pubDate>Wed, 30 Jul 2008 05:16:36 +0000</pubDate>
		<dc:creator>skywalker</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[session_wait]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[等待事件]]></category>

		<guid isPermaLink="false">http://www.dbalife.com/?p=144</guid>
		<description><![CDATA[--求等待事件及其对应的latch
col event format a32
col name format a32
select sid,event,p1 as file_id, p2 as "block_id/latch", p3 as blocks,l.name
from v$session_wait sw,v$latch l
where event not like '%SQL%' and event not like '%rdbms%'
and event not like '%mon%' and sw.p2 = l.latch#(+);
--求等待事件及其热点对象
col owner format a18
col segment_name format a32
col segment_type format a32
select owner,segment_name,segment_type
from dba_extents
where file_id = &#38;file_id and &#38;block_id between block_id
and block_id [...]


Related posts:<ol><li><a href='http://www.dbalife.com/archives/178.html' rel='bookmark' title='Permanent Link: 表空间使用状况查询'>表空间使用状况查询</a></li>
<li><a href='http://www.dbalife.com/archives/278.html' rel='bookmark' title='Permanent Link: undo相关的sql'>undo相关的sql</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: x-small;">--求等待事件及其对应的latch</span></p>
<p><span style="font-size: x-small;">col event format a32<br />
col name format a32<br />
select sid,event,p1 as file_id, p2 as "block_id/latch", p3 as blocks,l.name<br />
from v$session_wait sw,v$latch l<br />
where event not like '%SQL%' and event not like '%rdbms%'<br />
and event not like '%mon%' and sw.p2 = l.latch#(+);</span><br />
<span style="font-size: x-small;">--求等待事件及其热点对象</span></p>
<p><span style="font-size: x-small;">col owner format a18<br />
col segment_name format a32<br />
col segment_type format a32<br />
select owner,segment_name,segment_type<br />
from dba_extents<br />
where file_id = &amp;file_id and &amp;block_id between block_id<br />
and block_id + &amp;blocks - 1;</span><br />
<span style="font-size: x-small;">--综合以上两条sql，同时显示latch及热点对象(速度较慢)</span></p>
<p><span style="font-size: x-small;">select sw.sid,event,l.name,de.segment_name<br />
from v$session_wait sw,v$latch l,dba_extents de<br />
where event not like '%SQL%' and event not like '%rdbms%'<br />
and event not like '%mon%' and sw.p2 = l.latch#(+) and sw.p1 = de.file_id(+) and p2 between de.block_id and de.block_id + de.blocks - 1;</span><br />
<span style="font-size: x-small;">--如果是非空闲等待事件，通过等待会话的sid可以求出该会话在执行的sql</span></p>
<p><span style="font-size: x-small;">select sql_text<br />
from v$sqltext_with_newlines st,v$session se<br />
where st.address=se.sql_address and st.hashvalue=se.sql_hash_value<br />
and se.sid =&amp;wait_sid order by piece;</span></p>


<p>Related posts:<ol><li><a href='http://www.dbalife.com/archives/178.html' rel='bookmark' title='Permanent Link: 表空间使用状况查询'>表空间使用状况查询</a></li>
<li><a href='http://www.dbalife.com/archives/278.html' rel='bookmark' title='Permanent Link: undo相关的sql'>undo相关的sql</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.dbalife.com/archives/144.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
