文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>MySQL的优化(2)

MySQL的优化(2)

时间:2007-02-17  来源:PHP爱好者

--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
十一、维护
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果可能,偶尔运行一下OPTIMIZE table,这对大量更新的变长行非常重要。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
偶尔用myisamchk -a更新一下表中的键码分布统计。记住在做之前关掉MySQL。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果有碎片文件,可能值得将所有文件复制到另一个磁盘上,清除原来的磁盘并拷回文件。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果遇到问题,用myisamchk或CHECK table检查表。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
用mysqladmin -i10 precesslist extended-status监控MySQL的状态。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
用MySQL GUI客户程序,你可以在不同的窗口内监控进程列表和状态。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
使用mysqladmin debug获得有关锁定和性能的信息。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
十二、优化SQL
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
扬SQL之长,其它事情交由应用去做。使用SQL服务器来做:
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
找出基于WHERE子句的行。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
JOIN表
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
GROUP BY
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
ORDER BY
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
DISTINCT
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
不要使用SQL来做:
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
检验数据(如日期)
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
成为一只计算器
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
技巧:
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
明智地使用键码。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
键码适合搜索,但不适合索引列的插入/更新。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
保持数据为数据库第三范式,但不要担心冗余信息或这如果你需要更快的速度,创建总结表。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
在大表上不做GROUP BY,相反创建大表的总结表并查询它。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
UPDATE table set count=count+1 where key_column=constant非常快。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
对于大表,或许最好偶尔生成总结表而不是一直保持总结表。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
充分利用INSERT的默认值。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
十三、不同SQL服务器的速度差别(以秒计)
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
 
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
通过键码读取2000000行: NT Linux
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
mysql 367 249
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
mysql_odbc 464  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
db2_odbc 1206  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
informix_odbc 121126  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
ms-sql_odbc 1634  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
oracle_odbc 20800  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
solid_odbc 877  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
sybase_odbc 17614  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
 
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
插入350768行: NT Linux
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
mysql 381 206
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
mysql_odbc 619  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
db2_odbc 3460  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
informix_odbc 2692  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
ms-sql_odbc 4012  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
oracle_odbc 11291  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
solid_odbc 1801  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
sybase_odbc 4802  
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
在上述测试中,MySQL配置8M高速缓存运行,其他数据库以默认安装运行。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
十四、重要的MySQL启动选项
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
back_log 如果需要大量新连接,修改它。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
thread_cache_size 如果需要大量新连接,修改它。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
key_buffer_size 索引页池,可以设成很大。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
bdb_cache_size BDB表使用的记录和键吗高速缓存。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
table_cache 如果有很多的表和并发连接,修改它。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
delay_key_write 如果需要缓存所有键码写入,设置它。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
log_slow_queries 找出需花大量时间的查询。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
max_heap_table_size 用于GROUP BY
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
sort_buffer 用于ORDER BY和GROUP BY
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
myisam_sort_buffer_size 用于REPAIR TABLE
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
join_buffer_size 在进行无键吗的联结时使用。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
十五、优化表
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
MySQL拥有一套丰富的类型。你应该对每一列尝试使用最有效的类型。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
ANALYSE过程可以帮助你找到表的最优类型:SELECT * FROM table_name PROCEDURE ANALYSE()。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
对于不保存NULL值的列使用NOT NULL,这对你想索引的列尤其重要。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
将ISAM类型的表改为MyISAM。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果可能,用固定的表格式创建表。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
不要索引你不想用的东西。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
利用MySQL能按一个索引的前缀进行查询的事实。如果你有索引INDEX(a,b),你不需要在a上的索引。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
不在长CHAR/VARCHAR列上创建索引,而只索引列的一个前缀以节省存储空间。CREATE TABLE table_name (hostname CHAR(255) not null, index(hostname(10)))
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
对每个表使用最有效的表格式。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
在不同表中保存相同信息的列应该有同样的定义并具有相同的列名。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
十六、MySQL如何次存储数据
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
数据库以目录存储。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
表以文件存储。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
列以变长或定长格式存储在文件中。对BDB表,数据以页面形式存储。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
支持基于内存的表。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
数据库和表可在不同的磁盘上用符号连接起来。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
在Windows上,MySQL支持用.sym文件内部符号连接数据库。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
十七、MySQL表类型
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
HEAP表:固定行长的表,只存储在内存中并用HASH索引进行索引。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
ISAM表:MySQL 3.22中的早期B-tree表格式。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
MyIASM:IASM表的新版本,有如下扩展:
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
二进制层次的可移植性。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
NULL列索引。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
对变长行比ISAM表有更少的碎片。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
支持大文件。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
更好的索引压缩。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
更好的键吗统计分布。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
更好和更快的auto_increment处理。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
来自Sleepcat的Berkeley DB(BDB)表:事务安全(有BEGIN WORK/COMMIT|ROLLBACK)。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
十八、MySQL行类型(专指IASM/MyIASM表)
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果所有列是定长格式(没有VARCHAR、BLOB或TEXT),MySQL将以定长表格式创建表,否则表以动态长度格式创建。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
定长格式比动态长度格式快很多并更安全。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
动态长度行格式一般占用较少的存储空间,但如果表频繁更新,会产生碎片。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
在某些情况下,不值得将所有VARCHAR、BLOB和TEXT列转移到另一个表中,只是获得主表上的更快速度。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
利用myiasmchk(对ISAM,pack_iasm),可以创建只读压缩表,这使磁盘使用率最小,但使用慢速磁盘时,这非常不错。压缩表充分地利用将不再更新的日志表
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
十九、MySQL高速缓存(所有线程共享,一次性分配)
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
键码缓存:key_buffer_size,默认8M。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
表缓存:table_cache,默认64。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
线程缓存:thread_cache_size,默认0。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
主机名缓存:可在编译时修改,默认128。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
内存映射表:目前仅用于压缩表。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
注意:MySQL没有行高速缓存,而让操作系统处理。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
二十、MySQL缓存区变量(非共享,按需分配)
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
sort_buffer:ORDER BY/GROUP BY
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
record_buffer:扫描表。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
join_buffer_size:无键联结
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
myisam_sort_buffer_size:REPAIR TABLE
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
net_buffer_length:对于读SQL语句并缓存结果。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
tmp_table_size:临时结果的HEAP表大小。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
 
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
二十一、MySQL表高速缓存工作原理
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
每个MyISAM表的打开实例(instance)使用一个索引文件和一个数据文件。如果表被两个线程使用或在同一条查询中使用两次,MyIASM将共享索引文件而是打开数据文件的另一个实例。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果所有在高速缓存中的表都在使用,缓存将临时增加到比表缓存尺寸大些。如果是这样,下一个被释放的表将被关闭。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
你可以通过检查mysqld的Opened_tables变量以检查表缓存是否太小。如果该值太高,你应该增大表高速缓存。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
 
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
二十二、MySQL扩展/优化-提供更快的速度
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
使用优化的表类型(HEAP、MyIASM或BDB表)。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
对数据使用优化的列。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果可能使用定长行。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
使用不同的锁定类型(SELECT HIGH_PRIORITY,INSERT LOW_PRIORITY)
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
Auto_increment
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
REPLACE (REPLACE INTO table_name VALUES (...))
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
INSERT DELAYED
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
LOAD DATA INFILE / LOAD_FILE()
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
使用多行INSERT一次插入多行。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT INTO OUTFILE
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
LEFT JOIN, STRAIGHT JOIN
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
LEFT JOIN ,结合IS NULL
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
ORDER BY可在某些情况下使用键码。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果只查询在一个索引中的列,将只使用索引树解决查询。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
联结一般比子查询快(对大多数SQL服务器亦如此)。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
LIMIT
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT * from table1 WHERE a > 10 LIMIT 10,20
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
DELETE * from table1 WHERE a > 10 LIMIT 10
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
foo IN (常数列表) 高度优化。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
GET_LOCK()/RELEASE_LOCK()
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
LOCK TABLES
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
INSERT和SELECT可同时运行。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
UDF函数可装载进一个正在运行的服务器。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
压缩只读表。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
CREATE TEMPORARY TABLE
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
CREATE TABLE .. SELECT
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
带RAID选项的MyIASM表将文件分割成很多文件以突破某些文件系统的2G限制。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
Delay_keys
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
复制功能
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
二十二、MySQL何时使用索引
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
对一个键码使用>, >=, =, <, <=, IF NULL和BETWEEN
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT * FROM table_name WHERE key_part1=1 and key_part2 > 5;
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT * FROM table_name WHERE key_part1 IS NULL;
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
当使用不以通配符开始的LIKE
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT * FROM table_name WHERE key_part1 LIKE 'jani%'
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
在进行联结时从另一个表中提取行时
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT * from t1,t2 where t1.col=t2.key_part
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
找出指定索引的MAX()或MIN()值
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT MIN(key_part2),MAX(key_part2) FROM table_name where key_part1=10
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
一个键码的前缀使用ORDER BY或GROUP BY
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT * FROM foo ORDER BY key_part1,key_part2,key_part3
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
在所有用在查询中的列是键码的一部分时间
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT key_part3 FROM table_name WHERE key_part1=1
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
--------------------------------------------------------------------------------
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
二十三、MySQL何时不使用索引
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果MySQL能估计出它将可能比扫描整张表还要快时,则不使用索引。例如如果key_part1均匀分布在1和100之间,下列查询中使用索引就不是很好:
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT * FROM table_name where key_part1 > 1 and key_part1 <90
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果使用HEAP表且不用=搜索所有键码部分。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
在HEAP表上使用ORDER BY。
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果不是用键码第一部分
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT * FROM table_name WHERE key_part2=1
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
如果使用以一个通配符开始的LIKE
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT * FROM table_name WHERE key_part1 LIKE '%jani%'
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
搜索一个索引而在另一个索引上做ORDER BY
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
SELECT * from table_name WHERE key_part1 = # ORDER BY key2
wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N

wwww.chin aitpower.comSKblKcHMkyiqJXHWIie28CS0N
php爱好者站 http://www.phpfans.net 为phper提供一切资讯.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载