﻿<?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>C++博客-庸人自扰-随笔分类-数据库</title><link>http://www.cppblog.com/wmuu/category/1782.html</link><description>含笑半步癫是用蜂蜜、川贝、桔梗、加上天山雪莲配制而成，不需冷藏，也没有防腐剂，除了毒性猛烈之外，味道还很好吃！而吃了含笑半步癫的朋友，顾名思义绝对不能走半步路或是面露笑容，否则也会全身爆炸而死。实在是居家旅行，杀人灭口，必备良药！
</description><language>zh-cn</language><lastBuildDate>Wed, 21 May 2008 07:43:32 GMT</lastBuildDate><pubDate>Wed, 21 May 2008 07:43:32 GMT</pubDate><ttl>60</ttl><item><title>mysql 常用命令(记录)</title><link>http://www.cppblog.com/wmuu/archive/2008/01/11/40943.html</link><dc:creator>含笑半步癫</dc:creator><author>含笑半步癫</author><pubDate>Fri, 11 Jan 2008 01:55:00 GMT</pubDate><guid>http://www.cppblog.com/wmuu/archive/2008/01/11/40943.html</guid><wfw:comment>http://www.cppblog.com/wmuu/comments/40943.html</wfw:comment><comments>http://www.cppblog.com/wmuu/archive/2008/01/11/40943.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wmuu/comments/commentRss/40943.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wmuu/services/trackbacks/40943.html</trackback:ping><description><![CDATA[1) 添加远程用户登陆权限<br>&nbsp; 系统提示符下 mysql --user=root -p<br>&nbsp;&nbsp; use mysql<br>GRANT ALL PRIVILEGES ON *.* TO <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#114;&#111;&#111;&#116;&#64;&#102;&#105;&#110;&#97;&#112;&#112;">root@finapp</a> IDENTIFIED BY "123"<br>grant all privieges on database.table to <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#117;&#115;&#101;&#114;&#64;&#73;&#112;&#79;&#114;&#78;&#97;&#109;&#101;">user@IpOrName</a> identified by "password"<br><br><br><br><br><br><br><br>
<img src ="http://www.cppblog.com/wmuu/aggbug/40943.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wmuu/" target="_blank">含笑半步癫</a> 2008-01-11 09:55 <a href="http://www.cppblog.com/wmuu/archive/2008/01/11/40943.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MySQL性能优化相关(转) 作者：andyao</title><link>http://www.cppblog.com/wmuu/archive/2008/01/09/40838.html</link><dc:creator>含笑半步癫</dc:creator><author>含笑半步癫</author><pubDate>Wed, 09 Jan 2008 15:52:00 GMT</pubDate><guid>http://www.cppblog.com/wmuu/archive/2008/01/09/40838.html</guid><wfw:comment>http://www.cppblog.com/wmuu/comments/40838.html</wfw:comment><comments>http://www.cppblog.com/wmuu/archive/2008/01/09/40838.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wmuu/comments/commentRss/40838.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wmuu/services/trackbacks/40838.html</trackback:ping><description><![CDATA[作者：andyao<br>原文link: http://andyao.javaeye.com/admin/show/144033<br>转载请留名<br><br><font size=4><strong>1. 简介</strong></font><br>在Web应用程序体系架构中，数据持久层（通常是一个关系数据库）是关键的核心部分，它对系统的性能有非常重要的影响。MySQL是目前使用最多的开源数据库，但是MySQL数据库的默认设置性能非常的差，仅仅是一个玩具数据库。因此在产品中使用MySQL数据库必须进行必要的优化。<br>优化是一个复杂的任务，本文描述MySQL相关的数据库设计和查询优化，服务器端优化，存储引擎优化。<br><br><strong><font size=4>2. 数据库设计和查询优化</font></strong><br>在MySQL Server性能调优中，首先要考虑的就是Database Schema设计，这一点是非常重要的。一个糟糕的Schema设计即使在性能调优的MySQL Server上运行，也会表现出很差的性能；和Schema相似，查询语句的设计也会影响MySQL的性能，应该避免写出低效的SQL查询。这一节将详细讨论这两方面的优化。<br><br><font size=3>2.1 Schema Design</font><br>Schema的优化取决于将要运行什么样的query，不同的query会有不同的Schema优化方案。2.2节将介绍Query Design的优化。Schema设计同样受到预期数据集大小的影响。Schema设计时主要考虑：标准化，数据类型，索引。<br><br><font size=2>2.1.1 标准化</font><br><br>标准化是在数据库中组织数据的过程。其中包括，根据设计规则创建表并在这些表间建立关系；通过取消冗余度与不一致相关性，该设计规则可以同时保护数据并提高数据的灵活性。通常数据库标准化是让数据库设计符合某一级别的范式，通常满足第三范式即可。也有第四范式（也称为 Boyce Codd范式，BCNF)）与第五范式存在，但是在实际设计中很少考虑。忽视这些规则可能使得数据库的设计不太完美，但这不应影响功能。<br>标准化的特点：<br>
<blockquote>1) 所有的&#8220;对象&#8221;都在它自己的table中，没有冗余。<br>2) 数据库通常由E-R图生成。<br>3) 简洁，更新属性通常只需要更新很少的记录。<br>4) Join操作比较耗时。<br>5) Select，sort优化措施比较少。<br>6) 适用于OLTP应用。<br></blockquote>非标准化的特点：<br>
<blockquote>1) 在一张表中存储很多数据，数据冗余。<br>2) 更新数据开销很大，更新一个属性可能会更新很多表，很多记录。<br>3) 在删除数据是有可能丢失数据。<br>4) Select，order有很多优化的选择。<br>5) 适用于DSS应用。<br></blockquote><br>标准化和非标准化都有各自的优缺点，通常在一个数据库设计中可以混合使用，一部分表格标准化，一部分表格保留一些冗余数据：<br>
<blockquote>1) 对OLTP使用标准化，对DSS使用非标准化<br>2) 使用物化视图。MySQL不直接支持该数据库特性，但是可以用MyISAM表代替。<br>3) 冗余一些数据在表格中，例如将ref_id和name存在同一张表中。但是要注意更新问题。<br>4) 对于一些简单的对象，直接使用value作为建。例如IP address等<br>5) Reference by PRIMARY/UNIQUE KEY。MySQL可以优化这种操作，例如：<br>
<div class=code_title>java 代码</div>
<div class=dp-highlighter>
<ol class=dp-j>
    <li class=alt><span><span>select city_name </span></span>
    <li class=""><span>from city,state </span>
    <li class=alt><span>where state_id<span class=hilite2>=</span>state.id and state.code<span class=hilite2>=</span>&#8216;CA&#8217;&#8221; converted to &#8220;select city_name from city where state_id<span class=hilite2>=</span><span class=number>12</span><span> </span></span></li>
