|
一、連接MySQL 格式:mysql -h主機(jī)地址-u用戶名-p用戶密碼 1、例1:連接到本機(jī)上的MYSQL。 首先在打開DOS窗口,然后進(jìn)入目錄mysqlbin,再鍵入命令mysql -uroot -p,回車后提示你輸密碼,如果剛安裝好MYSQL,超級(jí)用戶root是沒有密碼的,故直接回車即可進(jìn)入到MYSQL中了,MYSQL的提示符是:mysql>。 2、例2:連接到遠(yuǎn)程主機(jī)上的MYSQL。假設(shè)遠(yuǎn)程主機(jī)的IP為:110.110.110.110,用戶名為root,密碼為abcd123。則鍵入以下命令: mysql -h110.110.110.110 -uroot -pabcd123 (注:u與root可以不用加空格,其它也一樣) 3、退出MYSQL命令:exit (回車)。 二、修改密碼 格式:mysqladmin -u用戶名-p舊密碼password 新密碼 1、例1:給root加個(gè)密碼ab12。首先在DOS下進(jìn)入目錄mysqlbin,然后鍵入以下命令: mysqladmin -uroot -password ab12 注:因?yàn)殚_始時(shí)root沒有密碼,所以-p舊密碼一項(xiàng)就可以省略了。 2、例2:再將root的密碼改為djg345。 mysqladmin -uroot -pab12 password djg345 三、增加新用戶。(注意:和上面不同,下面的因?yàn)槭荕ySQL環(huán)境中的命令,所以后面都帶一個(gè)分號(hào)作為命令結(jié)束符) 格式:grant select on 數(shù)據(jù)庫(kù).* to 用戶名@登錄主機(jī)identified by "密碼" 例1、增加一個(gè)用戶test1密碼為abc,讓他可以在任何主機(jī)上登錄,并對(duì)所有數(shù)據(jù)庫(kù)有查詢、插入、修改、刪除的權(quán)限。首先用以root用戶連入MySQL,然后鍵入以下命令: grant select,insert,update, delete on *.* to [email=test2@localhost]test2@localhost[/email] identified by "abc"; 如果你不想test2有密碼,可以再打一個(gè)命令將密碼消掉。 grant select,insert,update,delete on mydb.* to [email=test2@localhost]test2@localhost[/email] identified by ""; 在上面講了登錄、增加用戶、密碼更改等問(wèn)題。下面我們來(lái)看看MySQL中有關(guān)數(shù)據(jù)庫(kù)方面的操作。注意:你必須首先登錄到MySQL中,以下操作都是在MySQL的提示符下進(jìn)行的,而且每個(gè)命令以分號(hào)結(jié)束。 1、MySQL常用命令 create database name; 創(chuàng)建數(shù)據(jù)庫(kù) use databasename; 選擇數(shù)據(jù)庫(kù) drop database name 直接刪除數(shù)據(jù)庫(kù),不提醒 show tables; 顯示表 describe tablename; 表的詳細(xì)描述 select 中加上distinct去除重復(fù)字段 mysqladmin drop database name 刪除數(shù)據(jù)庫(kù)前,有提示。 顯示當(dāng)前mysql版本和當(dāng)前日期 select version(),current_date; 2、修改mysql中root的密碼: shell>mysql -u root -p mysql> update user set password=password(”xueok654123″) where user=?root?; mysql> flush privileges //刷新數(shù)據(jù)庫(kù) mysql>use dbname;打開數(shù)據(jù)庫(kù): mysql>show databases; 顯示所有數(shù)據(jù)庫(kù) mysql>show tables; 顯示數(shù)據(jù)庫(kù)mysql中所有的表:先use mysql;然后 mysql>describe user; 顯示表mysql數(shù)據(jù)庫(kù)中user表的列信息); 3、grant 創(chuàng)建一個(gè)可以從任何地方連接服務(wù)器的一個(gè)完全的超級(jí)用戶,但是必須使用一個(gè)口令something做這個(gè) mysql> grant all privileges on *.* to [email=user@localhost]user@localhost[/email] identified by ?something? with 增加新用戶 格式:grant select on 數(shù)據(jù)庫(kù).* to 用戶名@登錄主機(jī)identified by “密碼” GRANT ALL PRIVILEGES ON *.* TO [email=monty@localhost]monty@localhost[/email] IDENTIFIED BY ?something? WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* TO [email=monty@%E2%80%9D%]monty@”%[/email]” IDENTIFIED BY ?something? WITH GRANT OPTION; 刪除授權(quán): mysql> revoke all privileges on *.* from [email=root@%E2%80%9D%]root@”%[/email]”; mysql> delete from user where user=”root” and host=”%”; mysql> flush privileges; 創(chuàng)建一個(gè)用戶custom在特定客戶端it363.com登錄,可訪問(wèn)特定數(shù)據(jù)庫(kù)fangchandb mysql >grant select, insert, update, delete, create,drop on fangchandb.* to custom@ it363.com identified by … passwd? 重命名表: mysql > alter table t1 rename t2; 4、mysqldump 備份數(shù)據(jù)庫(kù) shell> mysqldump -h host -u root -p dbname >dbname_backup.sql 恢復(fù)數(shù)據(jù)庫(kù) shell> mysqladmin -h myhost -u root -p create dbname shell> mysqldump -h host -u root -p dbname < dbname_backup.sql 如果只想卸出建表指令,則命令如下: shell> mysqladmin -u root -p -d databasename > a.sql 如果只想卸出插入數(shù)據(jù)的sql命令,而不需要建表命令,則命令如下: shell> mysqladmin -u root -p -t databasename > a.sql 那么如果我只想要數(shù)據(jù),而不想要什么sql命令時(shí),應(yīng)該如何操作呢? mysqldump -T./ phptest driver 其中,只有指定了-T參數(shù)才可以卸出純文本文件,表示卸出數(shù)據(jù)的目錄,./表示當(dāng)前目錄,即與mysqldump同一目錄。如果不指定driver 表,則將卸出整個(gè)數(shù)據(jù)庫(kù)的數(shù)據(jù)。每個(gè)表會(huì)生成兩個(gè)文件,一個(gè)為.sql文件,包含建表執(zhí)行。另一個(gè)為.txt文件,只包含數(shù)據(jù),且沒有sql指令。 5、可將查詢存儲(chǔ)在一個(gè)文件中并告訴mysql從文件中讀取查詢而不是等待鍵盤輸入。可利用外殼程序鍵入重定向?qū)嵱贸绦騺?lái)完成這項(xiàng)工作。例如,如果在文件my_file.sql 中存放有查詢,可如下執(zhí)行這些查詢: 例如,如果您想將建表語(yǔ)句提前寫在sql.txt中: mysql > mysql -h myhost -u root -p database < sql.txt 1、安裝環(huán)境: Windows XP Mysql 4.0.17 從下次就需要用mysql -uroot -proot才可以登陸 在遠(yuǎn)程或本機(jī)可以使用mysql -h 172.5.1.183 -uroot 登陸,這個(gè)根據(jù)第二行的策略確定權(quán)限修改生效: 1)net stop mysql net start mysql 2)c:/mysql/bin/mysqladmin flush-privileges 3)登陸mysql后,用flush privileges語(yǔ)句 6、創(chuàng)建數(shù)據(jù)庫(kù)staffer create database staffer; 7、下面的語(yǔ)句在mysql環(huán)境在執(zhí)行 顯示用戶擁有權(quán)限的數(shù)據(jù)庫(kù)show databases; 切換到staffer數(shù)據(jù)庫(kù)use staffer; 顯示當(dāng)前數(shù)據(jù)庫(kù)中有權(quán)限的表show tables; 顯示表staffer的結(jié)構(gòu)desc staffer; 8、創(chuàng)建測(cè)試環(huán)境 1)創(chuàng)建數(shù)據(jù)庫(kù)staffer mysql> create database staffer 2)創(chuàng)建表staffer,department,position,depart_pos create table s_position ( id int not null auto_increment, name varchar(20) not null default '經(jīng)理', #設(shè)定默認(rèn)值 description varchar(100), primary key PK_positon (id) #設(shè)定主鍵 ); create table department ( id int not null auto_increment, name varchar(20) not null default '系統(tǒng)部', #設(shè)定默認(rèn)值 description varchar(100), primary key PK_department (id) #設(shè)定主鍵 ); create table depart_pos ( department_id int not null, position_id int not null, primary key PK_depart_pos (department_id,position_id) #設(shè)定復(fù)和主鍵); create table staffer ( id int not null auto_increment primary key, #設(shè)定主鍵 name varchar(20) not null default '無(wú)名氏', #設(shè)定默認(rèn)值 department_id int not null, position_id int not null, unique (department_id,position_id) #設(shè)定唯一值 ); 3)刪除 mysql> drop table depart_pos; drop table department; drop table s_position; drop table staffer; drop database staffer; 9、修改結(jié)構(gòu) mysql> #表position增加列test alter table position add(test char(10)); #表position修改列test alter table position modify test char(20) not null; #表position修改列test默認(rèn)值 alter table position alter test set default 'system'; #表position去掉test默認(rèn)值 alter table position alter test drop default; #表position去掉列test alter table position drop column test; #表depart_pos刪除主鍵 alter table depart_pos drop primary key; #表depart_pos增加主鍵 alter table depart_pos add primary key PK_depart_pos (department_id,position_id); 10、操作數(shù)據(jù) #插入表department insert into department(name,description) values('系統(tǒng)部','系統(tǒng)部'); insert into department(name,description) values('公關(guān)部','公關(guān)部'); insert into department(name,description) values('客服部','客服部'); insert into department(name,description) values('財(cái)務(wù)部','財(cái)務(wù)部'); insert into department(name,description) values('測(cè)試部','測(cè)試部'); #插入表s_position insert into s_position(name,description) values('總監(jiān)','總監(jiān)'); insert into s_position(name,description) values('經(jīng)理','經(jīng)理'); insert into s_position(name,description) values('普通員工','普通員工'); #插入表depart_pos insert into depart_pos(department_id,position_id) select a.id department_id,b.id postion_id from department a,s_position b; #插入表staffer insert into staffer(name,department_id,position_id) values('陳達(dá)治',1,1); insert into staffer(name,department_id,position_id) values('李文賓',1,2); insert into staffer(name,department_id,position_id) values('馬佳',1,3); insert into staffer(name,department_id,position_id) values('亢志強(qiáng)',5,1); insert into staffer(name,department_id,position_id) values('楊玉茹',4,1); 11、查詢及刪除操作 #顯示系統(tǒng)部的人員和職位 select a.name,b.name department_name,c.name position_name from staffer a,department b,s_position c where a.department_id=b.id and a.position_id=c.id and b.name='系統(tǒng)部'; #顯示系統(tǒng)部的人數(shù) select count(*) from staffer a,department b where a.department_id=b.id and b.name='系統(tǒng)部' #顯示各部門的人數(shù) select count(*) cou,b.name from staffer a,department b where a.department_id=b.id group by b.name; #刪除客服部 delete from department where name='客服部'; #將財(cái)務(wù)部修改為財(cái)務(wù)一部 update department set name='財(cái)務(wù)一部' where name='財(cái)務(wù)部'; 12、備份和恢復(fù) 備份數(shù)據(jù)庫(kù)staffer c:/mysql/bin/mysqldump -uroot -proot staffer>e:staffer.sql 得到的staffer.sql是一個(gè)sql腳本,不包括建庫(kù)的語(yǔ)句,所以你需要手工 創(chuàng)建數(shù)據(jù)庫(kù)才可以導(dǎo)入 恢復(fù)數(shù)據(jù)庫(kù)staffer,需要?jiǎng)?chuàng)建一個(gè)空庫(kù)staffer c:/mysql/bin/mysql -uroot -proot staffer<staffer.sql 如果不希望后來(lái)手工創(chuàng)建staffer,可以 c:/mysql/bin/mysqldump -uroot -proot --databases staffer>e:staffer.sql mysql -uroot -proot >e:staffer.sql 但這樣的話系統(tǒng)種就不能存在staffer庫(kù),且無(wú)法導(dǎo)入其他名字的數(shù)據(jù)庫(kù), 當(dāng)然你可以手工修改staffer.sql文件 13、從文本向數(shù)據(jù)庫(kù)導(dǎo)入數(shù)據(jù) 1)使用工具c:/mysql/bin/mysqlimport 這個(gè)工具的作用是將文件導(dǎo)入到和去掉文件擴(kuò)展名名字相同的表里,如 staffer.txt,staffer都是導(dǎo)入到staffer表中 常用選項(xiàng)及功能如下 -d or --delete 新數(shù)據(jù)導(dǎo)入數(shù)據(jù)表中之前刪除數(shù)據(jù)數(shù)據(jù)表中的所有信息 -f or --force 不管是否遇到錯(cuò)誤,mysqlimport將強(qiáng)制繼續(xù)插入數(shù)據(jù) -i or --ignore mysqlimport跳過(guò)或者忽略那些有相同唯一 關(guān)鍵字的行,導(dǎo)入文件中的數(shù)據(jù)將被忽略。 -l or -lock-tables 數(shù)據(jù)被插入之前鎖住表,這樣就防止了, 你在更新數(shù)據(jù)庫(kù)時(shí),用戶的查詢和更新受到影響。 -r or -replace 這個(gè)選項(xiàng)與-i選項(xiàng)的作用相反;此選項(xiàng)將替代 表中有相同唯一關(guān)鍵字的記錄。 --fields-enclosed- by= char 指定文本文件中數(shù)據(jù)的記錄時(shí)以什么括起的,很多情況下 數(shù)據(jù)以雙引號(hào)括起。默認(rèn)的情況下數(shù)據(jù)是沒有被字符括起的。 --fields-terminated- by=char 指定各個(gè)數(shù)據(jù)的值之間的分隔符,在句號(hào)分隔的文件中, 分隔符是句號(hào)。您可以用此選項(xiàng)指定數(shù)據(jù)之間的分隔符。 默認(rèn)的分隔符是跳格符(Tab) --lines-terminated- by=str 此選項(xiàng)指定文本文件中行與行之間數(shù)據(jù)的分隔字符串 或者字符。默認(rèn)的情況下mysqlimport以newline為行分隔符。 您可以選擇用一個(gè)字符串來(lái)替代一個(gè)單個(gè)的字符: 一個(gè)新行或者一個(gè)回車。 mysqlimport命令常用的選項(xiàng)還有-v 顯示版本(version), -p 提示輸入密碼(password)等。 這個(gè)工具有個(gè)問(wèn)題,無(wú)法忽略某些列,這樣對(duì)我們的數(shù)據(jù)導(dǎo)入有很大的麻煩,雖然可以手工設(shè)置這個(gè)字段,但會(huì)出現(xiàn)莫名其妙的結(jié)果,我們做一個(gè)簡(jiǎn)單的示例 我們定義如下的depart_no.txt,保存在e盤,間隔為制表符 10 10 11 11 12 24 執(zhí)行如下命令 c:/mysql/bin/mysqlimport -uroot -proot staffer e:depart_pos.txt 在這里沒有使用列的包圍符號(hào),分割采用默認(rèn)的 ,因?yàn)椴捎脛e的符號(hào)會(huì)有問(wèn)題, 不知道是不是windows的原因 2)Load Data INFILE file_name into table_name(column1_name,column2_name) 這個(gè)命令在mysql>提示符下使用,優(yōu)點(diǎn)是可以指定列導(dǎo)入,示例如下 c:/mysql/bin/mysql -uroot -proot staffer mysql>load data infile "e:/depart_no.txt" into depart_no(department_id,position_id); 這兩個(gè)工具在Windows下使用都有問(wèn)題,不知道是Windows的原因還是中文的問(wèn)題,而且不指定的列它產(chǎn)生了空值,這顯然不是我們想要的,所以謹(jǐn)慎使用這些工具 進(jìn)入MySQL:mysql -uuser -ppassword --port=3307 1:使用SHOW語(yǔ)句找出在服務(wù)器上當(dāng)前存在什么數(shù)據(jù)庫(kù): mysql> SHOW DA TABASES; 2:2、創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)MYSQLDATA mysql> Create DATABASE MYSQLDA TA; 3:選擇你所創(chuàng)建的數(shù)據(jù)庫(kù) mysql> USE MYSQLDATA; (按回車鍵出現(xiàn)Database changed 時(shí)說(shuō)明操作成功!) 4:查看現(xiàn)在的數(shù)據(jù)庫(kù)中存在什么表 mysql> SHOW TABLES; 5:創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)表 mysql> Create TABLE MYTABLE (name V ARCHAR(20), sex CHAR(1)); 6:顯示表的結(jié)構(gòu): mysql> DESCRIBE MYTABLE; 7:往表中加入記錄 mysql> insert into MYTABLE values ("hyq","M"); 8:用文本方式將數(shù)據(jù)裝入數(shù)據(jù)庫(kù)表中(例如D:/mysql.txt) mysql> LOAD DA TA LOCAL INFILE "D:/mysql.txt" INTO TABLE MYTABLE; 9:導(dǎo)入.sql文件命令(例如D:/mysql.sql) mysql>use database; mysql>source d:/mysql.sql; 10:刪除表 mysql>drop TABLE MYTABLE; 11:清空表 mysql>delete from MYTABLE; 12:更新表中數(shù)據(jù) mysql>update MYTABLE set sex="f" where name='hyq'; UPDATE [LOW_PRIORITY] [IGNORE] tbl_name SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE where_definition] [ORDER BY ...] [LIMIT rows] or UPDATE [LOW_PRIORITY] [IGNORE] tbl_name [, tbl_name ...] SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE where_definition] UPDATE 以新的值更新現(xiàn)存表中行的列。SET 子句指出要修改哪個(gè)列和他們應(yīng)該給定的值。WHERE 子句如果被給出,指定哪個(gè)記錄行應(yīng)該被更新。否則,所有的記錄行被更新。如果ORDER BY 子句被指定,記錄行將被以指定的次序更新。 如果你指定關(guān)鍵詞LOW_PRIORITY,UPDA TE 的執(zhí)行將被延遲,直到?jīng)]有其它的客戶端正在讀取表。 如果你指定關(guān)鍵詞IGNORE,該更新語(yǔ)句將不會(huì)異常中止,即使在更新過(guò)程中出現(xiàn)重復(fù)鍵錯(cuò)誤。導(dǎo)致沖突的記錄行將不會(huì)被更新。 如果在一個(gè)表達(dá)式中從tbl_name 中訪問(wèn)一個(gè)列,UPDATE 使用列的當(dāng)前值。舉例來(lái)說(shuō),下面的語(yǔ)句設(shè)置age 列值為它的當(dāng)前值加 1 : mysql> UPDATE persondata SET age=age+1; UPDATE 賦值是從左到右計(jì)算的。舉例來(lái)說(shuō),下列語(yǔ)句將age 列設(shè)置為它的兩倍,然后再加1 : mysql> UPDATE persondata SET age=age*2, age=age+1; 如果你設(shè)置列為其當(dāng)前的值,MySQL 注意到這點(diǎn),并不更新它。 UPDATE 返回實(shí)際被改變的記錄行數(shù)目。在MySQL 3.22 或更新的版本中,C API 函數(shù)mysql_info() 返回被匹配并更新的記錄行數(shù)目,以及在UPDATE 期間發(fā)生的警告的數(shù)目。 在MySQL 3.23 中,你可以使用LIMIT # 來(lái)確保只有給定的記錄行數(shù)目被更改。 如果一個(gè)ORDER BY 子句被使用(從MySQL 4.0.0 開始支持),記錄行將以指定的次序被更新。這實(shí)際上只有連同LIMIT 一起才有用。 從MySQL 4.0.4 開始,你也可以執(zhí)行一個(gè)包含多個(gè)表的UPDATE 的操作: UPDATE items,month SET items.price=month.price WHERE items.id=month.id; 注意:多表UPDA TE 不可以使用ORDER BY 或LIMIT。 關(guān)鍵字: mysql 啟動(dòng):net start mySql; 進(jìn)入:mysql -u root -p/mysql -h localhost -u root -p databaseName; 列出數(shù)據(jù)庫(kù):show databases; 選擇數(shù)據(jù)庫(kù):use databaseName; 列出表格:show tables; 顯示表格列的屬性:show columns from tableName; 建立數(shù)據(jù)庫(kù):source fileName.txt; 匹配字符:可以用通配符_代表任何一個(gè)字符,%代表任何字符串; 增加一個(gè)字段:alter table tabelName add column fieldName dateType; 增加多個(gè)字段:alter table tabelName add column fieldName1 dateType,add columns fieldName2 dateType; 多行命令輸入:注意不能將單詞斷開;當(dāng)插入或更改數(shù)據(jù)時(shí),不能將字段的字符串展開到多行里,否則硬回車將被儲(chǔ)存到數(shù)據(jù)中; 增加一個(gè)管理員帳戶:grant all on *.* to [email=user@localhost]user@localhost[/email] identified by "password"; 每條語(yǔ)句輸入完畢后要在末尾填加分號(hào)';',或者填加'g'也可以; 查詢時(shí)間:select now(); 查詢當(dāng)前用戶:select user(); 查詢數(shù)據(jù)庫(kù)版本:select version(); 查詢當(dāng)前使用的數(shù)據(jù)庫(kù):select database(); 1、刪除student_course數(shù)據(jù)庫(kù)中的students數(shù)據(jù)表: rm -f student_course/students.* 2、備份數(shù)據(jù)庫(kù):(將數(shù)據(jù)庫(kù)test備份) mysqldump -u root -p test>c: est.txt 備份表格:(備份test數(shù)據(jù)庫(kù)下的mytable表格) mysqldump -u root -p test mytable>c: est.txt 將備份數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù):(導(dǎo)回test數(shù)據(jù)庫(kù)) mysql -u root -p test 3、創(chuàng)建臨時(shí)表:(建立臨時(shí)表zengchao) create temporary table zengchao(name varchar(10)); 4、創(chuàng)建表是先判斷表是否存在 create table if not exists students(……); 5、從已經(jīng)有的表中復(fù)制表的結(jié)構(gòu) create table table2 select * from table1 where 1<>1; 6、復(fù)制表 create table table2 select * from table1; 7、對(duì)表重新命名 alter table table1 rename as table2; 8、修改列的類型 alter table table1 modify id int unsigned;//修改列id的類型為int unsigned alter table table1 change id sid int unsigned;//修改列id的名字為sid,而且把屬性修改為int unsigned 9、創(chuàng)建索引 alter table table1 add index ind_id (id); create index ind_id on table1 (id); create unique index ind_id on table1 (id);//建立唯一性索引 10、刪除索引 drop index idx_id on table1; alter table table1 drop index ind_id; 11、聯(lián)合字符或者多個(gè)列(將列id與":"和列name和"="連接) select concat(id,':',name,'=') from students; 12、limit(選出10到20條)<第一個(gè)記錄集的編號(hào)是0> select * from students order by id limit 9,10; 13、MySQL不支持的功能 事務(wù),視圖,外鍵和引用完整性,存儲(chǔ)過(guò)程和觸發(fā)器 14、MySQL會(huì)使用索引的操作符號(hào) <,<=,>=,>,=,between,in,不帶%或者_(dá)開頭的like 15、使用索引的缺點(diǎn) 1)減慢增刪改數(shù)據(jù)的速度; 2)占用磁盤空間; 3)增加查詢優(yōu)化器的負(fù)擔(dān); 當(dāng)查詢優(yōu)化器生成執(zhí)行計(jì)劃時(shí),會(huì)考慮索引,太多的索引會(huì)給查詢優(yōu)化器增加工作量,導(dǎo)致無(wú)法選擇最優(yōu)的查詢方案; 16、分析索引效率 方法:在一般的SQL語(yǔ)句前加上explain; 分析結(jié)果的含義: 1)table:表名; 2)type:連接的類型,(ALL/Range/Ref)。其中ref是最理想的; 3)possible_keys:查詢可以利用的索引名; 4)key:實(shí)際使用的索引; 5)key_len:索引中被使用部分的長(zhǎng)度(字節(jié)); 6)ref:顯示列名字或者"const"(不明白什么意思); 7)rows:顯示MySQL認(rèn)為在找到正確結(jié)果之前必須掃描的行數(shù); 8)extra:MySQL的建議; 17、使用較短的定長(zhǎng)列 1)盡可能使用較短的數(shù)據(jù)類型; 2)盡可能使用定長(zhǎng)數(shù)據(jù)類型; a)用char代替varchar,固定長(zhǎng)度的數(shù)據(jù)處理比變長(zhǎng)的快些; b)對(duì)于頻繁修改的表,磁盤容易形成碎片,從而影響數(shù)據(jù)庫(kù)的整體性能; c)萬(wàn)一出現(xiàn)數(shù)據(jù)表崩潰,使用固定長(zhǎng)度數(shù)據(jù)行的表更容易重新構(gòu)造。使用固定長(zhǎng)度的數(shù)據(jù)行,每個(gè)記錄的開始位置都是固定記錄長(zhǎng)度的倍數(shù),可以很容易被檢測(cè)到,但是使用可變長(zhǎng)度的數(shù)據(jù)行就不一定了; d)對(duì)于MyISAM類型的數(shù)據(jù)表,雖然轉(zhuǎn)換成固定長(zhǎng)度的數(shù)據(jù)列可以提高性能,但是占據(jù)的空間也大; 18、使用not null和enum 盡量將列定義為not null,這樣可使數(shù)據(jù)的出來(lái)更快,所需的空間更少,而且在查詢時(shí), MySQL不需要檢查是否存在特例,即null值,從而優(yōu)化查詢; 如果一列只含有有限數(shù)目的特定值,如性別,是否有效或者入學(xué)年份等,在這種情況下應(yīng)該考慮將其轉(zhuǎn)換為enum列的值,MySQL處理的更快,因?yàn)樗械膃num值在系統(tǒng)內(nèi)都是 以標(biāo)識(shí)數(shù)值來(lái)表示的; 19、使用optimize table 對(duì)于經(jīng)常修改的表,容易產(chǎn)生碎片,使在查詢數(shù)據(jù)庫(kù)時(shí)必須讀取更多的磁盤塊,降低查詢性能。具有可變長(zhǎng)的表都存在磁盤碎片問(wèn)題,這個(gè)問(wèn)題對(duì)blob數(shù)據(jù)類型更為突出,因?yàn)槠涑叽缱兓浅4?。可以通過(guò)使用optimize table來(lái)整理碎片,保證數(shù)據(jù)庫(kù)性能不下降,優(yōu)化那些受碎片影響的數(shù)據(jù)表。optimize table可以用于MyISAM和BDB類型的數(shù)據(jù)表。實(shí)際上任何碎片整理方法都是用mysqldump來(lái)轉(zhuǎn)存數(shù)據(jù)表,然后使用轉(zhuǎn)存后的文件并重新建數(shù)據(jù)表; 20、使用procedure analyse() 可以使用procedure analyse()顯示最佳類型的建議,使用很簡(jiǎn)單,在select語(yǔ)句后面加上procedure analyse()就可以了;例如: select * from students procedure analyse(); select * from students procedure analyse(16,256); 第二條語(yǔ)句要求procedure analyse()不要建議含有多于16個(gè)值,或者含有多于256字節(jié)的enum類型,如果沒有限制,輸出可能會(huì)很長(zhǎng); 21、使用查詢緩存 1)查詢緩存的工作方式: 第一次執(zhí)行某條select語(yǔ)句時(shí),服務(wù)器記住該查詢的文本內(nèi)容和查詢結(jié)果,存儲(chǔ)在緩存中,下次碰到這個(gè)語(yǔ)句時(shí),直接從緩存中返回結(jié)果;當(dāng)更新數(shù)據(jù)表后,該數(shù)據(jù)表的任何緩存查詢都變成無(wú)效的,并且會(huì)被丟棄。 2)配置緩存參數(shù): 變量:query_cache _type,查詢緩存的操作模式。有3中模式,0:不緩存;1:緩存查詢,除非與select sql_no_cache開頭;2:根據(jù)需要只緩存那些以select sql_cache開頭的查詢; query_cache_size:設(shè)置查詢緩存的最大結(jié)果集的大小,比這個(gè)值大的不會(huì)被緩存。 22、調(diào)整硬件 1)在機(jī)器上裝更多的內(nèi)存; 2)增加更快的硬盤以減少I/O等待時(shí)間; 尋道時(shí)間是決定性能的主要因素,逐字地移動(dòng)磁頭是最慢的,一旦磁頭定位,從磁道讀則很快; 3)在不同的物理硬盤設(shè)備上重新分配磁盤活動(dòng); 如果可能,應(yīng)將最繁忙的數(shù)據(jù)庫(kù)存放在不同的物理設(shè)備上,這跟使用同一物理設(shè)備的不同分區(qū)是不同的,因?yàn)樗鼈儗?zhēng)用相同的物理資源(磁頭)
信息發(fā)布:廣州名易軟件有限公司 http://m.jetlc.com
|