MYSQL 教程:§9, MYSQL表类型
2008-10-16 11:08
796 查看
§9,MYSQL表类型
设计数据库管理系统由很多折衷。一些任务必须用transaction-safe的方法完成,但是这样增加了时间,磁盘,内存的开销。表类型又成为storageengines.它揭示了一些表类型依靠大量单独的源代码来管理自己的caching,
indexing,locking,anddiskaccess.也揭示数据库的根本目标:存储数据。
transactionortransaction
safe。一些查询必须一起执行。比如银行转账,要同时扣一个人钱和给另外一个人加钱。
InnoDBand
BerkeleyDBaretransactionsafe。Theothers(ISAM,MyISAM,MERGE,andHEAP)arenot.
We
willalsocoverthespecialfeaturesofMyISAMtablesinthischapter,specifically
compressedtablesandfull-textsearching。
§9.1ISAM表
ISAM是历史使用的表类型,在表移植性,支持大表,使用磁盘效率,key的使用等方面有很多缺陷。不推荐使用createtableasset
(
assetIDintnotnull,
descriptionvarchar(255)
)type=ISAM;
5.0已经不支持这个类型。
§9.2MYISAM表
createtablearticle(
articleIDintnotnullauto_incrementprimarykey,
titlevarchar(255),
bodytext
);
后面可以添加)type=MyISAM;不过默认也是这样的。
MyISAMtables有3种类型:dynamic,static,orcompressed.Compressedtables要使用myisampacktool来创建。
固定长度的表自动成为statictables,不固定的成为dynamictables.含有varchar,text,andblobcolumns类型的为不固定表。
statictable查找快速,易于cache,数据易于恢复。dynamictables节约空间,但是由于不固定长度,后面附加的内容和前面的未必在一个地方,cache,find,orrepairrecords比较困难。
*压缩MyISAM表
使用myisampack压缩,压缩表是只读的。Myisampack所作的工作包含压缩(Huffmancoding)和收缩表的优化,比如转换为更小的数据类型,用枚举代替列等。每个记录是单独压缩的,解压开销并不大。
*MyISAM表的全文本查找
全文本查找用于在一块文本中寻找词或字符串。创建举例:
createtablearticle(
articleIDintnotnullauto_incrementprimarykey,
titlevarchar(255),
bodytext,
fulltext(title,body)
);
查找单词'merger'
selecttitle
fromarticle
wherematch(title,body)against('merger');
查找'merge','acquisition',
'acquire',or'takeover'.中的任意单词。
selecttitlefromarticle
wherematch(title,body)against('mergeacquisitionacquiretakeover');
针对'acquire'和'acquisitions'要单独查询。即没有stemming功能。
以下部分暂不涉及,注意的是表的记录比较多的时候,效率并不高。
*布尔全文本查找
Thefollowingquerywillmatchonlyrecords
thancontaintheword'linux'andthestring"Open
Source",butnottheword'desktop'.Thewords'Java'
and'Oracle'areoptional,butwhendecidingonrelevance,finding'Java'
inarecordwillimproveitsranking,whereasfinding'Oracle'will
degradetheranking.Theorderofwordsinthesearchstringortherecordis
notimportant.
selecttitle
fromarticle
wherematch(title,body)
against('+linux+"OpenSource"-desktopJava~Oracle'INBOOLEANMODE);
Table9.1. BooleanModeSearchOperators | |
Operator | Meaning |
+ | Thiswordiscompulsory. |
- | Thiswordmustnotappear. |
< | Thiswordislessimportant. |
> | Thiswordismoreimportant. |
() | Groupwordstogetherasasubexpression. |
~ | Thiswordmayappear,butithasanegative effectonranking. |
* | Wildcardsuffix.Forexample,merge willnotmatchmerger,butmerge*willmatchbothmerge andmerger.Maybeusedonlyattheendofaword. |
"" | Thisisaphrase.Matchesonlyexactlythe samecontentinthesameorder. |
以下的例子略。
§9.3InnoDB表
afast,transaction-safestorageengine.
提供:
事务,见第10章
Row-levellocking.其他表除了BDB外,更新时是锁定表的。该表此时不可访问。
支持外键
ConsistentnonlockingreadsinSELECTs.(Theidea
forthisisborrowedfromOracle.)
InnoDB有自己的配置选项,目录和存储数据的方法。MyISAM中一个table一个文件。InnoDB则是在表空间存储tablesandindexes,可能存储于多个文件中,为此大小不受操作系统单个文件大小的限制。不过与MyISAM比要浪费很多空间。
它的配置部分参见1,12章。
它也是使用dual-licensingagreement。
Slashdot
(
Google(
andYahoo!Finance(
它是世界上最快的transaction-safesystems之一,但是安全性不够。
InnoDB由InnoBaseOy生产,它的网站:
§9.4BerkeleyDB(BDB)Tables
略§9.5合并
Tables
MyISAM中,由于操作系统限制文件大小,可以使用合并表解决。查询时可以把多个表当作一个表。Listing9.1AMERGETableExample
createdatabase
logs;
uselogs;
createtable
log2003Jan
(logidint
auto_incrementprimarykey,
logtsdatetime,
entry
char(255));
insertinto
log2003Janvalues
(NULL,'2003-01-01',
'firstjanentry');
createtable
log2003Feb
(logidint
auto_incrementprimarykey,
logtsdatetime,
entry
char(255));
insertinto
log2003Febvalues
(NULL,'2003-02-01',
'firstfebentry');
createtable
log2003Mar
(logidint
auto_incrementprimarykey,
logtsdatetime,
entrychar(255));
insertinto
log2003Marvalues
(NULL,'2003-03-01',
'firstmarentry');
createtable
logs
(logidint
auto_incrementprimarykey,
logtsdatetime,
entrychar(255))
type=merge
union=
(log2003Jan,log2003Feb,log2003Mar)
insert_method=
last;
INSERT_METHOD表从后面的表开始插入。FIRST从前面,NO表示不允许插入。
mysql>select
*fromlogs;
+-------+---------------------+-----------------+
|logid|
logts|entry|
+-------+---------------------+-----------------+
|1|2003-01-0100:00:00|firstjanentry
|
|1|2003-02-0100:00:00|firstfebentry
|
|1|2003-03-0100:00:00|firstmarentry
|
+-------+---------------------+-----------------+
3rowsinset
(0.00sec)
由于是3个表,primarykey是有可能重复的。
使用MERGE表的时候,可以查询单个表,但不可以DROP,ALTER,DELETEFROMTABLE,REPAIR,TRUNCATE,OPTIMIZE,orANALYZE单表。FLUSHTABLES可以关闭表。可以使用myisampack压缩,比如日志文件。
§9.6HEAPTables
存储于内存的表,多用于临时存储。createtabletestHeap
(idintnotnullprimarykey,
datachar(100))
type=heap
max_rows=100;
max_heap_table_size.可限制内存大小
缺陷如下:
·
Theydon'tsupportAUTO_INCREMENT.
·
Theydon'tsupportBLOBorTEXT
types.
·
HEAPtablescannotusethe
leftmostprefixofanindextofindrows.(Ifyouwouldlikemoreinformation
aboutwhatthismeans,youcanreadmoreaboutindexingin
·
Indexeswillbeusedonlyto
findrowswithqueriesthatusethe=or<=>operatorsinthesearch
clause.
§9.7小结
SummaryMySQLhassixtabletypes:ISAM,MyISAM,
InnoDB,BDB,MERGE,andHEAP.
OnlyInnoDBandBDBtablesaretransaction
safe.
OnlyMyISAMtablessupportfull-text
indexingandsearching.
ISAM
ISAMhadbeendeprecatedandsupercededby
MyISAM.
ISAMtableshaveahardsizelimitof4GB.
ISAMtablesarenotportable.
Youcanhaveamaximumof16keyspertable
andamaximumkeylengthof256bytes(characters).
MyISAM
MyISAMisthedefaulttabletype.Itis
veryfast,butnottransactionsafe.
MyISAMtablessupporttablecompression.
ThesizeofMyISAMtablesislimitedonly
bytheoperatingsystem,andthiscanbeworkedaroundwithMERGEtables.
ThedatafilesthatstoreMyISAMtablesare
portablefromsystemtosystem.
Youcanhaveamaximumof64keyspertable
andamaximumkeylengthof1024bytes.
InnoDB
InnoDBtablesaretransactionsafe.
InnoDBsupportsrow-levellocking.
Thereisnotheoreticalmaximumtablesize
becausetablesmaybestoredinmorethanonefile.
InnoDBprovidesconsistentnonlockingreads
inSELECT.
InnoDBtablesareportablefromsystemto
system.
InnoDBtablestakemorediskspacethan
MyISAMtables.
ForeignkeysaresupportedbetweenInnoDB
tables.
BDB
LikeInnoDBtables,BDBtablesare
transactionsafe.BDBtablesarenotaswidelyusedwithMySQLasInnoDB.
BDBsupportspage-levellocking.
BDBtablesarenotportable.
MERGE
MERGEtablesareusedtotreatmultiple
MyISAMtablesasasingletable,andtherefore,themaximumfilesize
limitationisremovedfromMyISAMtables.
HEAP
HEAPtablesarestoredonlyinmemoryand
needtobelimitedinsizetoavoidrunningoutofmemory.
DatastoredinaHEAPtableisvolatileand
willbelostintheeventofapowerfailure.
HEAPtablesaresuperfast,aslongasyou
haveenoughphysicalmemorytokeepthem.
HEAPtablesdonotsupportAUTO_INCREMENT,
TEXT,orBLOB.
相关文章推荐
- 21分钟 MySQL 入门教程 && mysql数据类型
- MySQL所支持的数据类型与表字段约束类型的学习教程
- MySQL基础教程2 —— 数据类型之日期和时间类型
- J2EE入门视频教程第五讲——MySQL中的数据类型及命令行基本查询语句
- 使用js将long类型转化为"yyyy-MM-dd hh:mm:ss"日期格式&jQuery自带日历控件的汉化教程
- 史上最简单的 MySQL 教程(九)「列类型 之 数值型」
- MySQL安装教程&Navicat安装
- MySQL基本概念--索引&索引类型
- MySQL基础教程3 —— 数据类型之字符串类型
- MySql基础教程二(视图,存储过程,游标,触发器,事务,权限,数据类型)
- Mysql的存储引擎的类型,MyISAM&InnoDB等
- mysql 刘道成视频教程 第4-8课 --- 数据类型
- MySQL所支持的数据类型与表字段约束类型的学习教程
- Ecmall&nbsp;MySql&nbsp;数据库调用教程
- 暗月mysql全版本通杀提权神器使用教程&工具
- 【转载&收藏】Oracle/MSSQL/Mysql 常用数据库的字段类型及大小
- MySQL基础教程18-Mysql数据类型-字符串类型
- Mysql 登陆&退出、创建&删除&选择数据库、基本数据类型、创建&删除表格
- MySQL基础教程4 —— 数据类型之存储需求及如何选择正确的类型
- 2014年4月1日MySQL学习记录--mysql数据类型及占用空间&&修改表的语法