您的位置:首页 > 数据库 > MySQL

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.
没有full-textindexes的也可以这样查找,不过速度比较慢。

以下的例子略。

§9.3InnoDB表

a
fast,transaction-safestorageengine.

提供:

事务,见第10章

Row-levellocking.其他表除了BDB外,更新时是锁定表的。该表此时不可访问。

支持外键

ConsistentnonlockingreadsinSELECTs.(Theidea
forthisisborrowedfromOracle.)

InnoDB有自己的配置选项,目录和存储数据的方法。MyISAM中一个table一个文件。InnoDB则是在表空间存储tablesandindexes,可能存储于多个文件中,为此大小不受操作系统单个文件大小的限制。不过与MyISAM比要浪费很多空间。

它的配置部分参见1,12章。

它也是使用dual-licensingagreement。

Slashdot
(www.slashdot.org),
Google(www.google.com),
andYahoo!Finance(http://finance.yahoo.com)等都使用InnoDB。适合处理事务环境下大量数据的高速处理。

它是世界上最快的transaction-safesystems之一,但是安全性不够。

InnoDB由InnoBaseOy生产,它的网站:www.innodb.com。

§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,youcanreadmoreaboutindexinginChapter19.)

·
Indexeswillbeusedonlyto
findrowswithqueriesthatusethe=or<=>operatorsinthesearch
clause.

§9.7小结

Summary

MySQLhassixtabletypes: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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: