﻿<?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-自然-文章分类-DataBase</title><link>http://www.blogjava.net/masen/category/22361.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 27 Feb 2016 05:00:16 GMT</lastBuildDate><pubDate>Sat, 27 Feb 2016 05:00:16 GMT</pubDate><ttl>60</ttl><item><title>DBCP配置(转)</title><link>http://www.blogjava.net/masen/articles/429456.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Sat, 27 Feb 2016 03:06:00 GMT</pubDate><guid>http://www.blogjava.net/masen/articles/429456.html</guid><wfw:comment>http://www.blogjava.net/masen/comments/429456.html</wfw:comment><comments>http://www.blogjava.net/masen/articles/429456.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/masen/comments/commentRss/429456.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/masen/services/trackbacks/429456.html</trackback:ping><description><![CDATA[<div style="margin-bottom: 15px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 12px; line-height: 12px;"><h3><br /></h3></div><div id="blog_content" style="font-size: 14px; line-height: 1.8em; font-family: Helvetica, Tahoma, Arial, sans-serif;"><div style="font-size: 14px;"><h1>DBCP连接池介绍</h1><p style="margin: 0px; padding: 0px;">-----------------------------</p><p style="margin: 0px; padding: 0px;">目前 DBCP 有两个版本分别是 1.3 和 1.4。</p><p style="margin: 0px; padding: 0px;">DBCP 1.3 版本需要运行于 JDK 1.4-1.5 ，支持 JDBC 3。</p><p style="margin: 0px; padding: 0px;">DBCP 1.4 版本需要运行于 JDK 1.6 ，支持 JDBC 4。</p><p style="margin: 0px; padding: 0px;">1.3和1.4基于同一套源代码，含有所有的bug修复和新特性。因此在选择DBCP版本的时候，要看你用的是什么JDK版本。</p><p style="margin: 0px; padding: 0px;">DBCP1.2版本性能一般，比c3p0差挺多。DBCP1.4和1.3，配合（依赖）commons pool 1.6的jar包,各方面功能、性能推进到新的高峰。相对1.2版本提高不少。超越(或相当)了c3p0.建议使用DBCP1.4或1.3 + &nbsp;commons pool 1.6</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">Tomcat7 中保留DBCP连接池，以兼容已有应用。并提供了新的Tomcat JDBC pool作为DBCP的可选替代。新出的Tomcat JDBC pool，据说比DBCP 1.4要好，未接触，也不在本文讨论范围内。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><h1>DBCP连接池配置参数讲解</h1><p style="margin: 0px; padding: 0px;">-----------------------------</p><p style="margin: 0px; padding: 0px;">一、Apache官方DBCP文档给出的配置示例：</p><p style="margin: 0px; padding: 0px;">可参见：http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html</p><p style="margin: 0px; padding: 0px;">&lt;Context&gt;</p><p style="margin: 0px; padding: 0px;">&nbsp; &lt;Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maxActive="100" maxIdle="30" maxWait="10000"</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url="jdbc:mysql://localhost:3306/javatest"/&gt;</p><p style="margin: 0px; padding: 0px;">&lt;/Context&gt;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;"><strong>tomcat JDBC连接池配置示例，自动检查连接的可用性，dbcp定时检测连接，dbcp自动重连的配置</strong></p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Xml代码&nbsp;&nbsp;<a title="收藏这段代码" style="color: #108ac6; text-decoration: underline;"><img src="http://elf8848.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: #006699; font-weight: bold;">&lt;</span><span style="color: #006699; font-weight: bold;">Resource</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">name</span>=<span style="color: blue;">"jdbc/TestDB"</span>&nbsp;&nbsp;JNDI数据源的name，查找时用：java:comp/env/jdbc/TestDB&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">type</span>=<span style="color: blue;">"javax.sql.DataSource"</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">factory</span>=<span style="color: blue;">"org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">driverClassName</span>=<span style="color: blue;">"com.mysql.jdbc.Driver"</span>&nbsp;JDBC驱动类&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">url</span>="jdbc:mysql://localhost:3306/test?&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">characterEncoding</span>=<span style="color: blue;">UTF</span>-8&amp;amp;<span style="color: red;">autoReconnectForPools</span>=<span style="color: blue;">true</span>&amp;amp;<span style="color: red;">rewriteBatchedStatements</span>=<span style="color: blue;">true</span>&amp;amp;<span style="color: red;">useCursorFetch</span>=<span style="color: blue;">true</span>&amp;amp;<span style="color: red;">defaultFetchSize</span>=<span style="color: blue;">20</span>"&nbsp;数据库URL地址&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">username</span>=<span style="color: blue;">"xxx"</span>&nbsp;访问数据库用户名&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">password</span>=<span style="color: blue;">"xxx"</span>&nbsp;访问数据库的密码&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">maxWait</span>=<span style="color: blue;">"3000"</span>&nbsp;从池中取连接的最大等待时间，单位ms.&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">initialSize</span>=<span style="color: blue;">"10"</span>&nbsp;&nbsp;初始化连接&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">maxIdle</span>=<span style="color: blue;">"60"</span>&nbsp;&nbsp;&nbsp;最大空闲连接&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">minIdle</span>=<span style="color: blue;">"10"</span>&nbsp;&nbsp;&nbsp;最小空闲连接&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">maxActive</span>=<span style="color: blue;">"80"</span>&nbsp;最大活动连接&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">validationQuery</span>&nbsp;=&nbsp;<span style="color: blue;">"SELECT&nbsp;1"</span>&nbsp;&nbsp;验证使用的SQL语句&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">testWhileIdle</span>&nbsp;=&nbsp;<span style="color: blue;">"true"</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">testOnBorrow</span>&nbsp;=&nbsp;<span style="color: blue;">"false"</span>&nbsp;&nbsp;&nbsp;借出连接时不要测试，否则很影响性能&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">timeBetweenEvictionRunsMillis</span>&nbsp;=&nbsp;<span style="color: blue;">"30000"</span>&nbsp;&nbsp;每30秒运行一次空闲连接回收器&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">minEvictableIdleTimeMillis</span>&nbsp;=&nbsp;<span style="color: blue;">"1800000"</span>&nbsp;&nbsp;池中的连接空闲30分钟后被回收&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">numTestsPerEvictionRun</span>=<span style="color: blue;">"10"</span>&nbsp;在每次空闲连接回收器线程(如果有)运行时检查的连接数量&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">removeAbandoned</span>=<span style="color: blue;">"true"</span>&nbsp;&nbsp;连接泄漏回收参数，当可用连接数少于3个时才执行&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: red;">removeAbandonedTimeout</span>=<span style="color: blue;">"180"</span>&nbsp;&nbsp;连接泄漏回收参数，180秒，泄露的连接可以被删除的超时值&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: #006699; font-weight: bold;">/&gt;</span>&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><h1>DBCP连接池的自我检测</h1><p style="margin: 0px; padding: 0px;">-----------------------------</p><p style="margin: 0px; padding: 0px;">默认配置的DBCP连接池，是不对池中的连接做测试的，有时连接已断开了，但DBCP连接池不知道，还以为连接是好的呢。</p><p style="margin: 0px; padding: 0px;">应用从池中取出这样的连接访问数据库一定会报错。这也是好多人不喜欢DBCP的原因。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">问题例一：</p><p style="margin: 0px; padding: 0px;">MySQL8小时问题，Mysql服务器默认连接的&#8220;wait_timeout&#8221;是8小时，也就是说一个connection空闲超过8个小时，Mysql将自动断开该 connection。</p><p style="margin: 0px; padding: 0px;">但是DBCP连接池并不知道连接已经断开了，如果程序正巧使用到这个已经断开的连接，程序就会报错误。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">问题例二：</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 以前还使用Sybase数据库，由于某种原因，数据库死了后重启、或断网后恢复。</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 等了约10分钟后，DBCP连接池中的连接还都是不能使用的（断开的），访问数据应用一直报错，最后只能重启Tomcat问题才解决 。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">解决方案：</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 方案1、定时对连接做测试，测试失败就关闭连接。</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 方案2、控制连接的空闲时间达到N分钟，就关闭连接，（然后可再新建连接）。</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 以上两个方案使用任意一个就可以解决以述两类问题。如果只使用方案2，建议 N &lt;= 5分钟。连接断开后最多5分钟后可恢复。</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 也可混合使用两个方案，建议 N = 30分钟。</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp;&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 下面就是DBCP连接池，同时使用了以上两个方案的配置配置</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; validationQuery = "SELECT 1" &nbsp;验证连接是否可用，使用的SQL语句</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; testWhileIdle = "true" &nbsp; &nbsp; &nbsp;指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; testOnBorrow = "false" &nbsp; 借出连接时不要测试，否则很影响性能</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; timeBetweenEvictionRunsMillis = "30000" &nbsp;每30秒运行一次空闲连接回收器</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; minEvictableIdleTimeMillis = "1800000" &nbsp;池中的连接空闲30分钟后被回收,默认值就是30分钟。</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; numTestsPerEvictionRun="3" 在每次空闲连接回收器线程(如果有)运行时检查的连接数量，默认值就是3.</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp;&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 解释：</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 配置timeBetweenEvictionRunsMillis = "30000"后，每30秒运行一次空闲连接回收器（独立线程）。并每次检查3个连接，如果连接空闲时间超过30分钟就销毁。销毁连接后，连接数量就少了，如果小于minIdle数量，就新建连接，维护数量不少于minIdle，过行了新老更替。</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; testWhileIdle = "true" 表示每30秒，取出3条连接，使用validationQuery = "SELECT 1" 中的SQL进行测试 ，测试不成功就销毁连接。销毁连接后，连接数量就少了，如果小于minIdle数量，就新建连接。</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; testOnBorrow = "false" 一定要配置，因为它的默认值是true。false表示每次从连接池中取出连接时，不需要执行validationQuery = "SELECT 1" 中的SQL进行测试。若配置为true,对性能有非常大的影响，性能会下降7-10倍。所在一定要配置为false.</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 每30秒，取出numTestsPerEvictionRun条连接（本例是3，也是默认值），发出"SELECT 1" SQL语句进行测试 ，测试过的连接不算是&#8220;被使用&#8221;了，还算是空闲的。连接空闲30分钟后会被销毁。</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp;&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><h1>DBCP连接池配置参数注意事项 &nbsp;</h1><p style="margin: 0px; padding: 0px;">-----------------------------</p><p style="margin: 0px; padding: 0px;">maxIdle值与maxActive值应配置的接近。</p><p style="margin: 0px; padding: 0px;">因为，当连接数超过maxIdle值后，刚刚使用完的连接（刚刚空闲下来）会立即被销毁。而不是我想要的空闲M秒后再销毁起一个缓冲作用。这一点DBCP做的可能与你想像的不一样。</p><p style="margin: 0px; padding: 0px;">若maxIdle与maxActive相差较大，在高负载的系统中会导致频繁的创建、销毁连接，连接数在maxIdle与maxActive间快速频繁波动，这不是我想要的。</p><p style="margin: 0px; padding: 0px;">高负载系统的maxIdle值可以设置为与maxActive相同或设置为-1(-1表示不限制)，让连接数量在minIdle与maxIdle间缓冲慢速波动。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">timeBetweenEvictionRunsMillis建议设置值</p><p style="margin: 0px; padding: 0px;">initialSize="5"，会在tomcat一启动时，创建5条连接，效果很理想。</p><p style="margin: 0px; padding: 0px;">但同时我们还配置了minIdle="10"，也就是说，最少要保持10条连接，那现在只有5条连接，哪什么时候再创建少的5条连接呢？</p><p style="margin: 0px; padding: 0px;">1、等业务压力上来了， DBCP就会创建新的连接。</p><p style="margin: 0px; padding: 0px;">2、配置timeBetweenEvictionRunsMillis=&#8220;时间&#8221;,DBCP会启用独立的工作线程定时检查，补上少的5条连接。销毁多余的连接也是同理。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><h1>连接销毁的逻辑</h1><p style="margin: 0px; padding: 0px;">------------------------------</p><p style="margin: 0px; padding: 0px;">DBCP的连接数会在 &nbsp;0 - minIdle - maxIdle - maxActive &nbsp;之间变化。变化的逻辑描述如下：</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">默认未配置initialSize(默认值是0)和timeBetweenEvictionRunsMillis参数时，刚启动tomcat时，连接数是0。当应用有一个并发访问数据库时DBCP创建一个连接。</p><p style="margin: 0px; padding: 0px;">目前连接数量还未达到minIdle，但DBCP也不自动创建新连接已使数量达到minIdle数量（没有一个独立的工作线程来检查和创建）。</p><p style="margin: 0px; padding: 0px;">随着应用并发访问数据库的增多，连接数也增多，但都与minIdle值无关，很快minIdle被超越，minIdle值一点用都没有。</p><p style="margin: 0px; padding: 0px;">直到连接的数量达到maxIdle值，这时的连接都是只增不减的。 再继续发展，连接数再增多并超过maxIdle时，使用完的连接（刚刚空闲下来的）会立即关闭，总体连接的数量稳定在maxIdle但不会超过maxIdle。</p><p style="margin: 0px; padding: 0px;">但活动连接（在使用中的连接）可能数量上瞬间超过maxIdle，但永远不会超过maxActive。</p><p style="margin: 0px; padding: 0px;">这时如果应用业务压力小了，访问数据库的并发少了，连接数也不会减少（没有一个独立的线程来检查和销毁），将保持在maxIdle的数量。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">默认未配置initialSize(默认值是0)，但配置了timeBetweenEvictionRunsMillis=&#8220;30000&#8221;（30秒）参数时，刚启动tomcat时，连接数是0。马上应用有一个并发访问数据库时DBCP创建一个连接。</p><p style="margin: 0px; padding: 0px;">目前连接数量还未达到minIdle，每30秒DBCP的工作线程检查连接数是否少于minIdle数量，若少于就创建新连接直到达到minIdle数量。</p><p style="margin: 0px; padding: 0px;">随着应用并发访问数据库的增多，连接数也增多，直到达到maxIdle值。这期间每30秒DBCP的工作线程检查连接是否空闲了30分钟，若是就销毁。但此时是业务的高峰期，是不会有长达30分钟的空闲连接的，工作线程查了也是白查，但它在工作。到这里连接数量一直是呈现增长的趋势。</p><p style="margin: 0px; padding: 0px;">当连接数再增多超过maxIdle时，使用完的连接(刚刚空闲下来)会立即关闭，总体连接的数量稳定在maxIdle。停止了增长的趋势。但活动连接（在使用中的连接）可能数量上瞬间超过maxIdle，但永远不会超过maxActive。</p><p style="margin: 0px; padding: 0px;">这时如果应用业务压力小了，访问数据库的并发少了，每30秒DBCP的工作线程检查连接(默认每次查3条)是否空闲达到30分钟(这是默认值)，若连接空闲达到30分钟，就销毁连接。这时连接数减少了，呈下降趋势，将从maxIdle走向minIdle。当小于minIdle值时，则DBCP创建新连接已使数量稳定在minIdle，并进行着新老更替。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">配置initialSize=&#8220;10&#8221;时，tomcat一启动就创建10条连接。其它同上。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">minIdle要与timeBetweenEvictionRunsMillis配合使用才有用,单独使用minIdle不会起作用。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><h1>Tomcat中配置DBCP连接池</h1><p style="margin: 0px; padding: 0px;">-----------------------------</p><p style="margin: 0px; padding: 0px;">Tomcat自带DBCP的包，是$CATALINA_HOME/lib/tomcat-dbcp.jar。</p><p style="margin: 0px; padding: 0px;">tomcat-dbcp.jar含有commons pool、commons DBCP两个包的内容。但只含有与连接池有关的类。</p><p style="margin: 0px; padding: 0px;">数据源配置在context.xml文件中， 要在tomcat的lib目录中放jdbc 驱动包</p><p style="margin: 0px; padding: 0px;">数据源配置在server.xml的host中，不需要在tomcat的lib目录中放jdbc 驱动包，只使用工程中的jdbc驱动包</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">JNDI配置:更改tomcat的server.xml或context.xml</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 全局的数据源：</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 如果需要配置全局的 Resource，则在server.xml的GlobalNamingResources节点里加入Resource，再在Context节点里加入ResourceLink的配置。</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; 全局的resource只是为了重用，方便所有该tomcat下的web工程的数据源管理，但如果你的tomcat不会同时加载多个web工程，也就是说一个tomcat只加载一个web工程时，是没有必要配置全局的resource的。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">每个web工程一个数据源：</p><p style="margin: 0px; padding: 0px;">在$CATALINA_HOME/conf/context.xml的根节点Context里加入Resource配置。这种配置方法，你在context.xml配置了一个数据源，但Tomcat中有同时运行着5个工程，那了就坏事儿了，这个在Tomcat启动时数据源被创建了5份，每个工程1份数据源。连接数会是你配置的参数的5倍。</p><p style="margin: 0px; padding: 0px;">只有在你的Tomcat只加载一个web工程时,才可以直接以context.xml配置数据源。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&lt;Resource name="jdbc/testDB" &nbsp; &nbsp; &nbsp; //指定的jndi名称，会用于spring数据源bean的配置和ResourceLink的配置</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type="javax.sql.DataSource" &nbsp; //数据源类型，使用标准的javax.sql.DataSource</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;driverClassName="com.mysql.jdbc.Driver" &nbsp; &nbsp;//JDBC驱动器&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url="jdbc:mysql://localhost:3306/test" //数据库URL地址 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;username="test" &nbsp; &nbsp; //数据库用户名</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;password="test" &nbsp; //数据库密码</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maxIdle="40" &nbsp; //最大的空闲连接数</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maxWait="4000" //当池的数据库连接已经被占用的时候，最大等待时间</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maxActive="40" //连接池当中最大的数据库连接</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;removeAbandoned="true"&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;removeAbandonedTimeout="180"</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;logAbandoned="true" //被丢弃的数据库连接是否做记录，以便跟踪</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" /&gt;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; 这里的factory指的是该Resource 配置使用的是哪个数据源配置类，这里使用的是tomcat自带的标准数据源Resource配置类，这个类也可以自己写，实现javax.naming.spi.ObjectFactory 接口即可。某些地方使用的commons-dbcp.jar中的org.apache.commons.dbcp.BasicDataSourceFactory，如果使用这个就需把commons-dbcp.jar及其依赖的jar包，都放在tomcat的lib下，光放在工程的WEB-INF/lib下是不够的。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp;ResourceLink 的配置有多种：</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp;1)tomcat安装目录下的conf/context.xml，把全局的resource直接公开给该tomcat下的所有web工程，在Context节点中加入：</p><p style="margin: 0px; padding: 0px;">&lt;ResourceLink global="jdbc/testMDB" name="jdbc/testMDB" type="javax.sql.DataSource"/&gt; &nbsp;&nbsp;</p><p style="margin: 0px; padding: 0px;">不建议在此文件中，不使用&lt;ResourceLink/&gt;，而使用&lt;Resource/&gt;直接配置数据源，原因上面已说明了。 &nbsp;&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp;2)tomcat安装目录下的conf/server.xml，该方法可以指定把哪些source绑定到哪个web工程下。</p><p style="margin: 0px; padding: 0px;">&lt;!-- 新增，第一行为加载的工程配置，第二行是该工程需要的ResourceLink配置 --&gt;</p><p style="margin: 0px; padding: 0px;">&lt;context docBase="/web/webapps/phoenix" path="" reloadable="false"&gt;&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp; &lt;ResourceLink global="jdbc/testMDB" name="jdbc/testMDB" type="javax.sql.DataSource"/&gt;</p><p style="margin: 0px; padding: 0px;">&lt;/context&gt;</p><p style="margin: 0px; padding: 0px;">也可在此文件中，不使用&lt;ResourceLink/&gt;，而使用&lt;Resource/&gt;直接配置数据源。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp;3)安装目录下的conf/localhost/下建立一个xml文件，文件名是&lt;yourAppName&gt;.xml。比如工程名为test，则该xml名为test.xml。</p><p style="margin: 0px; padding: 0px;">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</p><p style="margin: 0px; padding: 0px;">&lt;Context&gt; &nbsp;&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &lt;ResourceLink global="jdbc/testMDB" name="jdbc/testMDB" type="javax.sql.DataSource"/&gt; &nbsp; &nbsp; &nbsp;&nbsp;</p><p style="margin: 0px; padding: 0px;">&lt;/context&gt;</p><p style="margin: 0px; padding: 0px;">也可在此文件中，不使用&lt;ResourceLink/&gt;，而使用&lt;Resource/&gt;直接配置数据源。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp; &nbsp; &nbsp;4)tomcat安装目录下的\webapps\test\META-INF\context.xml的Context节点中增加:</p><p style="margin: 0px; padding: 0px;">&lt;ResourceLink global="jdbc/testMDB" name="jdbc/testMDB" type="javax.sql.DataSource"/&gt;</p><p style="margin: 0px; padding: 0px;">也可在此文件中，不使用&lt;ResourceLink/&gt;，而使用&lt;Resource/&gt;直接配置数据源。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">本文内容都在tomcat6.0上运行测试过，还下载了commons DBCP的源码，加入了跟踪日志，用于验证本文的理论。</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><h1>连接池排名（纯个人看法）</h1><p style="margin: 0px; padding: 0px;">-----------------------------</p><p style="margin: 0px; padding: 0px;">Tomcat JDBC pool</p><p style="margin: 0px; padding: 0px;">DBCP 1.4</p><p style="margin: 0px; padding: 0px;">c3p0 &nbsp; 速度不错</p><p style="margin: 0px; padding: 0px;">BoneCP 速度不错，但会启用很多附加线程做回收、关闭工作。</p><p style="margin: 0px; padding: 0px;">Proxool 在高并发时出现异常</p><p style="margin: 0px; padding: 0px;">DBCP 1.2</p><p style="margin: 0px; padding: 0px;">DBPool 最差，垫底。</p><p style="margin: 0px; padding: 0px;">参考文档《Java连接池评估报告》</p><p style="margin: 0px; padding: 0px;">&nbsp;</p></div></div><img src ="http://www.blogjava.net/masen/aggbug/429456.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/masen/" target="_blank">Masen</a> 2016-02-27 11:06 <a href="http://www.blogjava.net/masen/articles/429456.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql防止重复插入记录方法总结 （转）</title><link>http://www.blogjava.net/masen/articles/424345.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Sun, 12 Apr 2015 11:34:00 GMT</pubDate><guid>http://www.blogjava.net/masen/articles/424345.html</guid><wfw:comment>http://www.blogjava.net/masen/comments/424345.html</wfw:comment><comments>http://www.blogjava.net/masen/articles/424345.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/masen/comments/commentRss/424345.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/masen/services/trackbacks/424345.html</trackback:ping><description><![CDATA[<div style="list-style: none; margin: 0px; color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px;">防止mysql重复插入记录的方法有很多种，常用的是ignore,Replace,ON DUPLICATE KEY UPDATE,当然我们也可以在php中加以判断了。</div><div style="list-style: none; margin: 0px; padding-top: 20px; padding-bottom: 20px; height: auto; overflow: hidden; color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px;"><span style="font-size: 14.3999996185303px;">方案一：使用ignore关键字</span></div><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">如果是用主键primary或者唯一索引unique区分了记录的唯一性,避免重复插入记录可以使用：</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">代码如下:</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy5733" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;">&nbsp;INSERT IGNORE INTO `table_name` (`email`, `phone`, `user_id`) VALUES ('test9@163.com', '99999', '9999');&nbsp;<br /></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">&nbsp;<br />这样当有重复记录就会忽略,执行后返回数字0</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />还有个应用就是复制表,避免重复记录：&nbsp;<br />代码如下:</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy8362" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;">&nbsp;INSERT IGNORE INTO `table_1` (`name`) SELECT `name` FROM `table_2`;&nbsp;<br /></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">&nbsp;<br />方案二：使用Replace</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />语法格式：</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">代码如下:</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy3733" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;"><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; word-wrap: break-word;">REPLACE INTO `table_name`(`col_name`, ...) VALUES (...);<br />REPLACE INTO `table_name` (`col_name`, ...) SELECT ...;<br />REPLACE INTO `table_name` SET `col_name`='value',</p></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">...算法说明：</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">REPLACE的运行与INSERT很相像,但是如果旧记录与新记录有相同的值，则在新记录被插入之前，旧记录被删除，即：</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />尝试把新行插入到表中</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">当因为对于主键或唯一关键字出现重复关键字错误而造成插入失败时：&nbsp;<br />从表中删除含有重复关键字值的冲突行&nbsp;<br />再次尝试把新行插入到表中&nbsp;<br />旧记录与新记录有相同的值的判断标准就是：<br />表有一个PRIMARY KEY或UNIQUE索引，否则，使用一个REPLACE语句没有意义。该语句会与INSERT相同，因为没有索引被用于确定是否新行复制了其它的行。&nbsp;<br />返回值：<br />REPLACE语句会返回一个数，来指示受影响的行的数目。该数是被删除和被插入的行数的和<br />受影响的行数可以容易地确定是否REPLACE只添加了一行，或者是否REPLACE也替换了其它行：检查该数是否为1（添加）或更大（替换）。&nbsp;<br />示例:<br /># eg:(phone字段为唯一索引)</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">代码如下:</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy3906" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;"><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; word-wrap: break-word;">REPLACE INTO `table_name` (`email`, `phone`, `user_id`) VALUES ('test569', '99999', '123');</p></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">另外,在 SQL Server 中可以这样处理：</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />代码如下:</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy6648" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;"><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; word-wrap: break-word;">if not exists (<a href="http://www.111cn.net/tags.php/select/" target="_blank" style="font-size: 15px; color: #2d64b3;">select</a>&nbsp;phone from t where phone= '1')&nbsp;&nbsp; insert into t(phone, update_time) values('1', getdate()) else&nbsp;&nbsp;&nbsp; update t set update_time = getdate() where phone= '1'</p></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><strong>方案三：ON DUPLICATE KEY UPDATE</strong></p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />如&#8205;上所写，你也可以在INSERT INTO&#8230;..后面加上 ON DUPLICATE KEY UPDATE方法来实现。如果您指定了ON DUPLICATE KEY UPDATE，并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值，则执行旧行UPDATE。&nbsp;<br />例如，如果列a被定义为UNIQUE，并且包含值1，则以下两个语句具有相同的效果：</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />代码如下:</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy1862" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;">&nbsp;<br />INSERT INTO `table` (`a`, `b`, `c`) VALUES (1, 2, 3) ON DUPLICATE KEY UPDATE `c`=`c`+1;&nbsp;<br />UPDATE `table` SET `c`=`c`+1 WHERE `a`=1;</td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">如果行作为新记录被插入，则受影响行的值为1；如果原有的记录被更新，则受影响行的值为2。</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />注释：如果列b也是唯一列，则INSERT与此UPDATE语句相当：&nbsp;<br />代码如下:</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy6940" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;">UPDATE `table` SET `c`=`c`+1 WHERE `a`=1 OR `b`=2 LIMIT 1;</td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">如果a=1 OR b=2与多个行向匹配，则只有一个行被更新。通常，您应该尽量避免对带有多个唯一关键字的表使用ON DUPLICATE KEY子句。</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />您可以在UPDATE子句中使用VALUES(col_name)函数从INSERT&#8230;UPDATE语句的INSERT部分引用列值。换句话说，如果没有发生重复关键字冲突，则UPDATE子句中的VALUES(col_name)可以引用被插入的col_name的值。本函数特别适用于多行插入。VALUES()函数只在INSERT&#8230;UPDATE语句中有意义，其它时候会返回NULL。</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />代码如下:</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy1005" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;">INSERT INTO `table` (`a`, `b`, `c`) VALUES (1, 2, 3), (4, 5, 6) ON DUPLICATE KEY UPDATE `c`=VALUES(`a`)+VALUES(`b`);</td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">本语句与以下两个语句作用相同：</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />代码如下:</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy5394" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;">INSERT INTO `table` (`a`, `b`, `c`) VALUES (1, 2, 3) ON DUPLICATE KEY UPDATE `c`=3;&nbsp;<br />INSERT INTO `table` (`a`, `b`, `c`) VALUES (4, 5, 6) ON DUPLICATE KEY UPDATE c=9;</td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">注释：当您使用ON DUPLICATE KEY UPDATE时，DELAYED选项被忽略。</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />示例：</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">这个例子是我在实际项目中用到的：是将一个表的数据导入到另外一个表中，数据的重复性就得考虑(如下)，唯一索引为：email：</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />代码如下:</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy7574" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;"><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; word-wrap: break-word;">INSERT INTO `table_name1` (`title`, `first_name`, `last_name`, `email`, `phone`, `user_id`, `role_id`, `status`, `campaign_id`)&nbsp;<br />&nbsp;&nbsp;&nbsp; SELECT '', '', '', `table_name2`.`email`, `table_name2`.`phone`, NULL, NULL, 'pending', 29 FROM `table_name2`&nbsp;<br />&nbsp;&nbsp;&nbsp; WHERE `table_name2`.`status` = 1&nbsp;<br />ON DUPLICATE KEY UPDATE `table_name1`.`status`='pending'</p></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">再贴一个例子：</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br />代码如下:</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy6609" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;"><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; word-wrap: break-word;">&nbsp;INSERT INTO `class` SELECT * FROM `class1` ON DUPLICATE KEY UPDATE `class`.`course`=`class1`.`course`</p></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">其它关键：DELAYED&nbsp; 做为快速插入，并不是很关心失效性，提高插入性能。&nbsp;<br />IGNORE&nbsp; 只关注主键对应记录是不存在，无则添加，有则忽略。</p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">php防止重复插入记录实例</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy9542" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;"><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; word-wrap: break-word;"><br />&lt;?php&nbsp;<br />$link=mysql_connect(&#8216;localhost&#8217;,&#8217;root&#8217;,&#8217;1234&#8217;); //得到MySQL<a href="http://www.111cn.net/list-55/" target="_blank" style="font-size: 15px; color: #2d64b3;">数据库</a>连接&nbsp;<br />$username=$_GET["name"]; //得到从客户端表单传过来的数据&nbsp;<br />$q="select * from usertable where user_name='$username'";&nbsp;<br />mysql_query("SET NAMES gb2312"); //避免出现中文乱码&nbsp;<br />$rs = mysql_query($q, $link); //查询<a href="http://www.111cn.net/database/database.html" target="_blank" style="font-size: 15px; color: #2d64b3;">数据库</a>&nbsp;<br />$num_rows = mysql_num_rows($rs); //得到查询结果的总行数&nbsp;<br />if($num_rows==0)&nbsp;<br />// 烈火? liehuo.net 欢迎复制,拒绝恶意采集 ｌｉｅｈｕｏ．ｎｅｔ&nbsp;<br />{&nbsp;<br />$exec="insert into student (user_name) values ($username)";&nbsp;<br />mysql_query("SET NAMES gb2312");&nbsp;<br />mysql_query($exec, $link); //若没有此用户则将数据插入到数据库(注册用户)&nbsp;<br />echo "用户注册成功！";&nbsp;<br />}&nbsp;<br />else&nbsp;<br />{&nbsp;<br />echo "该用户名已存在，请重新选择用户名！";&nbsp;<br />}&nbsp;<br />?&gt;</p></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;"><br /><strong>附一些删除重复记录的方法</strong></p><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">查询及删除重复记录的SQL语句&nbsp;<br />1、查找表中多余的重复记录，重复记录是根据单个字段（peopleId）来判断</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy3864" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;">select * from people&nbsp;<br />where peopleId in (select peopleId from people&nbsp;<a href="http://www.111cn.net/tags.php/group+by/" target="_blank" style="font-size: 15px; color: #2d64b3;">group by</a>&nbsp;peopleId having count(peopleId) &gt; 1)&nbsp;<br /></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">2、删除表中多余的重复记录，重复记录是根据单个字段（peopleId）来判断，只留有rowid最小的记录</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy6248" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;">delete from people&nbsp;<br />where peopleId in (select peopleId from people group by peopleId having count(peopleId) &gt; 1)&nbsp;<br />and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )&gt;1)&nbsp;<br /></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">3、查找表中多余的重复记录（多个字段）</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy9394" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;">select * from vitae a&nbsp;<br />where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) &gt; 1)&nbsp;<br /></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">4、删除表中多余的重复记录（多个字段），只留有rowid最小的记录</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy5576" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;">delete from vitae a&nbsp;<br />where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) &gt; 1)&nbsp;<br />and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)&gt;1)&nbsp;<br /></td></tr></tbody></table><p style="list-style: none; margin: 0px; padding: 8px 0px; font-size: 14px; line-height: 26px; word-wrap: break-word; color: #444444; font-family: Simsun;">5、查找表中多余的重复记录（多个字段），不包含rowid最小的记录</p><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="color: #444444; font-family: Simsun; font-size: 15.3333330154419px; line-height: 26px; background: #ffbb77;"><tbody><tr><td width="464" height="27" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px;">&nbsp;代码如下</td><td width="109" align="center" bgcolor="#FFE7CE" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 0px; cursor: pointer;">复制代码</td></tr><tr><td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" id="copy4284" style="font-size: 12px; color: #000000; list-style: none; margin: 0px; padding: 10px;">select * from vitae a&nbsp;<br />where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) &gt; 1)&nbsp;<br />and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)&gt;1)<br /><br /></td></tr></tbody></table><img src ="http://www.blogjava.net/masen/aggbug/424345.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/masen/" target="_blank">Masen</a> 2015-04-12 19:34 <a href="http://www.blogjava.net/masen/articles/424345.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql 分表 分区 （转）</title><link>http://www.blogjava.net/masen/articles/424314.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Sat, 11 Apr 2015 11:00:00 GMT</pubDate><guid>http://www.blogjava.net/masen/articles/424314.html</guid><wfw:comment>http://www.blogjava.net/masen/comments/424314.html</wfw:comment><comments>http://www.blogjava.net/masen/articles/424314.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/masen/comments/commentRss/424314.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/masen/services/trackbacks/424314.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 为什么要分表和分区？日常开发中我们经常会遇到大表的情况，所谓的大表是指存储了百万级乃至千万级条记录的表。这样的表过于庞大，导致数据库在查询和插入的时候耗时太长，性能低下，如果涉及联合查询的情况，性能会更加糟糕。分表和表分区的目的就是减少数据库的负担，提高数据库的效率，通常点来讲就是提高表的增删改查效率。什么是分表？分表是将一个大表按照一定的规则分解成多张具有独立存储空间的实体表，我们可以称为子表，...&nbsp;&nbsp;<a href='http://www.blogjava.net/masen/articles/424314.html'>阅读全文</a><img src ="http://www.blogjava.net/masen/aggbug/424314.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/masen/" target="_blank">Masen</a> 2015-04-11 19:00 <a href="http://www.blogjava.net/masen/articles/424314.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Oracle 常用命令（转）</title><link>http://www.blogjava.net/masen/articles/327769.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Mon, 02 Aug 2010 09:25:00 GMT</pubDate><guid>http://www.blogjava.net/masen/articles/327769.html</guid><wfw:comment>http://www.blogjava.net/masen/comments/327769.html</wfw:comment><comments>http://www.blogjava.net/masen/articles/327769.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/masen/comments/commentRss/327769.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/masen/services/trackbacks/327769.html</trackback:ping><description><![CDATA[<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; ">第一章:日志管理<br />
1.forcing log switches&nbsp;&nbsp; 迫使开关日志<br />
sql&gt; alter system switch logfile;<br />
2.forcing checkpoints&nbsp;&nbsp;&nbsp; 强迫检查<br />
sql&gt; alter system checkpoint;<br />
3.adding online redo log groups&nbsp;&nbsp;&nbsp; 加入联机重做日志组<br />
sql&gt; alter database add logfile [group 4]<br />
sql&gt; ('/disk3/log4a.rdo','/disk4/log4b.rdo') size 1m;<br />
4.adding online redo log members&nbsp;&nbsp;&nbsp; 加入联机重做日志成员<br />
sql&gt; alter database add logfile member<br />
sql&gt; '/disk3/log1b.rdo' to group 1,<br />
sql&gt; '/disk4/log2b.rdo' to group 2;<br />
5.changes the name of the online redo logfile&nbsp;&nbsp;&nbsp;&nbsp; 更改名称的联机重做日志<br />
sql&gt; alter database rename file 'c:/oracle/oradata/oradb/redo01.log'<br />
sql&gt; to 'c:/oracle/oradata/redo01.log';<br />
6.drop online redo log groups&nbsp;&nbsp;&nbsp; 落在线重做日志组<br />
sql&gt; alter database drop logfile group 3;<br />
7.drop online redo log members&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 落在线重做日志成员<br />
sql&gt; alter database drop logfile member 'c:/oracle/oradata/redo01.log';<br />
8.clearing online redo log files 结算在线重做日志文件<br />
sql&gt; alter database clear [unarchived] logfile 'c:/oracle/log2a.rdo';<br />
9.using logminer analyzing redo logfiles&nbsp; 使用logminer分析重做日志<br />
a. in the init.ora specify utl_file_dir = ' '<br />
b. sql&gt; execute dbms_logmnr_d.build('oradb.ora','c:\oracle\oradb\log');<br />
c. sql&gt; execute dbms_logmnr_add_logfile('c:\oracle\oradata\oradb\redo01.log',<br />
sql&gt; dbms_logmnr.new);<br />
d. sql&gt; execute dbms_logmnr.add_logfile('c:\oracle\oradata\oradb\redo02.log',<br />
sql&gt; dbms_logmnr.addfile);<br />
e. sql&gt; execute dbms_logmnr.start_logmnr(dictfilename=&gt;'c:\oracle\oradb\log\oradb.ora');<br />
f. sql&gt; select * from v$logmnr_contents(v$logmnr_dictionary,v$logmnr_parameters<br />
sql&gt; v$logmnr_logs);<br />
g. sql&gt; execute dbms_logmnr.end_logmnr;</p>
<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; "><br />
第二章:表空间管理<br />
1.create tablespaces&nbsp;&nbsp; 创建表空间<br />
sql&gt; create tablespace tablespace_name datafile 'c:\oracle\oradata\file1.dbf' size 100m,<br />
sql&gt; 'c:\oracle\oradata\file2.dbf' size 100m minimum extent 550k [logging/nologging]<br />
sql&gt; default storage (initial 500k next 500k maxextents 500 pctinccease 0)<br />
sql&gt; [online/offline] [permanent/temporary] [extent_management_clause]<br />
2.locally managed tablespace&nbsp;&nbsp;&nbsp; 本地管理表空间<br />
sql&gt; create tablespace user_data datafile 'c:\oracle\oradata\user_data01.dbf'<br />
sql&gt; size 500m extent management local uniform size 10m;<br />
3.temporary tablespace&nbsp;&nbsp;&nbsp; 临时表空间<br />
sql&gt; create temporary tablespace temp tempfile 'c:\oracle\oradata\temp01.dbf'<br />
sql&gt; size 500m extent management local uniform size 10m;<br />
4.change the storage setting&nbsp;&nbsp;&nbsp; 改变存储设置<br />
sql&gt; alter tablespace app_data minimum extent 2m;<br />
sql&gt; alter tablespace app_data default storage(initial 2m next 2m maxextents 999);<br />
5.taking tablespace offline or online&nbsp;&nbsp;&nbsp; 同时表空间离线或在线<br />
sql&gt; alter tablespace app_data offline;<br />
sql&gt; alter tablespace app_data online;<br />
6.read_only tablespace&nbsp;&nbsp;&nbsp; 只读表空间<br />
sql&gt; alter tablespace app_data read only|write;<br />
7.droping tablespace&nbsp;&nbsp;&nbsp; 删除表空间<br />
sql&gt; drop tablespace app_data including contents;<br />
8.enableing automatic extension of data files&nbsp;&nbsp; enableing自动延长数据档案<br />
sql&gt; alter tablespace app_data add datafile 'c:\oracle\oradata\app_data01.dbf' size 200m<br />
sql&gt; autoextend on next 10m maxsize 500m;<br />
9.change the size fo data files manually&nbsp; 尺寸变化的数据档案手动<br />
sql&gt; alter database datafile 'c:\oracle\oradata\app_data.dbf' resize 200m;<br />
10.Moving data files: alter tablespace&nbsp; 移动数据档案:改变表空间<br />
sql&gt; alter tablespace app_data rename datafile 'c:\oracle\oradata\app_data.dbf'<br />
sql&gt; to 'c:\oracle\app_data.dbf';<br />
11.moving data files:alter database&nbsp;&nbsp; 移动数据档案:改变数据库<br />
sql&gt; alter database rename file 'c:\oracle\oradata\app_data.dbf'<br />
sql&gt; to 'c:\oracle\app_data.dbf';</p>
<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; ">&nbsp;</p>
<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; ">第三章:表<br />
1.create a table&nbsp;&nbsp; 新建表<br />
sql&gt; create table table_name (column datatype,column datatype]....)<br />
sql&gt; tablespace tablespace_name [pctfree integer] [pctused integer]<br />
sql&gt; [initrans integer] [maxtrans integer]<br />
sql&gt; storage(initial 200k next 200k pctincrease 0 maxextents 50)<br />
sql&gt; [logging|nologging] [cache|nocache]<br />
2.copy an existing table&nbsp;&nbsp; 复制一个现有的表<br />
sql&gt; create table table_name [logging|nologging] as subquery<br />
3.create temporary table&nbsp;&nbsp; 创建临时表<br />
sql&gt; create global temporary table xay_temp as select * from xay;<br />
on commit preserve rows/on commit delete rows<br />
4.pctfree = (average row size - initial row size) *100 /average row size<br />
pctused = 100-pctfree- (average row size*100/available data space)<br />
5.change storage and block utilization parameter&nbsp; 改变存储和利用块参数<br />
sql&gt; alter table table_name pctfree=30 pctused=50 storage(next 500k<br />
sql&gt; minextents 2 maxextents 100);<br />
6.manually allocating extents<br />
sql&gt; alter table table_name allocate extent(size 500k datafile 'c:/oracle/data.dbf');<br />
7.move tablespace&nbsp;&nbsp; 移动表空间<br />
sql&gt; alter table employee move tablespace users;<br />
8.deallocate of unused space&nbsp; deallocate的闲置空间<br />
sql&gt; alter table table_name deallocate unused [keep integer]<br />
9.truncate a table&nbsp;&nbsp; 截断表<br />
sql&gt; truncate table table_name;<br />
10.drop a table&nbsp; 删除表<br />
sql&gt; drop table table_name [cascade constraints];<br />
11.drop a column&nbsp; 删除列<br />
sql&gt; alter table table_name drop column comments cascade constraints checkpoint 1000;<br />
alter table table_name drop columns continue;<br />
12.mark a column as unused<br />
sql&gt; alter table table_name set unused column comments cascade constraints;<br />
alter table table_name drop unused columns checkpoint 1000;<br />
alter table orders drop columns continue checkpoint 1000<br />
data_dictionary : dba_unused_col_tabs</p>
<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; ">&nbsp;</p>
<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; ">　　第四章:索引<br />
1.creating function-based indexes&nbsp; 建立基于功能的索引<br />
sql&gt; create index summit.item_quantity on summit.item(quantity-quantity_shipped);<br />
2.create a B-tree index&nbsp;&nbsp;&nbsp; 创造一个B树索引<br />
sql&gt; create [unique] index index_name on table_name(column,.. asc/desc) tablespace<br />
sql&gt; tablespace_name [pctfree integer] [initrans integer] [maxtrans integer]<br />
sql&gt; [logging | nologging] [nosort] storage(initial 200k next 200k pctincrease 0<br />
sql&gt; maxextents 50);<br />
3.pctfree(index)=(maximum number of rows-initial number of rows)*100/maximum number of rows<br />
4.creating reverse key indexes&nbsp;&nbsp; 创造逆转关键指标<br />
sql&gt; create unique index xay_id on xay(a) reverse pctfree 30 storage(initial 200k<br />
sql&gt; next 200k pctincrease 0 maxextents 50) tablespace indx;<br />
5.create bitmap index&nbsp;&nbsp; 创建位图索引<br />
sql&gt; create bitmap index xay_id on xay(a) pctfree 30 storage( initial 200k next 200k<br />
sql&gt; pctincrease 0 maxextents 50) tablespace indx;<br />
6.change storage parameter of index&nbsp;&nbsp; 改变存储参数指标<br />
sql&gt; alter index xay_id storage (next 400k maxextents 100);<br />
7.allocating index space&nbsp;&nbsp;&nbsp; 分配索引空间<br />
sql&gt; alter index xay_id allocate extent(size 200k datafile 'c:/oracle/index.dbf');<br />
8.alter index xay_id deallocate unused;&nbsp;&nbsp;&nbsp; 改变指数xay_id deallocate剩</p>
<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; ">&nbsp;</p>
<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; "><br />
第五章:约束<br />
1.define constraints as immediate or deferred&nbsp;&nbsp;&nbsp;&nbsp; 界定制约即期或延期<br />
sql&gt; alter session set constraint[s] = immediate/deferred/default;<br />
set constraint[s] constraint_name/all immediate/deferred;<br />
2. sql&gt; drop table table_name cascade constraints<br />
sql&gt; drop tablespace tablespace_name including contents cascade constraints<br />
3. define constraints while create a table&nbsp;&nbsp;&nbsp; 界定限制,同时创建一个表<br />
sql&gt; create table xay(id number(7) constraint xay_id primary key deferrable<br />
sql&gt; using index storage(initial 100k next 100k) tablespace indx);<br />
primary key/unique/references table(column)/check<br />
4.enable constraints&nbsp;&nbsp;&nbsp; 使制约<br />
sql&gt; alter table xay enable novalidate constraint xay_id;<br />
5.enable constraints&nbsp;&nbsp;&nbsp;&nbsp; 使制约<br />
sql&gt; alter table xay enable validate constraint xay_id;</p>
<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; ">&nbsp;</p>
<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; "><br />
第六章:LOAD数据<br />
1.loading data using direct_load insert<br />
sql&gt; insert /*+append */ into emp nologging<br />
sql&gt; select * from emp_old;<br />
2.parallel direct-load insert<br />
sql&gt; alter session enable parallel dml;<br />
sql&gt; insert /*+parallel(emp,2) */ into emp nologging<br />
sql&gt; select * from emp_old;<br />
3.using sql*loader<br />
sql&gt; sqlldr scott/tiger \<br />
sql&gt; control = ulcase6.ctl \<br />
sql&gt; log = ulcase6.log direct=true</p>
<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; ">&nbsp;</p>
<p style="display: block; font-size: 14px; line-height: 26px; color: #2b516c; text-decoration: none; "><br />
第七章:数据整理<br />
1.using expoty<br />
$exp scott/tiger tables(dept,emp) file=c:\emp.dmp log=exp.log compress=n direct=y<br />
2.using import<br />
$imp scott/tiger tables(dept,emp) file=emp.dmp log=imp.log ignore=y<br />
3.transporting a tablespace<br />
sql&gt;alter tablespace sales_ts read only;<br />
$exp sys/.. file=xay.dmp transport_tablespace=y tablespace=sales_ts<br />
triggers=n constraints=n<br />
$copy datafile<br />
$imp sys/.. file=xay.dmp transport_tablespace=y datafiles=(/disk1/sles01.dbf,/disk2<br />
/sles02.dbf)<br />
sql&gt; alter tablespace sales_ts read write;<br />
4.checking transport set<br />
sql&gt; DBMS_tts.transport_set_check(ts_list =&gt;'sales_ts' ..,incl_constraints=&gt;true);<br />
在表transport_set_violations 中查看<br />
sql&gt; dbms_tts.isselfcontained 为true 是， 表示自包含<br />
第八章: 密码安全与资源管理<br />
1.controlling account lock and password<br />
sql&gt; alter user juncky identified by oracle account unlock;<br />
2.user_provided password function<br />
sql&gt; function_name(userid in varchar2(30),password in varchar2(30),<br />
old_password in varchar2(30)) return boolean<br />
3.create a profile : password setting<br />
sql&gt; create profile grace_5 limit failed_login_attempts 3<br />
sql&gt; password_lock_time unlimited password_life_time 30<br />
sql&gt;password_reuse_time 30 password_verify_function verify_function<br />
sql&gt; password_grace_time 5;<br />
4.altering a profile<br />
sql&gt; alter profile default failed_login_attempts 3<br />
sql&gt; password_life_time 60 password_grace_time 10;<br />
5.drop a profile<br />
sql&gt; drop profile grace_5 [cascade];<br />
6.create a profile : resource limit<br />
sql&gt; create profile developer_prof limit sessions_per_user 2<br />
sql&gt; cpu_per_session 10000 idle_time 60 connect_time 480;<br />
7. view =&gt; resource_cost : alter resource cost<br />
dba_Users,dba_profiles<br />
8. enable resource limits<br />
sql&gt; alter system set resource_limit=true;<br />
第九章:用户管理<br />
1.create a user: database authentication<br />
sql&gt; create user juncky identified by oracle default tablespace users<br />
sql&gt; temporary tablespace temp quota 10m on data password expire<br />
sql&gt; [account lock|unlock] [profile profilename|default];<br />
2.change user quota on tablespace<br />
sql&gt; alter user juncky quota 0 on users;<br />
3.drop a user<br />
sql&gt; drop user juncky [cascade];<br />
4. monitor user<br />
view: dba_users , dba_ts_quotas<br />
第十章:特权管理<br />
1.system privileges: view =&gt; system_privilege_map ,dba_sys_privs,session_privs<br />
2.grant system privilege<br />
sql&gt; grant create session,create table to managers;<br />
sql&gt; grant create session to scott with admin option;<br />
with admin option can grant or revoke privilege from any user or role;<br />
3.sysdba and sysoper privileges:<br />
sysoper: startup,shutdown,alter database open|mount,alter database backup controlfile,<br />
alter tablespace begin/end backup,recover database<br />
alter database archivelog,restricted session<br />
sysdba: sysoper privileges with admin option,create database,recover database until<br />
4.password file members: view:=&gt; v$pwfile_users<br />
5.O7_dictionary_accessibility =true restriction access to view or tables in other schema<br />
6.revoke system privilege<br />
sql&gt; revoke create table from karen;<br />
sql&gt; revoke create session from scott;<br />
7.grant object privilege<br />
sql&gt; grant execute on dbms_pipe to public;<br />
sql&gt; grant update(first_name,salary) on employee to karen with grant option;<br />
8.display object privilege : view =&gt; dba_tab_privs, dba_col_privs<br />
9.revoke object privilege<br />
sql&gt; revoke execute on dbms_pipe from scott [cascade constraints];<br />
10.audit record view :=&gt; sys.aud$<br />
11. protecting the audit trail<br />
sql&gt; audit delete on sys.aud$ by access;<br />
12.statement auditing<br />
sql&gt; audit user;<br />
13.privilege auditing<br />
sql&gt; audit select any table by summit by access;<br />
14.schema object auditing<br />
sql&gt; audit lock on summit.employee by access whenever successful;<br />
15.view audit option : view=&gt; all_def_audit_opts,dba_stmt_audit_opts,dba_priv_audit_opts,dba_obj_audit_opts<br />
16.view audit result: view=&gt; dba_audit_trail,dba_audit_exists,dba_audit_object,dba_audit_session,dba_audit_statement<br />
第十一章: 规则管理器<br />
1.create roles<br />
sql&gt; create role sales_clerk;<br />
sql&gt; create role hr_clerk identified by bonus;<br />
sql&gt; create role hr_manager identified externally;<br />
2.modify role<br />
sql&gt; alter role sales_clerk identified by commission;<br />
sql&gt; alter role hr_clerk identified externally;<br />
sql&gt; alter role hr_manager not identified;<br />
3.assigning roles<br />
sql&gt; grant sales_clerk to scott;<br />
sql&gt; grant hr_clerk to hr_manager;<br />
sql&gt; grant hr_manager to scott with admin option;<br />
4.establish default role<br />
sql&gt; alter user scott default role hr_clerk,sales_clerk;<br />
sql&gt; alter user scott default role all;<br />
sql&gt; alter user scott default role all except hr_clerk;<br />
sql&gt; alter user scott default role none;<br />
5.enable and disable roles<br />
sql&gt; set role hr_clerk;<br />
sql&gt; set role sales_clerk identified by commission;<br />
sql&gt; set role all except sales_clerk;<br />
sql&gt; set role none;<br />
6.remove role from user<br />
sql&gt; revoke sales_clerk from scott;<br />
sql&gt; revoke hr_manager from public;<br />
7.remove role<br />
sql&gt; drop role hr_manager;<br />
8.display role information<br />
view: =&gt;dba_roles,dba_role_privs,role_role_privs,dba_sys_privs,role_sys_privs,role_tab_privs,session_roles<br />
第十二章: 备份与恢复<br />
1. v$sga,v$instance,v$process,v$bgprocess,v$database,v$datafile,v$sgastat<br />
2. Rman need set dbwr_IO_slaves or backup_tape_IO_slaves and large_pool_size<br />
3. Monitoring Parallel Rollback<br />
&gt; v$fast_start_servers , v$fast_start_transactions<br />
4.perform a closed database backup (noarchivelog)<br />
&gt; shutdown immediate<br />
&gt; cp files /backup/<br />
&gt; startup<br />
5.restore to a different location<br />
&gt; connect system/manager as sysdba<br />
&gt; startup mount<br />
&gt; alter database rename file '/disk1/../user.dbf' to '/disk2/../user.dbf';<br />
&gt; alter database open;<br />
6.recover syntax<br />
--recover a mounted database<br />
&gt;recover database;<br />
&gt;recover datafile '/disk1/data/df2.dbf';<br />
&gt;alter database recover database;<br />
--recover an opened database<br />
&gt;recover tablespace user_data;<br />
&gt;recover datafile 2;<br />
&gt;alter database recover datafile 2;<br />
7.how to apply redo log files automatically<br />
&gt;set autorecovery on<br />
&gt;recover automatic datafile 4;<br />
8.complete recovery:<br />
--method 1(mounted databae)<br />
&gt;copy c:\backup\user.dbf c:\oradata\user.dbf<br />
&gt;startup mount<br />
&gt;recover datafile 'c:\oradata\user.dbf;<br />
&gt;alter database open;<br />
--method 2(opened database,initially opened,not system or rollback datafile)<br />
&gt;copy c:\backup\user.dbf c:\oradata\user.dbf (alter tablespace offline)<br />
&gt;recover datafile 'c:\oradata\user.dbf' or<br />
&gt;recover tablespace user_data;<br />
&gt;alter database datafile 'c:\oradata\user.dbf' online or<br />
&gt;alter tablespace user_data online;<br />
--method 3(opened database,initially closed not system or rollback datafile)<br />
&gt;startup mount<br />
&gt;alter database datafile 'c:\oradata\user.dbf' offline;<br />
&gt;alter database open<br />
&gt;copy c:\backup\user.dbf d:\oradata\user.dbf<br />
&gt;alter database rename file 'c:\oradata\user.dbf' to 'd:\oradata\user.dbf'<br />
&gt;recover datafile 'e:\oradata\user.dbf' or recover tablespace user_data;<br />
&gt;alter tablespace user_data online;<br />
--method 4(loss of data file with no backup and have all archive log)<br />
&gt;alter tablespace user_data offline immediate;<br />
&gt;alter database create datafile 'd:\oradata\user.dbf' as 'c:\oradata\user.dbf''<br />
&gt;recover tablespace user_data;<br />
&gt;alter tablespace user_data online<br />
5.perform an open database backup<br />
&gt; alter tablespace user_data begin backup;<br />
&gt; copy files /backup/<br />
&gt; alter database datafile '/c:/../data.dbf' end backup;<br />
&gt; alter system switch logfile;<br />
6.backup a control file<br />
&gt; alter database backup controlfile to 'control1.bkp';<br />
&gt; alter database backup controlfile to trace;<br />
7.recovery (noarchivelog mode)<br />
&gt; shutdown abort<br />
&gt; cp files<br />
&gt; startup<br />
8.recovery of file in backup mode<br />
&gt;alter database datafile 2 end backup;<br />
9.clearing redo log file<br />
&gt;alter database clear unarchived logfile group 1;<br />
&gt;alter database clear unarchived logfile group 1 unrecoverable datafile;<br />
10.redo log recovery<br />
&gt;alter database add logfile group 3 'c:\oradata\redo03.log' size 1000k;<br />
&gt;alter database drop logfile group 1;<br />
&gt;alter database open;<br />
or &gt;cp c:\oradata\redo02.log' c:\oradata\redo01.log<br />
&gt;alter database clear logfile 'c:\oradata\log01.log';</p>
<img src ="http://www.blogjava.net/masen/aggbug/327769.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/masen/" target="_blank">Masen</a> 2010-08-02 17:25 <a href="http://www.blogjava.net/masen/articles/327769.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDBC下各种数据库的连接字符串和驱动类一览</title><link>http://www.blogjava.net/masen/articles/182056.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Mon, 25 Feb 2008 09:56:00 GMT</pubDate><guid>http://www.blogjava.net/masen/articles/182056.html</guid><wfw:comment>http://www.blogjava.net/masen/comments/182056.html</wfw:comment><comments>http://www.blogjava.net/masen/articles/182056.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/masen/comments/commentRss/182056.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/masen/services/trackbacks/182056.html</trackback:ping><description><![CDATA[JDBC下各种数据库的连接字符串和驱动类一览
<h2 class="ContentAuthor">&nbsp;</h2>
<div id="log_1">
<div class="Content-body" id="logcontent_146" style="table-layout: fixed; word-break: break-all"><font color="#0000ff">DB2 v8:<br />
jdbc:db2://host/dbName<br />
COM.ibm.db2.jdbc.net.DB2Driver<br />
<br />
DB2 v9<br />
jdbc:db2://host/dbName:deferPrepares=false;<br />
com.ibm.db2.jcc.DB2Driver<br />
<br />
Informix:<br />
jdbc:informix-sqli://host/dbName:INFORMIXSERVER=server<br />
com.informix.jdbc.IfxDriver<br />
<br />
SQLServer:<br />
jdbc:sqlserver://host;DatabaseName=dbName<br />
com.microsoft.jdbc.sqlserver.SQLServerDriver<br />
<br />
MySQL：<br />
jdbc:mysql://host/dbName?useUnicode=true&amp;characterEncoding=GBK<br />
com.mysql.jdbc.Driver<br />
<br />
Oracle：<br />
jdbc:oracle:thin:@host:dbName<br />
oracle.jdbc.driver.OracleDriver<br />
<br />
Sybase：<br />
jdbc:sybase:Tds:host/dbName<br />
com.sybase.jdbc2.jdbc.SybDriver<br />
<br />
Teradata:<br />
jdbc:teradata://host/dbName<br />
com.ncr.teradata.TeraDriver</font>&nbsp;&nbsp;</div>
</div>
<br />
<p id="TBPingURL">&nbsp;</p>
<img src ="http://www.blogjava.net/masen/aggbug/182056.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/masen/" target="_blank">Masen</a> 2008-02-25 17:56 <a href="http://www.blogjava.net/masen/articles/182056.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>分页sql</title><link>http://www.blogjava.net/masen/articles/118702.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Sun, 20 May 2007 08:42:00 GMT</pubDate><guid>http://www.blogjava.net/masen/articles/118702.html</guid><wfw:comment>http://www.blogjava.net/masen/comments/118702.html</wfw:comment><comments>http://www.blogjava.net/masen/articles/118702.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/masen/comments/commentRss/118702.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/masen/services/trackbacks/118702.html</trackback:ping><description><![CDATA[<p>MSSQL</p>
<p>如在ms&nbsp;&nbsp; sqlserver&nbsp;&nbsp; 用此语句：&nbsp;&nbsp; <br>&nbsp; select&nbsp;&nbsp; top&nbsp;&nbsp; 2&nbsp;&nbsp; *&nbsp;&nbsp; from&nbsp;&nbsp; test01&nbsp;&nbsp; 就会只显示前2条记录，&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>&nbsp; </p>
<p><br>MYSQL</p>
<p>select&nbsp;&nbsp; *&nbsp;&nbsp; from&nbsp;&nbsp; your_table&nbsp;&nbsp; where&nbsp;&nbsp; ....&nbsp;&nbsp; limit&nbsp;&nbsp; 2;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>&nbsp; 使用limit就可以了. </p>
<p>&nbsp;</p>
<p>Oracle的语句:&nbsp; <br>select * from (select rownum r ,* from test) tt&nbsp; <br>where tt.r &gt; 50 and tt.r &lt;= 100;&nbsp; (最好用PLSQL游标来解决)</p>
<p><br>DB2中：&nbsp; <br>select * from payment fetch first 5 row only --查前5条记录&nbsp; </p>
<p>&nbsp;</p>
<p>MySQL</p>
<p><br>&nbsp;<br>select * from Cat limit ?,?&nbsp;&nbsp; <br>select * from Cat limit 20000,100&nbsp;&nbsp; <br>从Cat表中第20000条开始取出100条记录，即记录号为20000到20099的记录&nbsp;&nbsp; <br>&nbsp;&nbsp; <br>select * from Cat limit ?&nbsp;&nbsp; <br>select * from Cat limit 100&nbsp;&nbsp; <br>从Cat表中取出记录号为1到100的记录&nbsp;&nbsp; <br>&nbsp;&nbsp; <br>select * from Cat order by rand() limit ?&nbsp;&nbsp; <br>select * from Cat order by rand() limit 100&nbsp;&nbsp; <br>从Cat表中随机取出100条记录&nbsp;&nbsp; </p>
<p>Oracle</p>
<p><br>&nbsp;<br>select * from (select *, rownum rownum_ from Cat where rownum &lt;= ?) where rownum_ &gt; ?&nbsp;&nbsp; <br>select * from (select *, rownum rownum_ from Cat where rownum &lt;= 20099) where rownum_ &gt; 19999&nbsp;&nbsp; <br>从Cat表中第20000条开始取出100条记录，即记录号为20000到20099的记录&nbsp;&nbsp; <br>&nbsp;&nbsp; <br>select * from Cat where rownum &lt;= ?&nbsp;&nbsp; <br>select * from Cat where rownum &lt;= 100&nbsp;&nbsp; <br>从Cat表中取出记录号为1到100的记录&nbsp;&nbsp; <br>&nbsp;&nbsp; <br>select * from (select * from Cat order by dbms_random.value) where rownum &lt; ?&nbsp;&nbsp; <br>select * from (select * from Cat order by dbms_random.value) where rownum &lt; 100&nbsp;&nbsp; <br>从Cat表中随机取出100条记录&nbsp;&nbsp; </p>
<p>HSQLDB</p>
<p><br>select * from Cat limit ? ?&nbsp;&nbsp; <br>select * from Cat limit 20000 100&nbsp;&nbsp; <br>从Cat表中第20000条开始取出100条记录，即记录号为20000到20099的记录&nbsp;&nbsp; <br>&nbsp;&nbsp; <br>select * from Cat top ?&nbsp;&nbsp; <br>select * from Cat top 100&nbsp;&nbsp; <br>从Cat表中取出记录号为1到100的记录&nbsp;&nbsp; <br></p>
<img src ="http://www.blogjava.net/masen/aggbug/118702.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/masen/" target="_blank">Masen</a> 2007-05-20 16:42 <a href="http://www.blogjava.net/masen/articles/118702.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Mysql常用函数列表</title><link>http://www.blogjava.net/masen/articles/116812.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Fri, 11 May 2007 09:38:00 GMT</pubDate><guid>http://www.blogjava.net/masen/articles/116812.html</guid><wfw:comment>http://www.blogjava.net/masen/comments/116812.html</wfw:comment><comments>http://www.blogjava.net/masen/articles/116812.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/masen/comments/commentRss/116812.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/masen/services/trackbacks/116812.html</trackback:ping><description><![CDATA[<table style="BORDER-COLLAPSE: collapse; WORD-WRAP: break-word" cellSpacing=0 cellPadding=0 width=760 align=center bgColor=#ffffff border=0>
    <tbody>
        <tr>
            <td align=middle height=30><font style="FONT-SIZE: 14pt" color=#02368d><strong>Mysql常用函数列表</strong></font><br></td>
        </tr>
        <tr>
            <td align=middle height=9><img height=9 alt="" src="file:///C:/Documents%20and%20Settings/masen/桌面/Mysql常用函数列表%20-%20MYSQL%20-%20windlike%20的技术博客.files/right_line.gif" width=502 border=0></td>
        </tr>
        <tr>
            <td align=middle>
            <table style="BORDER-COLLAPSE: collapse; WORD-WRAP: break-word" cellSpacing=0 cellPadding=0 width=740 border=0>
                <tbody>
                    <tr>
                        <td width=740>
                        <div id=art style="MARGIN: 15px" width="560">
                        <div>
                        <div><strong><u>字符串类</u></strong></div>
                        <div><strong><u></u></strong>&nbsp;</div>
                        <div>CHARSET(str) //返回字串字符集<br>CONCAT (string2&nbsp; [,... ]) //连接字串<br>INSTR (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0<br>LCASE (string2 ) //转换成小写<br>LEFT (string2 ,length ) //从string2中的左边起取length个字符<br>LENGTH (string ) //string长度<br>LOAD_FILE (file_name ) //从文件读取内容<br>LOCATE (substring , string&nbsp; [,start_position ] ) 同INSTR,但可指定开始位置<br>LPAD (string2 ,length ,pad ) //重复用pad加在string开头,直到字串长度为length<br>LTRIM (string2 ) //去除前端空格<br>REPEAT (string2 ,count ) //重复count次<br>REPLACE (str ,search_str ,replace_str ) //在str中用replace_str替换search_str<br>RPAD (string2 ,length ,pad) //在str后用pad补充,直到长度为length<br>RTRIM (string2 ) //去除后端空格<br>STRCMP (string1 ,string2 ) //逐字符比较两字串大小,<br>SUBSTRING (str , position&nbsp; [,length ]) //从str的position开始,取length个字符,<br>TRIM([[BOTH|LEADING|TRAILING] [padding] FROM]string2) //去除指定位置的指定字符<br>UCASE (string2 ) //转换成大写<br>RIGHT(string2,length) //取string2最后length个字符<br>SPACE(count) //生成count个空格</div>
                        <div>&nbsp;</div>
                        <div><strong><u>数学类</u></strong></div>
                        <div><strong><u></u></strong><br>ABS (number2 ) //绝对值<br>BIN (decimal_number ) //十进制转二进制<br>CEILING (number2 ) //向上取整<br>CONV(number2,from_base,to_base) //进制转换<br>FLOOR (number2 ) //向下取整<br>FORMAT (number,decimal_places ) //保留小数位数<br>HEX (DecimalNumber ) //转十六进制<br>LEAST (number , number2&nbsp; [,..]) //求最小值<br>MOD (numerator ,denominator ) //求余<br>POWER (number ,power ) //求指数<br>RAND([seed]) //随机数<br>ROUND (number&nbsp; [,decimals ]) //四舍五入,decimals为小数位数]<br>SIGN (number2 ) //返回符号,正负或0<br>SQRT(number2) //开平方</div>
                        <div>&nbsp;</div>
                        <div><strong><u>日期时间类</u></strong></div>
                        <div><strong><u></u></strong>&nbsp;</div>
                        <div>ADDTIME (date2 ,time_interval ) //将time_interval加到date2<br>CONVERT_TZ (datetime2 ,fromTZ ,toTZ ) //转换时区<br>CURRENT_DATE (&nbsp; ) //当前日期<br>CURRENT_TIME (&nbsp; ) //当前时间<br>CURRENT_TIMESTAMP (&nbsp; ) //当前时间戳<br>DATE (datetime ) //返回datetime的日期部分<br>DATE_ADD (date2 , INTERVAL d_value d_type ) //在date2中加上日期或时间<br>DATE_FORMAT (datetime ,FormatCodes ) //使用formatcodes格式显示datetime<br>DATE_SUB (date2 , INTERVAL d_value d_type ) //在date2上减去一个时间<br>DATEDIFF (date1 ,date2 ) //两个日期差<br>DAY (date ) //返回日期的天<br>DAYNAME (date ) //英文星期<br>DAYOFWEEK (date ) //星期(1-7) ,1为星期天<br>DAYOFYEAR (date ) //一年中的第几天<br>EXTRACT (interval_name&nbsp; FROM date ) //从date中提取日期的指定部分<br>MAKEDATE (year ,day ) //给出年及年中的第几天,生成日期串<br>MAKETIME (hour ,minute ,second ) //生成时间串<br>MONTHNAME (date ) //英文月份名<br>NOW (&nbsp; ) //当前时间<br>SEC_TO_TIME (seconds ) //秒数转成时间<br>STR_TO_DATE (string ,format ) //字串转成时间,以format格式显示<br>TIMEDIFF (datetime1 ,datetime2 ) //两个时间差<br>TIME_TO_SEC (time ) //时间转秒数]<br>WEEK (date_time [,start_of_week ]) //第几周<br>YEAR (datetime ) //年份<br>DAYOFMONTH(datetime) //月的第几天<br>HOUR(datetime) //小时<br>LAST_DAY(date) //date的月的最后日期<br>MICROSECOND(datetime) //微秒<br>MONTH(datetime) //月<br>MINUTE(datetime) //分</div>
                        <div>&nbsp;</div>
                        <div>附:可用在INTERVAL中的类型<br>DAY ,DAY_HOUR ,DAY_MINUTE ,DAY_SECOND ,HOUR ,HOUR_MINUTE ,HOUR_SECOND ,MINUTE ,MINUTE_SECOND,MONTH ,SECOND ,YEAR </div>
                        </div>
                        </div>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/masen/aggbug/116812.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/masen/" target="_blank">Masen</a> 2007-05-11 17:38 <a href="http://www.blogjava.net/masen/articles/116812.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>