</ol>
</div>
<br></blockquote><font size=2>2.1.2 数据类型</font><br>最基本的优化之一就是使表在磁盘上占据的空间尽可能小。这能带来性能非常大的提升，因为数据小，磁盘读入较快，并且在查询过程中表内容被处理所占用的内存更少。同时，在更小的列上建索引，索引也会占用更少的资源。<br>可以使用下面的技术可以使表的性能更好并且使存储空间最小：<br>
<blockquote>1) 使用正确合适的类型，不要将数字存储为字符串。<br>2) 尽可能地使用最有效(最小)的数据类型。MySQL有很多节省磁盘空间和内存的专业化类型。<br>3) 尽可能使用较小的整数类型使表更小。例如，MEDIUMINT经常比INT好一些，因为MEDIUMINT列使用的空间要少25%。<br>4) 如果可能，声明列为NOT NULL。它使任何事情更快而且每列可以节省一位。注意如果在应用程序中确实需要NULL，应该毫无疑问使用它，只是避免 默认地在所有列上有它。<br>5) 对于MyISAM表，如果没有任何变长列(VARCHAR、TEXT或BLOB列)，使用固定尺寸的记录格式。这比较快但是不幸地可能会浪费一些空间。即使你已经用CREATE选项让VARCHAR列<span class=hilite1>ROW_FORMAT</span><span class=hilite2>=</span><span class=hilite3>fixed</span>，也可以提示想使用固定长度的行。<br>6) 使用sample character set，例如latin1。尽量少使用utf-8，因为utf-8占用的空间是latin1的3倍。可以在不需要使用utf-8的字段上面使用latin1，例如mail，url等。<br></blockquote><br><font size=2>2.1.3 索引</font><br>所有MySQL列类型可以被索引。对相关列使用索引是提高SELECT操作性能的最佳途径。使用索引应该注意以下几点：<br>
<blockquote>1) MySQL只会使用前缀，例如key(a, b) &#8230;where b<span class=hilite2>=</span>5 将使用不到索引。<br>2) 要选择性的使用索引。在变化很少的列上使用索引并不是很好，例如性别列。<br>3) 在Unique列上定义Unique index。<br>4) 避免建立使用不到的索引。<br>5) 在Btree index中（InnoDB使用Btree），可以在需要排序的列上建立索引。<br>6) 避免重复的索引。<br>7) 避免在已有索引的前缀上建立索引。例如：如果存在index（a，b）则去掉index（a）。<br>8) 控制单个索引的长度。使用key（name（8））在数据的前面几个字符建立索引。<br>9) 越是短的键值越好，最好使用integer。<br>10) 在查询中要使用到索引（使用explain查看），可以减少读磁盘的次数，加速读取数据。<br>11) 相近的键值比随机好。Auto_increment就比uuid好。<br>12) Optimize table可以压缩和排序index，注意不要频繁运行。<br>13) Analyze table可以更新数据。<br></blockquote><font size=2>2.2 Designing queries</font><br>查询语句的优化是一个Case by case的问题，不同的sql有不同的优化方案，在这里我只列出一些通用的技巧。<br>
<blockquote>1) 在有index的情况下，尽量保证查询使用了正确的index。可以使用EXPLAIN select &#8230;查看结果，分析查询。<br>2) 查询时使用匹配的类型。例如select * from a where id<span class=hilite2>=</span>5， 如果这里id是字符类型，同时有index，这条查询则使用不到index，会做全表扫描，速度会很慢。正确的应该是 &#8230; where id<span class=hilite2>=</span>&#8221;5&#8221; ，加上引号表明类型是字符。<br>3) 使用--log-slow-queries &#8211;long-query-time<span class=hilite2>=</span>2查看查询比较慢的语句。然后使用explain分析查询，做出优化。<br></blockquote><strong><font size=4>3. 服务器端优化<br></font></strong><font size=3>3.1 MySQL安装</font><br>MySQL有很多发行版本，最好使用MySQL AB发布的二进制版本。也可以下载源代码进行编译安装，但是编译器和类库的一些bug可能会使编译完成的MySQL存在潜在的问题。<br>如果安装MySQL的服务器使用的是Intel公司的处理器，可以使用intel c++编译的版本，在Linux World2005的一篇PPT中提到，使用intel C++编译器编译的MySQL查询速度比正常版本快30%左右。Intel c++编译版本可以在MySQL官方网站下载。<br><br><font size=3>3.2 服务器设置优化</font><br>MySQL默认的设置性能很差，所以要做一些参数的调整。这一节介绍一些通用的参数调整，不涉及具体的存储引擎（主要指MyISAM，InnoDB，相关优化在4中介绍）。<br>
<blockquote>--character-set：如果是单一语言使用简单的character set例如latin1。尽量少用Utf-8，utf-8占用空间较多。<br>--memlock：锁定MySQL只能运行在内存中，避免swapping，但是如果内存不够时有可能出现错误。<br>--max_allowed_packet：要足够大，以适应比较大的SQL查询，对性能没有太大影响，主要是避免出现packet错误。<br>--max_connections：server允许的最大连接。太大的话会出现out of memory。<br>--table_cache：MySQL在同一时间保持打开的table的数量。打开table开销比较大。一般设置为512。<br>--query_cache_size： 用于缓存查询的内存大小。<br>--datadir：mysql存放数据的根目录，和安装文件分开在不同的磁盘可以提高一点性能。<br></blockquote><strong><font size=4>4. 存储引擎优化</font></strong><br>MySQL支持不同的存储引擎，主要使用的有MyISAM和InnoDB。<br><br><font size=3>4.1 MyISAM</font><br>MyISAM管理非事务表。它提供高速存储和检索，以及全文搜索能力。MyISAM在所有MySQL配置里被支持，它是默认的存储引擎，除非配置MySQL默认使用另外一个引擎。<br><br><font size=2>4.1.1 MyISAM特性</font><br><font size=2>4.1.1.1 MyISAM Properties</font><br>
<blockquote>1) 不支持事务，宕机会破坏表<br>2) 使用较小的内存和磁盘空间<br>3) 基于表的锁，并发更新数据会出现严重性能问题<br>4) MySQL只缓存Index，数据由OS缓存<br></blockquote>4.1.1.2 Typical MyISAM usages<br>
<blockquote>1) 日志系统<br>2) 只读或者绝大部分是读操作的应用<br>3) 全表扫描<br>4) 批量导入数据<br>5) 没有事务的低并发读/写<br></blockquote>4.1.2 MyISAM优化要点<br>
<blockquote>1) 声明列为NOT NULL，可以减少磁盘存储。<br>2) 使用optimize table做碎片整理，回收空闲空间。注意仅仅在非常大的数据变化后运行。<br>3) Deleting/updating/adding大量数据的时候禁止使用index。使用ALTER TABLE t DISABLE KEYS。<br>4) 设置myisam_max_[extra]_sort_file_size足够大，可以显著提高repair table的速度。<br></blockquote>4.1.3 MyISAM Table Locks<br>
<blockquote>1) 避免并发insert，update。<br>2) 可以使用insert delayed，但是有可能丢失数据。<br>3) 优化查询语句。<br>4) 水平分区。<br>5) 垂直分区。<br>6) 如果都不起作用，使用InnoDB。<br></blockquote>4.1.4 MyISAM Key Cache<br>
<blockquote>1) 设置key_buffer_size variable。MyISAN最主要的cache设置，用于缓存MyISAM表格的index数据，该参数只对MyISAM有影响。通常在只使用MyISAM的Server中设置25-33%的内存大小。<br>2) 可以使用几个不同的Key Caches（对一些hot data）。<br>
<blockquote>a) SET GLOBAL test.key_buffer_size<span class=hilite2>=</span>512*1024;<br>b) CACHE INDEX t1.i1, t2.i1, t3 IN test;<br></blockquote>2) Preload index到Cache中可以提高查询速度。因为preloading index是顺序的，所以非常快。<br>
<blockquote>a) LOAD INDEX INTO CACHE t1, t2 IGNORE LEAVES；<br></blockquote></blockquote><br><font size=3>4.2 InnoDB<br></font>InnoDB给MySQL提供了具有提交，回滚和崩溃恢复能力的事务安全（ACID兼容）存储引擎。InnoDB提供row level lock，并且也在SELECT语句提供一个Oracle风格一致的非锁定读。这些特色增加了多用户部署和性能。没有在InnoDB中扩大锁定的需要，因为在InnoDB中row level lock适合非常小的空间。InnoDB也支持FOREIGN KEY约束。在SQL查询中，你可以自由地将InnoDB类型的表与其它MySQL的表的类型混合起来，甚至在同一个查询中也可以混合。<br>InnoDB是为在处理巨大数据量时获得最大性能而设计的。它的CPU使用效率非常高。<br>InnoDB存储引擎已经完全与MySQL服务器整合，InnoDB存储引擎为在内存中缓存数据和索引而维持它自己的缓冲池。 InnoDB存储它的表＆索引在一个表空间中，表空间可以包含数个文件（或原始磁盘分区）。这与MyISAM表不同，比如在MyISAM表中每个表被存在分离的文件中。InnoDB 表可以是任何大小，即使在文件尺寸被限制为2GB的操作系统上。<br>许多需要高性能的大型数据库站点上使用了InnoDB引擎。著名的Internet新闻站点Slashdot.org运行在InnoDB上。 Mytrix, Inc.在InnoDB上存储超过1TB的数据，还有一些其它站点在InnoDB上处理平均每秒800次插入/更新的负荷。<br><font size=2>4.2.1 InnoDB特性</font><br>4.2.1.1 InnoDB Properties<br>
<blockquote>1) 支持事务，ACID，外键。<br>2) Row level locks。 <br>3) 支持不同的隔离级别。<br>4) 和MyISAM相比需要较多的内存和磁盘空间。<br>5) 没有键压缩。<br>6) 数据和索引都缓存在内存hash表中。<br></blockquote>4.2.1.2 InnoDB Good For<br>
<blockquote>1) 需要事务的应用。<br>2) 高并发的应用。<br>3) 自动恢复。<br>4) 较快速的基于主键的操作。<br></blockquote>4.2.2 InnoDB优化要点<br>
<blockquote>1) 尽量使用short，integer的主键。<br>2) Load/Insert数据时按主键顺序。如果数据没有按主键排序，先排序然后再进行数据库操作。<br>3) 在Load数据是为设置SET UNIQUE_CHECKS<span class=hilite2>=</span>0，SET FOREIGN_KEY_CHECKS<span class=hilite2>=</span>0，可以避免外键和唯一性约束检查的开销。<br>4) 使用prefix keys。因为InnoDB没有key压缩功能。<br></blockquote>4.2.3 InnoDB服务器端设定<br>
<blockquote>innodb_buffer_pool_size：这是InnoDB最重要的设置，对InnoDB性能有决定性的影响。默认的设置只有8M，所以默认的数据库设置下面InnoDB性能很差。在只有InnoDB存储引擎的数据库服务器上面，可以设置60-80%的内存。更精确一点，在内存容量允许的情况下面设置比InnoDB tablespaces大10%的内存大小。<br><br>innodb_data_file_path：指定表数据和索引存储的空间，可以是一个或者多个文件。最后一个数据文件必须是自动扩充的，也只有最后一个文件允许自动扩充。这样，当空间用完后，自动扩充数据文件就会自动增长（以8MB为单位）以容纳额外的数据。例如： innodb_data_file_path<span class=hilite2>=</span>/disk1/ibdata1:900M;/disk2/ibdata2:50M:autoextend两个数据文件放在不同的磁盘上。数据首先放在ibdata1中，当达到900M以后，数据就放在ibdata2中。一旦达到50MB，ibdata2将以8MB为单位自动增长。如果磁盘满了，需要在另外的磁盘上面增加一个数据文件。<br><br>innodb_data_home_dir：放置表空间数据的目录，默认在mysql的数据目录，设置到和MySQL安装文件不同的分区可以提高性能。<br><br>innodb_log_file_size：该参数决定了recovery speed。太大的话recovery就会比较慢，太小了影响查询性能，一般取256M可以兼顾性能和recovery的速度<br>。<br>innodb_log_buffer_size：磁盘速度是很慢的，直接将log写道磁盘会影响InnoDB的性能，该参数设定了log buffer的大小，一般4M。如果有大的blob操作，可以适当增大。<br><br>innodb_flush_logs_at_trx_commit<span class=hilite2>=</span>2： 该参数设定了事务提交时内存中log信息的处理。<br>
<blockquote>1) <span class=hilite2>=</span>1时，在每个事务提交时，日志缓冲被写到日志文件，对日志文件做到磁盘操作的刷新。Truly ACID。速度慢。<br>2) <span class=hilite2>=</span>2时，在每个事务提交时，日志缓冲被写到文件，但不对日志文件做到磁盘操作的刷新。只有操作系统崩溃或掉电才会删除最后一秒的事务，不然不会丢失事务。<br>3) <span class=hilite2>=</span>0时， 日志缓冲每秒一次地被写到日志文件，并且对日志文件做到磁盘操作的刷新。任何mysqld进程的崩溃会删除崩溃前最后一秒的事务<br></blockquote>innodb_file_per_table：可以存储每个InnoDB表和它的索引在它自己的文件中。<br><br>transaction-isolation<span class=hilite2>=</span>READ-COMITTED: 如果应用程序可以运行在READ-COMMITED隔离级别，做此设定会有一定的性能提升。<br><br>innodb_flush_method： 设置InnoDB同步IO的方式：<br>
<blockquote>1) Default &#8211; 使用fsync（）。<br>2) O_SYNC 以sync模式打开文件，通常比较慢。<br>3) O_DIRECT，在Linux上使用Direct IO。可以显著提高速度，特别是在RAID系统上。避免额外的数据复制和double buffering（mysql buffering 和OS buffering）。<br></blockquote>innodb_thread_concurrency： InnoDB kernel最大的线程数。<br>
<blockquote>1) 最少设置为(num_disks+num_cpus)*2。<br>2) 可以通过设置成1000来禁止这个限制<br></blockquote></blockquote><strong><font size=4>5. 缓存</font></strong><br>缓存有很多种，为应用程序加上适当的缓存策略会显著提高应用程序的性能。由于应用缓存是一个比较大的话题，所以这一部分还需要进一步调研。 <br><br><strong><font size=4>6. Reference</font></strong><br>1) http://www.mysqlperformanceblog.com/<br>2) Advanced MySQL Performance Optimization, Peter Zaitsev, Tobias Asplund, MySQL Users Conference 2005<br>3) Improving MySQL Server Performance with Intel C++ Compiler，Peter Zaitsev，Linux World 2005<br>4) MySQL Performance Optimization, Peter Zaitsev, Percona Ltd, OPEN SOURCE DATABASE CONFERENCE 2006<br>5) MySQL Server Settings Tuning, Peter Zaitsev, co-founder, Percona Ltd, 2007<br>6) MySQL Reference Manual 
<img src ="http://www.cppblog.com/wmuu/aggbug/40838.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wmuu/" target="_blank">含笑半步癫</a> 2008-01-09 23:52 <a href="http://www.cppblog.com/wmuu/archive/2008/01/09/40838.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一些Informix SQL函数的用法</title><link>http://www.cppblog.com/wmuu/archive/2006/10/30/14381.html</link><dc:creator>含笑半步癫</dc:creator><author>含笑半步癫</author><pubDate>Mon, 30 Oct 2006 06:44:00 GMT</pubDate><guid>http://www.cppblog.com/wmuu/archive/2006/10/30/14381.html</guid><wfw:comment>http://www.cppblog.com/wmuu/comments/14381.html</wfw:comment><comments>http://www.cppblog.com/wmuu/archive/2006/10/30/14381.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wmuu/comments/commentRss/14381.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wmuu/services/trackbacks/14381.html</trackback:ping><description><![CDATA[
		<p>
				<br />一、内部函数 <br />　 1、内部合计函数 <br />　　　 1）COUNT（*）　　　　　　　　　　返回行数 <br />　　　 2）COUNT（DISTINCT COLNAME）　　 返回指定列中唯一值的个数 <br />　　　 3）SUM（COLNAME/EXPRESSION）　　 返回指定列或表达式的数值和； <br />　　　 4）SUM（DISTINCT COLNAME）　　　 返回指定列中唯一值的和 <br />　　　 5）AVG（COLNAME/EXPRESSION）　　 返回指定列或表达式中的数值平均值 <br />　　　 6）AVG（DISTINCT COLNAME）　　　 返回指定列中唯一值的平均值 <br />　　　 7）MIN（COLNAME/EXPRESSION）　　 返回指定列或表达式中的数值最小值 <br />　　　 8）MAX（COLNAME/EXPRESSION）　　 返回指定列或表达式中的数值最大值 <br />　 2、日期与时间函数 <br />　　　 1）DAY（DATE/DATETIME EXPRESSION）　　　返回指定表达式中的当月几号 <br />　　　 2）MONTH（DATE/DATETIME EXPRESSION）　　返回指定表达式中的月份 <br />　　　 3）YEAR（DATE/DATETIME EXPRESSION）　　 返回指定表达式中的年份 <br />　　　 4）WEEKDAY（DATE/DATETIME EXPRESSION）　返回指定表达式中的当周星期几 <br />　　　 5）DATE（NOT DATE EXPRESSION）　　　　　返回指定表达式代表的日期值 <br />　　　 6）TODAY　　　　　　　　　　　　　　　　返回当前日期的日期值 <br />　　　 7）CURRENT[first to last]　　　　　　　 返回当前日期的日期时间值 <br />　　　 8）COLNAME/EXPRESSION UNITS PRECISION　 返回指定精度的指定单位数 <br />　　　 9）MDY（MONTH，DAY，YEAR）　　　　　　　返回标识指定年、月、日的日期值 <br />　　　 10）DATETIME（DATE/DATETIME EXPRESSION）FIRST TO LAST 返回表达式代表的日期时间值 <br />　　　 11）INTERVAL（DATE/DATETIME EXPRESSION）FIRST TO LAST 返回表达式代表的时间间隔值 <br />　　　 12）EXTEND（DATE/DATETIME EXPRESSION，[first to last]）返回经过调整的日期或日期时间</p>
		<p>值 </p>
		<p>　　　 例子1、和UNITS合用，指定日期或时间单位(year,month,day,hour,minute,seond,fraction)： <br />　　　　　　　let tmp_date = today + 3 UNITS day <br />　　　 例子2、let tmp_date = MDY(10,30,2002)　　-- 2002-10-30 <br />　　　 例子3、let tmp_date = today + interval(7) day to day　--当前时间加上7天； <br />　　　　　　　注：该功能与1相似； <br />　　　 例子4、EXTEND转换日期或日期时间值 <br />　　　　　　　let tmp_inthour = extend(datetime1,hour to hour) </p>
		<p>　 3、代数函数 <br />　　　1）ABS(COLNAME/EXPRESSION)：　　　　　　 取绝对值 <br />　　　2）MOD（COLNAME/EXPRESSION，DIVISOR）　　返回除以除数后的模（余数） <br />　　　3）POW（COLNAME/EXPRESSION，EXPONENT）　 返回一个值的指数冥 <br />　　　　 例子：let tmp_float = pow(2,3)　--8.00000000 </p>
		<p>　　　4）ROOT（COLNAME/EXPRESSION，[index]）　 返回指定列或表达式的根值 </p>
		<p>　　　5）SQRT（COLNAME/EXPRESSION）　　　　　　返回指定列或表达式的平方根值 </p>
		<p>　　　6）ROUND（COLNAME/EXPRESSION，[factor]） 返回指定列或表达式的圆整化值 <br />　　　7）TRUNC（COLNAME/EXPRESSION，[factor]） 返回指定列或表达式的截尾值 <br />　　　　 说明：上两者中FACTOR指定小数位数，若不指定，则为0；若为负数，则整化到小数点左边； <br />　　　　 注：ROUND是在指定位上进行4舍5入；TRUNC是在指定位上直接截断； <br />　　　　　let tmp_float = round(4.555,2) --4.56 <br />　　　　　let tmp_float = trunc(4.555,2) --4.55 </p>
		<p>　 4、指数与对数函数 <br />　　　1）EXP（COLNAME/EXPRESSION）　　　　返回指定列或表达式的指数值 <br />　　　2）LOGN（COLNAME/EXPRESSION）　　　 返回指定列或表达式的自然对数值 <br />　　　3）LOG10（COLNAME/EXPRESSION）　　　返回指定列或表达式的底数位10的对数值 </p>
		<p>　 5、三角函数 <br />　　　1）COS（RADIAN EXPRESSION）　　　　　返回指定弧度表达式的余弦值 <br />　　　2）SIN（RADIAN EXPRESSION）　　　　　正弦 <br />　　　3）TAN（RADIAN EXPRESSION）　　　　　正切 <br />　　　4）ACOS（RADIAN EXPRESSION）　　　　 反余弦 <br />　　　5）ASIN（RADIAN EXPRESSION）　　　　 反正弦 <br />　　　6）ATAN（RADIAN EXPRESSION）　　　　 反正切 <br />　　　7）ATAN2（X，Y）　　　　　　　　　　 返回坐标（X，Y）的极坐标角度组件 </p>
		<p>　 6、统计函数 <br />　　　1）RANGE（COLNAME）　　　 返回指定列的最大值与最小值之差 = MAX（COLNAME）-MIN</p>
		<p>（COLNAME） <br />　　　2）VARIANCE（COLNAME）　　返回指定列的样本方差； <br />　　　3）STDEV（COLNAME）　　　 返回指定列的标准偏差； </p>
		<p>　 7、其他函数 <br />　　　1）USER　　　　　　　　　　　　　　返回当前用户名 <br />　　　2）HEX（COLNAME/EXPRESSION）　　　 返回指定列或表达式的十六进制值 <br />　　　3）LENGTH（COLNAME/EXPRESSION）　　返回指定字符列或表达式的长度 <br />　　　4）TRIM（COLNAME/EXPRESSION）　　　删除指定列或表达式前后的字符 <br />　　　5）COLNAME/EXPRESSION || COLNAME/EXPRESSION　返回并在一起的字符； </p>
		<p>二、IDS内部函数 <br />　 1、DBSERVERNAME　　　返回数据库服务器名　let tmp_char=DBSERVERNAME <br />　 2、SITENAME　　　　　返回数据库服务器名　let tmp_char=SITENAME <br />　　　说明：两者功能相同； </p>
		<p>　 3、DBINFO（‘SPECIAL_KEYWORD')　　　返回只关键字值 <br />　　　例子1：返回数据中每个表的DBSPACE名称 <br />　　　　 select dbinfo('dbspace',partnum),tabname from systables <br />　　　　　where tabid&gt;99 and tabtype='T'　(OK) <br />　　　例子2：返回任何表中插入的最后一个SERIAL值 <br />　　　　　select dbinfo('sqlca.sqlerrd1') from systables where tabid = 1 <br />　　　例子3：返回最后一个SELECT，INSERT，UPDATE，DELETE或EXECUTE PROCEDURE语句处理的行数； <br />　　　　　select dbinfo('sqlca.sqlerrd2') from systables where tabid=1; </p>
		<p>
				<br />
				<br /> </p>
		<h3>Date Manipulation</h3>
		<p>One of the more difficult concepts in Informix's handling of date and time values concerns the use of the variables in arithmetic or relational expressions. You can add or subtract DATE and DATETIME variables from each other. You can add or subtract an INTERVAL to a DATE or DATETIME. Table 1 shows the results of different types of operations on DATE and DATETIME values. </p>
		<p>
				<b>Table 1. Operations on DATE and DATETIME Variables</b>
		</p>
		<table cellspacing="1" cellpadding="5" border="1">
				<tbody>
						<tr>
								<td>
										<font size="2">
												<p>
														<b>First Operand</b>
												</p>
										</font>
								</td>
								<td>
										<font size="2">
												<b>Operation</b>
										</font>
								</td>
								<td>
										<font size="2">
												<p>
														<b>Second Operand</b>
												</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>
														<b>Result</b>
												</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>DATE</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">-</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">-</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>DATE</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>DATE</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">+-</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">-</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">+-</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">+</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">+-</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">-</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>CURRENT</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>CURRENT</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">-</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">+</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>CURRENT</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>CURRENT</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">+-</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">+-</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>UNITS</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>DATETIME</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">+-</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>UNITS</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
						</tr>
						<tr>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p style="TEXT-ALIGN: center" align="center">*/</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>NUMBER</p>
										</font>
								</td>
								<td>
										<font size="2">
												<p>INTERVAL</p>
										</font>
								</td>
						</tr>
				</tbody>
		</table>
		<p>Notice that it's always okay to subtract one DATE or DATETIME value from another, as shown here: <font size="3"><pre>CURRENT - "07/01/1950" = INTERVAL (my age)
