MySQL 面试基础

    <div class="clear"></div>
    <div class="postBody">
        <div class="blogpost-body" id="cnblogs_post_body">1、如何登陆my<a style="color: rgb(51, 51, 51); font-size: 14px; text-decoration: none;" href="http://www.2cto.com/database/" target="_blank">sql数据库</a></p><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">mysql -u username -p</p><h2>2、如何开启/关闭mysql服务</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">service mysql start/stop</p><h2>3、查看mysql的状态</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">service mysql status</p><h2>4、如何显示数所有数据库</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">show databases</p><h2>5、如何获取表内所有字段对象的名称和类型</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">describe table_name;</p><h2>6、MYSQL支持事务吗?</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">在缺省模式下,MYSQL是autocommit模式的,所有的数据库更新操作都会即时提交,所以在缺省情况下,mysql是不支持事务的。<br>但是如果你的MYSQL表类型是使用InnoDB Tables 或 BDB tables的话,你的MYSQL就可以使用事务处理,使用SET AUTOCOMMIT=0就可以使MYSQL允许在非autocommit模式,在非autocommit模式下,你必须使用COMMIT来提交你的更改,或者用ROLLBACK来回滚你的更改。<br>示例如下:<br>START TRANSACTION;<br>SELECT @A:=SUM(salary) FROM table1 WHERE type=1;<br>UPDATE table2 SET summmary=@A WHERE type=1;<br>COMMIT;</p><h2>7、MYSQL相比于其他数据库有哪些特点?</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">MySQL是一个小型关系型数据库管理<a style="color: rgb(51, 51, 51); text-decoration: none;" href="http://www.2cto.com/os/" target="_blank">系统</a>,开发者为瑞典MySQL AB公司,现在已经被Sun公司收购,支持FreeBSD、<a style="color: rgb(51, 51, 51); text-decoration: none;" href="http://www.2cto.com/os/linux/" target="_blank">Linux</a>、MAC、Windows等多种操作系统与其他的大型数据库例如<a style="color: rgb(51, 51, 51); text-decoration: none;" href="http://www.2cto.com/database/Oracle/" target="_blank">Oracle</a>、<a style="color: rgb(51, 51, 51); text-decoration: none;" href="http://www.2cto.com/database/DB2/" target="_blank">DB2</a>、SQL Server等相比功能稍弱一些<br>1、可以处理拥有上千万条记录的大型数据<br>2、支持常见的SQL语句规范<br>3、可移植行高,安装简单小巧<br>4、良好的运行效率,有丰富信息的网络支持<br>5、调试、管理,优化简单(相对其他大型数据库)</p><h2>8、varchar和char的区别</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">Char是一种固定长度的类型,varchar是一种可变长度的类型</p><h2>9、数据库事物有哪几种?</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">隔离性、持续性、一致性、原子性</p><h2>10、请简洁地描述下MySQL中InnoDB支持的四种事务隔离级别名称,以及逐级之间的区别?</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">SQL标准定义的四个隔离级别为:<br>read uncommited:读取未提交内容<br>read committed:读取提交内容<br>repeatable read:可重读<br>serializable:可串行化<br>详细解释如下:<br>Read Uncommitted(读取未提交内容)<br>在该隔离级别,所有事务都可以看到其他未提交事务的执行结果。本隔离级别很少用于实际应用,因为它的性能也不比其他级别好多少。读取未提交的数据,也被称之为脏读(Dirty Read)。<br>Read Committed(读取提交内容)<br>这是大多数数据库系统的默认隔离级别(但不是MySQL默认的)。它满足了隔离的简单定义:一个事务只能看见已经提交事务所做的改变。这种隔离级别也支持所谓的不可重复读(Nonrepeatable Read),因为同一事务的其他实例在该实例处理其间可能会有新的commit,所以同一select可能返回不同结果。<br>Repeatable Read(可重读)<br>这是MySQL的默认事务隔离级别,它确保同一事务的多个实例在并发读取数据时,会看到同样的数据行。不过理论上,这会导致另一个棘手的问题:幻读(Phantom Read)。简单的说,幻读指当用户读取某一范围的数据行时,另一个事务又在该范围内插入了新行,当用户再读取该范围的数据行时,会发现有新的“幻影” 行。InnoDB和Falcon存储引擎通过多版本并发控制(MVCC,Multiversion Concurrency Control 间隙锁)机制解决了该问题。注:其实多版本只是解决不可重复读问题,而加上间隙锁(也就是它这里所谓的并发控制)才解决了幻读问题。<br>Serializable(可串行化)<br>这是最高的隔离级别,它通过强制事务排序,使之不可能相互冲突,从而解决幻读问题。简言之,它是在每个读的数据行上加上共享锁。在这个级别,可能导致大量的超时现象和锁竞争。<br>对于不同的事务,采用不同的隔离级别分别有不同的结果。不同的隔离级别有不同的现象。主要有下面3种现在:<br>1、脏读(dirty read):一个事务可以读取另一个尚未提交事务的修改数据。<br>2、非重复读(nonrepeatable read):在同一个事务中,同一个查询在T1时间读取某一行,在T2时间重新读取这一行时候,这一行的数据已经发生修改,可能被更新了(update),也可能被删除了(delete)。<br>3、幻像读(phantom read):在同一事务中,同一查询多次进行时候,由于其他插入操作(insert)的事务提交,导致每次返回不同的结果集。<br>不同的隔离级别有不同的现象,并有不同的锁定/并发机制,隔离级别越高,数据库的并发性就越差,4种事务隔离级别分别表现的现象如下表:<br><img title="" style="border- 0px; list-style: none; margin: 0px auto; padding: 0px;  619px; height: 162px; display: block;" alt="这里写图片描述" src="http://www.2cto.com/uploadfile/Collfiles/20150716/20150716102059356.png"></p><h2>11、mysql数据库引擎MyISAM和InnoDB的区别</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);"><img title="" style="border- 0px; list-style: none; margin: 0px auto; padding: 0px;  305px; height: 194px; display: block;" alt="这里写图片描述" src="http://www.2cto.com/uploadfile/Collfiles/20150716/20150716102059357.png"></p><h2>12、mysql有关权限的表都有哪几个</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">MySQL服务器通过权限表来控制用户对数据库的访问,权限表存放在mysql数据库里,由mysql_install_db脚本初始化。这些权限表分别user,db,table_priv,columns_priv和host。下面分别介绍一下这些表的结构和内容:<br>user权限表:记录允许连接到服务器的用户帐号信息,里面的权限是全局级的。<br>db权限表:记录各个帐号在各个数据库上的操作权限。<br>table_priv权限表:记录数据表级的操作权限。<br>columns_priv权限表:记录数据列级的操作权限。<br>host权限表:配合db权限表对给定主机上数据库级操作权限作更细致的控制。这个权限表不受GRANT和REVOKE语句的影响。</p><h2>13、mysql存储引擎有哪些?如何修改mysql存储引擎?</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">MyISAM indexed sequential access method (有索引的顺序访问方法)<br>MyISAM 具有检查和修复表格的大多数工具。表格可以被压缩,而且支持全文收索<br>不是事务安全的,而且不支持外键。<br>MEMORY 也是以前的(HEAP) 该类型表存储在内存中,表的索引是哈希分布的。<br>merge 这些表为了查询目的,把myisam 表集合作为单个表,因此你可以在某些操作系统中避开最大文件大小的限制。<br>archive 这种类型的表只支持,insert ,select 不支持delete,update,replace ,不使用索引。<br>csv 这些表保存在服务器的单个文件中,它包含了用逗号间隔的数据。</p><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">innodb 这种表是事务安全的。提供了commit(提交) rollback(实务回滚)支持外键,比myisam慢。<br>修改mysql存储引擎alter table tablename type = innodb;</p><h2>14、MYSQL 数据表修复及数据恢复面试题</h2><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">MYSQL数据表在什么情况下容易损坏?</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">服务器突然断电导致数据文件损坏。</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">强制关机,没有先关闭mysql 服务等。 数据表损坏后的主要现象是什么?</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">从表中选择数据之时,得到如下错误:Incorrect key file for table: ‘…’. Try to repair it</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">查询不能在表中找到行或返回不完全的数据。</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">Error: Table ‘p’ is marked as crashed and should be repaired 。</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">打开表失败: Can’t open file: ‘×××.MYI’ (errno: 145) 。 数据表损坏的修复方式有哪些?</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">使用 myisamchk 来修复,具体步骤:</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">1)修复前将mysql服务停止。</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">2)打开命令行方式,然后进入到mysql的/bin目录。</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">3)执行myisamchk –recover 数据库所在路径/*.MYI</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">使用repair table 或者 OPTIMIZE table命令来修复,REPAIR TABLE table_name 修复表 OPTIMIZE TABLE table_name 优化表 REPAIR TABLE 用于修复被破坏的表。</span><br style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);"><span style="color: rgb(51, 51, 51); font-family: 宋体; background-color: rgb(249, 249, 249);">OPTIMIZE TABLE 用于回收闲置的数据库空间,当表上的数据行被删除时,所占据的磁盘空间并没有立即被回收,使用了OPTIMIZE TABLE命令后这些空间将被回收,并且对磁盘上的数据行进行重排(注意:是磁盘上,而非数据库)</span><h2>15、MYSQL数据库服务器性能分析的方法命令有哪些?</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">Show status<br>一些值得监控的变量值:<br>Bytes_received和Bytes_sent<br>和服务器之间来往的流量。<br>Com_*服务器正在执行的命令。<br>Created_*在查询执行期限间创建的临时表和文件。<br>Handler_*存储引擎操作。<br>Select_*不同类型的联接执行计划。<br>Sort_*几种排序信息。<br>Show session status like ‘Select’;<br>Show profiles<br>SET profiling=1;<br>Show profilesG<br>Show profile;</p><h2>16、 mysql里记录货币用什么字段类型好</h2><p style="border- 0px; list-style: none; margin: 0px 0px 8px; padding: 0px; color: rgb(51, 51, 51); text-indent: 2em; font-family: 宋体; background-color: rgb(249, 249, 249);">NUMERIC和DECIMAL类型被MySQL实现为同样的类型,这在SQL92标准允许。他们被用于保存值,该值的准确精度是极其重要的值,例如与金钱有关的数据。当声明一个类是这些类型之一时,精度和规模的能被(并且通常是)指定;例如:<br>salary DECIMAL(9,2)<br>在这个例子中,9(precision)代表将被用于存储值的总的小数位数,而2(scale)代表将被用于存储小数点后的位数。因此,在这种情况下,能被存储在salary列中的值的范围是从-9999999.99到9999999.99。在ANSI/ISO SQL92中,句法DECIMAL(p)等价于DECIMAL(p,0)。同样,句法DECIMAL等价于DECIMAL(p,0),这里实现被允许决定值p。MySQL当前不支持DECIMAL/NUMERIC数据类型的这些变种形式的任一种。这一般说来不是一个严重的问题,因为这些类型的主要益处得自于明显地控制精度和规模的能力。<br>DECIMAL和NUMERIC值作为字符串存储,而不是作为二进制浮点数,以便保存那些值的小数精度。一个字符用于值的每一位、小数点(如果scale&gt;0)和“-”符号(对于负值)。如果scale是0,DECIMAL和NUMERIC值不包含小数点或小数部分。<br>DECIMAL和NUMERIC值得最大的范围与DOUBLE一样,但是对于一个给定的DECIMAL或NUMERIC列,实际的范围可由制由给定列的precision或scale限制。当这样的列赋给了小数点后面的位超过指定scale所允许的位的值,该值根据scale四舍五入。当一个DECIMAL或NUMERIC列被赋给了其大小超过指定(或缺省的)precision和scale隐含的范围的值,MySQL存储表示那个范围的相应的端点值。</p><p>&nbsp;</p><p>&nbsp;</p><div style="display: inline-block;"></div><p>&nbsp;</p><h1 style="color: rgb(68, 68, 68); line-height: 1.5; font-family: YoonGothic, AppleGothic; font-size: 28px; background-color: rgb(255, 255, 255);">2.数据库事务的四个特性及含义</h1><p>&nbsp;</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">数据库事务transanction正确执行的四个基本要素。ACID,原子性(Atomicity)、一致性(Correspondence)、隔离性(Isolation)、持久性(Durability)。<br><strong>原子性</strong>:整个事务中的所有操作,要么全部完成,要么全部不完成,不可能停滞在中间某个环节。事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这个事务从来没有执行过一样。<br><strong>一致性</strong>:在事务开始之前和事务结束以后,数据库的完整性约束没有被破坏。<br><strong>隔离性</strong>:隔离状态执行事务,使它们好像是<a style="color: rgb(102, 0, 204); text-decoration: none;" href="http://www.2cto.com/os/" target="_blank">系统</a>在给定时间内执行的唯一操作。如果有两个事务,运行在相同的时间内,执行 相同的功能,事务的隔离性将确保每一事务在系统中认为只有该事务在使用系统。这种属性有时称为串行化,为了防止事务操作间的混淆,必须串行化或序列化请 求,使得在同一时间仅有一个请求用于同一数据。<br><strong>持久性</strong>:在事务完成以后,该事务所对数据库所作的更改便持久的保存在数据库之中,并不会被回滚。</p><h1>3.视图的作用,视图可以更改么?</h1><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">视图是虚拟的表,与包含数据的表不一样,视图只包含使用时动态检索数据的查询;不包含任何列或数据。使用视图可以简化复杂的sql操作,隐藏具体的细节,保护数据;视图创建后,可以使用与表相同的方式利用它们。<br>视图不能被索引,也不能有关联的触发器或默认值,如果视图本身内有order by 则对视图再次order by将被覆盖。<br>创建视图:create view XXX as XXXXXXXXXXXXXX;<br>对于某些视图比如未使用联结子查询分组聚集函数Distinct Union等,是可以对其更新的,对视图的更新将对基表进行更新;但是视图主要用于简化检索,保护数据,并不用于更新,而且大部分视图都不可以更新。</p><h1>4.drop,delete与truncate的区别</h1><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">drop直接删掉表 truncate删除表中数据,再插入时自增长id又从1开始 delete删除表中数据,可以加where字句。</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(1) DELETE语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作。TRUNCATE TABLE 则一次性地从表中删除所有的数据并不把单独的删除操作记录记入日志保存,删除行是不能恢复的。并且在删除的过程中不会激活与表有关的删除触发器。执行速度快。</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(2) 表和索引所占空间。当表被TRUNCATE 后,这个表和索引所占用的空间会恢复到初始大小,而DELETE操作不会减少表或索引所占用的空间。drop语句将表所占用的空间全释放掉。</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(3) 一般而言,drop &gt; truncate &gt; delete</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(4) 应用范围。TRUNCATE 只能对TABLE;DELETE可以是table和view</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(5) TRUNCATE 和DELETE只删除数据,而DROP则删除整个表(结构和数据)。</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(6) truncate与不带where的delete :只删除数据,而不删除表的结构(定义)drop语句将删除表的结构被依赖的约束(constrain),触发器(trigger)索引(index);依赖于该表的存储过程/函数将被保留,但其状态会变为:invalid。</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(7) delete语句为DML(data maintain Language),这个操作会被放到 rollback segment中,事务提交后才生效。如果有相应的 tigger,执行的时候将被触发。</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(8) truncate、drop是DLL(data define language),操作立即生效,原数据不放到 rollback segment中,不能回滚</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(9) 在没有备份情况下,谨慎使用 drop 与 truncate。要删除部分数据行采用delete且注意结合where来约束影响范围。回滚段要足够大。要删除表用drop;若想保留表而将表中数据删除,如果于事务无关,用truncate即可实现。如果和事务有关,或老师想触发trigger,还是用delete。</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(10) Truncate table 表名 速度快,而且效率高,因为:<br>truncate table 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行。但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源少。DELETE 语句每次删除一行,并在事务日志中为所删除的每行记录一项。TRUNCATE TABLE 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放。</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(11) TRUNCATE TABLE 删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子。如果想保留标识计数值,请改用 DELETE。如果要删除表定义及其数据,请使用 DROP TABLE 语句。</p><p align="left" style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">(12) 对于由 FOREIGN KEY 约束引用的表,不能使用 TRUNCATE TABLE,而应使用不带 WHERE 子句的 DELETE 语句。由于 TRUNCATE TABLE 不记录在日志中,所以它不能激活触发器。</p><h1>5.索引的工作原理及其种类</h1><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);"><strong>数据库索引</strong>,是数据库管理系统中一个排序的数据结构,以协助快速查询、更新数据库表中数据。<strong>索引的实现通常使用B树及其变种B+树</strong>。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种方式引用(指向)数据,这样就可以在这些数据结构上实现高级查找算法。这种数据结构,就是索引。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">为表设置索引要付出代价的:一是增加了数据库的存储空间,二是在插入和修改数据时要花费较多的时间(因为索引也要随之变动)。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);"><iframe id="iframe_0.49641537941153047" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.2cto.com/uploadfile/Collfiles/20150416/2015041610033731.png?_=5930743%22%20style=%22border:none;max-630px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.49641537941153047',img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style=" 588px; height: 297px;"></iframe></p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">&nbsp;</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">图展示了一种可能的索引方式。左边是数据表,一共有两列七条记录,最左边的是数据记录的物理地址(注意逻辑上相邻的记录在磁盘上也并不是一定物理相邻的)。为了加快Col2的查找,可以维护一个右边所示的二叉查找树,每个节点分别包含索引键值和一个指向对应数据记录物理地址的指针,这样就可以运用二叉查找在O(log<sub>2</sub>n)的复杂度内获取到相应数据。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">创建索引可以大大提高系统的性能。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第一,通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第二,可以大大加快数据的检索速度,这也是创建索引的最主要的原因。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第三,可以加速表和表之间的连接,特别是在实现数据的参考完整性方面特别有意义。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第四,在使用分组和排序子句进行数据检索时,同样可以显著减少查询中分组和排序的时间。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第五,通过使用索引,可以在查询的过程中,使用优化隐藏器,提高系统的性能。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">也许会有人要问:增加索引有如此多的优点,为什么不对表中的每一个列创建一个索引呢?因为,增加索引也有许多不利的方面。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第一,创建索引和维护索引要耗费时间,这种时间随着数据量的增加而增加。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第二,索引需要占物理空间,除了数据表占数据空间之外,每一个索引还要占一定的物理空间,如果要建立聚簇索引,那么需要的空间就会更大。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第三,当对表中的数据进行增加、删除和修改的时候,索引也要动态的维护,这样就降低了数据的维护速度。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">索引是建立在数据库表中的某些列的上面。在创建索引的时候,应该考虑在哪些列上可以创建索引,在哪些列上不能创建索引。<strong>一般来说,应该在这些列上创建索引:</strong>在经常需要搜索的列上,可以加快搜索的速度;在作为主键的列上,强制该列的唯一性和组织表中数据的排列结构;在经常用在连接的列上,这些列主要是一些外键,可以加快连接的速度;在经常需要根据范围进行搜索的列上创建索引,因为索引已经排序,其指定的范围是连续的;在经常需要排序的列上创建索引,因为索引已经排序,这样查询可以利用索引的排序,加快排序查询时间;在经常使用在WHERE子句中的列上面创建索引,加快条件的判断速度。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">同样,对于有些列不应该创建索引。<strong>一般来说,不应该创建索引的的这些列具有下列特点:</strong></p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第一,对于那些在查询中很少使用或者参考的列不应该创建索引。这是因为,既然这些列很少使用到,因此有索引或者无索引,并不能提高查询速度。相反,由于增加了索引,反而降低了系统的维护速度和增大了空间需求。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第二,对于那些只有很少数据值的列也不应该增加索引。这是因为,由于这些列的取值很少,例如人事表的性别列,在查询的结果中,结果集的数据行占了表中数据行的很大比例,即需要在表中搜索的数据行的比例很大。增加索引,并不能明显加快检索速度。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第三,对于那些定义为text, image和bit数据类型的列不应该增加索引。这是因为,这些列的数据量要么相当大,要么取值很少。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">第四,当修改性能远远大于检索性能时,不应该创建索引。这是因为,<strong>修改性能和检索性能是互相矛盾的</strong>。当增加索引时,会提高检索性能,但是会降低修改性能。当减少索引时,会提高修改性能,降低检索性能。因此,当修改性能远远大于检索性能时,不应该创建索引。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">根据数据库的功能,可以在<a style="color: rgb(102, 0, 204); text-decoration: none;" href="http://www.2cto.com/database/" target="_blank">数据库设计</a>器中创建三种索引:<strong>唯一索引、主键索引和聚集索引</strong>。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);"><strong>唯一索引</strong></p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">唯一索引是不允许其中任何两行具有相同索引值的索引。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">当现有数据中存在重复的键值时,大多数数据库不允许将新创建的唯一索引与表一起保存。数据库还可能防止添加将在表中创建重复键值的新数据。例如,如果在employee表中职员的姓(lname)上创建了唯一索引,则任何两个员工都不能同姓。&nbsp;<strong>主键索引</strong>&nbsp;数据库表经常有一列或列组合,其值唯一标识表中的每一行。该列称为表的主键。 在数据库关系图中为表定义主键将自动创建主键索引,主键索引是唯一索引的特定类型。该索引要求主键中的每个值都唯一。当在查询中使用主键索引时,它还允许对数据的快速访问。&nbsp;<strong>聚集索引</strong>&nbsp;在聚集索引中,表中行的物理顺序与键值的逻辑(索引)顺序相同。<strong>一个表只能包含一个聚集索引。</strong></p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">如果某索引不是聚集索引,则表中行的物理顺序与键值的逻辑顺序不匹配。<strong>与非聚集索引相比,聚集索引通常提供更快的数据访问速度。</strong></p><h3>局部性原理与磁盘预读</h3><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">由于存储介质的特性,磁盘本身存取就比主存慢很多,再加上机械运动耗费,磁盘的存取速度往往是主存的几百分分之一,因此为了提高效率,要尽量减少磁盘I/O。为了达到这个目的,磁盘往往不是严格按需读取,而是每次都会预读,即使只需要一个字节,磁盘也会从这个位置开始,顺序向后读取一定长度的数据放入内存。这样做的理论依据是计算机科学中著名的<strong>局部性原理</strong>:<strong>当一个数据被用到时,其附近的数据也通常会马上被使用。程序运行期间所需要的数据通常比较集中。</strong></p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">由于磁盘顺序读取的效率很高(不需要寻道时间,只需很少的旋转时间),因此对于具有局部性的程序来说,预读可以提高I/O效率。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">预读的长度一般为页(page)的整倍数。页是计算机管理存储器的逻辑块,硬件及操作系统往往将主存和磁盘存储区分割为连续的大小相等的块,每个存储块称为一页(在许多操作系统中,页得大小通常为4k),主存和磁盘以页为单位交换数据。当程序要读取的数据不在主存中时,会触发一个缺页异常,此时系统会向磁盘发出读盘信号,磁盘会找到数据的起始位置并向后连续读取一页或几页载入内存中,然后异常返回,程序继续运行。</p><h3>B-/+Tree索引的性能分析</h3><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">到这里终于可以分析B-/+Tree索引的性能了。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">上文说过一般使用磁盘I/O次数评价索引结构的优劣。先从B-Tree分析,根据B-Tree的定义,可知检索一次最多需要访问h个节点。数据库系统的设计者巧妙利用了磁盘预读原理,将一个节点的大小设为等于一个页,这样每个节点只需要一次I/O就可以完全载入。为了达到这个目的,在实际实现B-Tree还需要使用如下技巧:</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">每次新建节点时,直接申请一个页的空间,这样就保证一个节点物理上也存储在一个页里,加之计算机存储分配都是按页对齐的,就实现了一个node只需一次I/O。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);"><strong>B-Tree中一次检索最多需要h-1次I/O(根节点常驻内存),渐进复杂度为O(h)=O(log<sub>d</sub>N)。</strong>一般实际应用中,出度d是非常大的数字,通常超过100,因此h非常小(通常不超过3)。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">而红黑树这种结构,h明显要深的多。由于逻辑上很近的节点(父子)物理上可能很远,无法利用局部性,所以红黑树的I/O渐进复杂度也为O(h),效率明显比B-Tree差很多。</p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);"><strong>综上所述,用B-Tree作为索引结构效率是非常高的。</strong></p><h1>6.连接的种类</h1><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">查询分析器中执行:<br>--建表table1,table2:<br>create table table1(id int,name varchar(10))<br>create table table2(id int,score int)<br>insert into table1 select 1,'lee'<br>insert into table1 select 2,'zhang'<br>insert into table1 select 4,'wang'<br>insert into table2 select 1,90<br>insert into table2 select 2,100<br>insert into table2 select 3,70<br>如表<br>-------------------------------------------------<br>table1 | table2 |<br>-------------------------------------------------<br>id name |id score |<br>1 lee |1 90|<br>2 zhang| 2 100|<br>4 wang| 3 70|<br>-------------------------------------------------<br><br>以下均在查询分析器中执行<br>一、外连接<br>1.概念:包括左向外联接、右向外联接或完整外部联接<br><br>2.左连接:left join 或 left outer join<br>(1)左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)。<br>(2)sql 语句<br>select * from table1 left join table2 on table1.id=table2.id<br>-------------结果-------------<br>idnameidscore<br>------------------------------<br>1lee190<br>2zhang2100<br>4wangNULLNULL<br>------------------------------<br>注释:包含table1的所有子句,根据指定条件返回table2相应的字段,不符合的以null显示<br><br>3.右连接:right join 或 right outer join<br>(1)右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。<br>(2)sql 语句<br>select * from table1 right join table2 on table1.id=table2.id<br>-------------结果-------------<br>idnameidscore<br>------------------------------<br>1lee190<br>2zhang2100<br>NULLNULL370<br>------------------------------<br>注释:包含table2的所有子句,根据指定条件返回table1相应的字段,不符合的以null显示<br><br>4.完整外部联接:full join 或 full outer join<br>(1)完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。<strong>如果表之间有匹配行,则整个结果集行包含基表的数据值。</strong><br>(2)sql 语句<br>select * from table1 full join table2 on table1.id=table2.id<br>-------------结果-------------<br>idnameidscore<br>------------------------------<br>1lee190<br>2zhang2100<br>4wangNULLNULL<br>NULLNULL370<br>------------------------------<br><strong>注释:返回左右连接的和(见上左、右连接)</strong><br><br>二、内连接<br>1.概念:内联接是用比较运算符比较要联接列的值的联接<br><br>2.内连接:join 或 inner join<br><br>3.sql 语句<br>select * from table1 join table2 on table1.id=table2.id<br>-------------结果-------------<br>idnameidscore<br>------------------------------<br>1lee190<br>2zhang2100<br>------------------------------<br>注释:<strong>只返回符合条件的table1和table2的列</strong><br><br>4.等价(与下列执行效果相同)<br>A:select a.*,b.* from table1 a,table2 b where a.id=b.id<br>B:select * from table1 cross join table2 where table1.id=table2.id (<strong>注:cross join后加条件只能用where,不能用on</strong>)<br><br>三、交叉连接<strong>(完全)</strong><br><br>1.概念:没有 WHERE 子句的交叉联接将产生联接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。(table1和table2交叉连接产生3*3=9条记录)<br><br>2.交叉连接:<strong>cross join (不带条件where...)</strong><br><br>3.sql语句<br>select * from table1 cross join table2<br>-------------结果-------------<br>idnameidscore<br>------------------------------<br>1lee190<br>2zhang190<br>4wang190<br>1lee2100<br>2zhang2100<br>4wang2100<br>1lee370<br>2zhang370<br>4wang370<br>------------------------------<br>注释:返回3*3=9条记录,即笛卡尔积<br><br>4.等价(与下列执行效果相同)<br>A:select * from table1,table2</p><h1>7.数据库范式</h1><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);"><strong>1 第一范式(1NF)</strong><br><br>在任何一个关系数据库中,第一范式(1NF)是对关系模式的基本要求,不满足第一范式(1NF)的数据库就不是关系数据库。<br>所谓第一范式(1NF)是指数据库表的每一列都是不可分割的基本数据项,同一列中不能有多个值,即实体中的某个属性不能有多个值或者不能有重复的属性。如果出现重复的属性,就可能需要定义一个新的实体,新的实体由重复的属性构成,新实体与原实体之间为一对多关系。在第一范式(1NF)中表的每一行只包含一个实例的信息。简而言之,<strong>第一范式就是无重复的列。</strong></p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);"><strong>2 第二范式(2NF)</strong><br><br>第二范式(2NF)是在第一范式(1NF)的基础上建立起来的,即满足第二范式(2NF)必须先满足第一范式(1NF)。第二范式(2NF)要求数据库表中的每个实例或行必须可以被惟一地区分。为实现区分通常需要为表加上一个列,以存储各个实例的惟一标识。这个惟一属性列被称为主关键字或主键、主码。<br>第二范式(2NF)要求实体的属性完全依赖于主关键字。所谓完全依赖是指不能存在仅依赖主关键字一部分的属性,如果存在,那么这个属性和主关键字的这一部分应该分离出来形成一个新的实体,新实体与原实体之间是一对多的关系。为实现区分通常需要为表加上一个列,以存储各个实例的惟一标识。简而言之,<strong>第二范式就是非主属性非部分依赖于主关键字。</strong></p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);"><strong>3 第三范式(3NF)</strong><br><br>满足第三范式(3NF)必须先满足第二范式(2NF)。简而言之,第三范式(3NF)要求一个数据库表中不包含已在其它表中已包含的非主关键字信息。例如,存在一个部门信息表,其中每个部门有部门编号(dept_id)、部门名称、部门简介等信息。那么在员工信息表中列出部门编号后就不能再将部门名称、部门简介等与部门有关的信息再加入员工信息表中。如果不存在部门信息表,则根据第三范式(3NF)也应该构建它,否则就会有大量的数据冗余。简而言之,<strong>第三范式就是属性不依赖于其它非主属性。(我的理解是消除冗余)</strong></p><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">&nbsp;</p><h1>8.数据库优化的思路</h1><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">这个我借鉴了慕课上关于数据库优化的课程。</p><h2>1.SQL语句优化</h2><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">1)应尽量避免在 where 子句中使用!=或&lt;&gt;操作符,否则将引擎放弃使用索引而进行全表扫描。<br>2)应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:<br>select id from t where num is null<br><strong>可以在num上设置默认值0,确保表中num列没有null值</strong>,然后这样查询:<br>select id from t where num=0<br>3)很多时候用 exists 代替 in 是一个好的选择<br>4)用Where子句替换HAVING 子句 因为HAVING 只会在检索出所有记录之后才对结果集进行过滤</p><h2>2.索引优化</h2><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">看上文索引</p><h2>3.数据库结构优化</h2><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">1)范式优化: 比如消除冗余(节省空间。。) 2)反范式优化:比如适当加冗余等(减少join) 3)拆分表: 分区将数据在物理上分隔开,不同分区的数据可以制定保存在处于不同磁盘上的数据文件里。这样,当对这个表进行查询时,只需要在表分区中进行扫描,而不必进行全表扫描,明显缩短了查询时间,另外处于不同磁盘的分区也将对这个表的数据传输分散在不同的磁盘I/O,一个精心设置的分区可以将数据传输对磁盘I/O竞争均匀地分散开。对数据量大的时时表可采取此方法。可按月自动建表分区。<br>4)拆分其实又分垂直拆分和水平拆分: 案例: 简单购物系统暂设涉及如下表: 1.产品表(数据量10w,稳定) 2.订单表(数据量200w,且有增长趋势) 3.用户表 (数据量100w,且有增长趋势) 以<a style="color: rgb(102, 0, 204); text-decoration: none;" href="http://www.2cto.com/database/MySQL/" target="_blank">mysql</a>为例讲述下水平拆分和垂直拆分,mysql能容忍的数量级在百万静态数据可以到千万&nbsp;<strong>垂直拆分:</strong>解决问题:表与表之间的io竞争 不解决问题:单表中数据量增长出现的压力 方案: 把产品表和用户表放到一个server上 订单表单独放到一个server上&nbsp;<strong>水平拆分:</strong>&nbsp;解决问题:单表中数据量增长出现的压力 不解决问题:表与表之间的io争夺<br>方案: 用户表通过性别拆分为男用户表和女用户表 订单表通过已完成和完成中拆分为已完成订单和未完成订单 产品表 未完成订单放一个server上 已完成订单表盒男用户表放一个server上 女用户表放一个server上(女的爱购物 哈哈)</p><h2>4.服务器硬件优化</h2><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">这个么多花钱咯!</p><h1>9.存储过程与触发器的区别</h1><p style="color: rgb(68, 68, 68); line-height: 19.5px; font-family: YoonGothic, AppleGothic; font-size: 13px; margin-top: 10px; margin-bottom: 10px; background-color: rgb(255, 255, 255);">触发器与存储过程非常相似,触发器也是SQL语句集,<strong>两者唯一的区别是触发器不能用EXECUTE语句调用,而是在用户执行Transact-SQL语句时自动触发(激活)执行。触发器是在一个修改了指定表中的数据时执行的存储过程。</strong>通<strong>常通过创建触发器来强制实现不同表中的逻辑相关数据的引用完整性和一致性。</strong>由于用户不能绕过触发器,所以可以用它来强制实施复杂的业务规则,以确保数据的完整性。触发器不同于存储过程,<strong>触发器主要是通过事件执行触发而被执行的</strong>,而<strong>存储过程可以通过存储过程名称名字而直接调用</strong>。当对某一表进行诸如UPDATE、INSERT、DELETE这些操作时,SQLSERVER就会自动执行触发器所定义的SQL语句,从而确保对数据的处理必须符合这些SQL语句所定义的规则。</p><p>&nbsp;</p></div><div id="MySignature"></div>
    </div>
</div>
原文地址:https://www.cnblogs.com/buxl/p/9351733.html