"12/25/2000" – CURRENT = INTERVAL (how long till Xmas?)</pre><p>In such a case, the result is always an INTERVAL variable. It would make no sense to add two DATE or DATETIME values together. What could such an addition represent? </p><h3>UNITS Keyword</h3><p>When working with INTERVAL values, sometimes it is necessary to specify the precision with which you are dealing. For example, suppose you have the following field defined: <font size="3"></font></p><pre>lead_time INTERVAL DAY to DAY</pre><p>To add 10 days to the lead time you could use a SQL statement like this: <font size="3"></font></p><pre>SELECT lead_time + INTERVAL(10) DAY to DAY
FROM orders</pre><p>You could achieve the same results using the UNITS keyword: <font size="3"></font></p><pre>SELECT lead_time + 10 UNITS DAY 
FROM orders
</pre><p>Like most other programming languages, SQL often allows you to achieve the same ends with different statements. Sometimes the choice is one of personal style. Other times, one format fits in better with a structured style of code writing than another format does. </p><h3>Functions</h3><p>There are several built-in functions that affect date and time calculations. They can apply to either DATE or DATETIME values, with some exceptions. </p><h4>TODAY</h4><p>The TODAY function returns a DATE data value representing the current date. For example, you could execute a SQL function like this: <font size="3"></font></p><pre>UPDATE member SET change_date = TODAY
WHER member_number = 12345</pre><h4>CURRENT</h4><p>The CURRENT function is similar to the TODAY function, except it returns a DATETIME value. Without specific qualifiers, the default is YEAR to FRACTION(3). You can change the precision by using the same YEAR to FRACTION qualifiers as you use for DATETIMES. Thus, this would be legal: <font size="3"></font></p><pre>SELECT * from member
WHERE elapsed_time &lt; CURRENT YEAR to DAY</pre><h4>DATE</h4><p>The DATE function takes as input a non-DATE value such as CHAR, DATETIME, or INTEGER and returns the corresponding DATE value. For example, the following SQL translates a CHARACTER value to a DATE: <font size="3"></font></p><pre>SELECT * from member
WHERE enrollment_date &gt; DATE('01/01/99')</pre><h4>DAY</h4><p>This function returns an integer representing the day of the month. Here's an example: <font size="3"></font></p><pre>SELECT * from member
WHERE DAY(enrollment_date) &gt; DAY(CURRENT)</pre><h4>MONTH</h4><p>This performs like the DAY function except it returns an integer between 1 and 12 representing the month: <font size="3"></font></p><pre>SELECT * from member
WHERE enrollment_date &gt; MONTH('01/01/99')</pre><h4>WEEKDAY</h4><p>This returns an integer representing the day of the week, with 0 being Sunday and 6 being Saturday: <font size="3"></font></p><pre>SELECT * from member
WHERE WEEKDAY(enrollment_date) &gt; WEEKDAY(CURRENT)</pre><h4>YEAR</h4><p>This function is like the ones above, only it returns a four-digit integer representing the year. </p><h4>EXTEND</h4><p>This function allows you to use different precisions in a DATETIME than you have specified in the declaration of the variable. It uses the same FIRST to LAST syntax as the DATETIME variables. This function is used to adjust the precision of a DATETIME variable to match the precision of an INTERVAL that you are using in a calculation. If the INTERVAL value has fields that are not part of the DATETIME value that you are using in a calculation, use the EXTEND function to adjust the precision of the DATETIME. EXTEND can either increase or decrease the precision of a DATETIME, depending upon the FIRST and LAST values. </p><p>Suppose myvariable is declared as DATETIME YEAR to DAY. If you want to add or subtract an INTERVAL defined as MINUTE, you first have to extend the DATETIME as follows: <font size="3"><pre>SELECT EXTEND(myvariable, YEAR to MINUTE) – 
INTERVAL(5) MINUTE to MINUTE
FROM member</pre><p>The resulting value will be DATETIME YEAR to MINUTE. </p><h4>MDY</h4><p>The MDY function converts three-integer values into a DATE format. The first integer is the month and must evaluate to an integer in the range 1–12. The second integer is the day and must evaluate to a number in the range from 1 to however many days are in the particular month (28–31). The third expression is the year and must be a four-digit integer. Thus, the following MDY functions would each be valid: <font size="3"></font></p><pre>MDY(7,1,1950)</pre><p>returns a DATE of "07/01/50" <font size="3"></font></p><pre>MDY(MONTH(TODAY), 1, YEAR(TODAY))</pre><p>returns a DATE equal to the first day of the current month in the current year </p><p>Informix has extensive capabilities for manipulating dates and times, which can make for long and complex SQL statements. Using the three time-related data types and the time-related functions and keywords, you can accomplish almost any type of manipulation of time data. Unfortunately, getting there may be cryptic and painful. If you regularly do extensive date and time manipulation, you should understand all of the intricacies of these data structures. 
</p><p>Have fun! <br /></p></font></p></font><br /> </p>
<img src ="http://www.cppblog.com/wmuu/aggbug/14381.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wmuu/" target="_blank">含笑半步癫</a> 2006-10-30 14:44 <a href="http://www.cppblog.com/wmuu/archive/2006/10/30/14381.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>informix数据复制的资料</title><link>http://www.cppblog.com/wmuu/archive/2006/05/17/7290.html</link><dc:creator>含笑半步癫</dc:creator><author>含笑半步癫</author><pubDate>Tue, 16 May 2006 17:13:00 GMT</pubDate><guid>http://www.cppblog.com/wmuu/archive/2006/05/17/7290.html</guid><wfw:comment>http://www.cppblog.com/wmuu/comments/7290.html</wfw:comment><comments>http://www.cppblog.com/wmuu/archive/2006/05/17/7290.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wmuu/comments/commentRss/7290.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wmuu/services/trackbacks/7290.html</trackback:ping><description><![CDATA[ibm网页上的信息可以用一个字来形容，乱！就像杂货堆。在ibm上找东西，经常是第一次找到，第二次就找不到了（迷路了）。<br /><a href="http://www.dvbbs.net/tech/data/2006042553131.asp">http://www.dvbbs.net/tech/data/2006042553131.asp</a> 远程数据复制<br /><a href="http://fanqiang.chinaunix.net/db/informix/2001-06-12/1402.shtml">http://fanqiang.chinaunix.net/db/informix/2001-06-12/1402.shtml</a> Informix动态服务器配置--6.相关基本概念<br /><a href="http://fanqiang.chinaunix.net/db/informix/2005-09-07/3624.shtml">http://fanqiang.chinaunix.net/db/informix/2005-09-07/3624.shtml</a> INFORMIX－ONLINE数据库三种备份方法的选择<br /><a href="http://fanqiang.chinaunix.net/db/informix/2005-11-14/3842.shtml">http://fanqiang.chinaunix.net/db/informix/2005-11-14/3842.shtml</a> Infomix数据库的备份与恢复<br /><a href="http://www.linuxmine.com/3348.html">http://www.linuxmine.com/3348.html</a>      INFORMIX 的数据复制技术<br /><br /><a href="http://www.pcvz.com/Program/Database/Oracle/">http://www.pcvz.com/Program/Database/Oracle/</a> 有关Oracle的<br /><br /><img src ="http://www.cppblog.com/wmuu/aggbug/7290.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wmuu/" target="_blank">含笑半步癫</a> 2006-05-17 01:13 <a href="http://www.cppblog.com/wmuu/archive/2006/05/17/7290.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>