Ubuntu mysql可以把data防止到內(nèi)存盤中
Ubuntu mysql對于電腦使用的玩家的常用軟件,然后我就學習及深入的研究Ubuntu mysql,在這里和大家一起探討Ubuntu mysql的使用方法,希望對大家有用。
1、如果Ubuntu mysql的data數(shù)據(jù)很少,內(nèi)存足夠大,可以把data防止到內(nèi)存盤中。linux如下設置內(nèi)存盤:mount -t ramfs none /ram默認使用內(nèi)存一半如果內(nèi)存不夠大,系統(tǒng)有多個硬盤,則把Ubuntu mysql應用程序和data目錄分開到不同硬盤上。
2、Ubuntu mysql的表設置為myiasm,比同等條件下的innodb能快20倍以上
3、導入完成以后才創(chuàng)建數(shù)據(jù)庫索引
4、導入完成以后根據(jù)需要轉(zhuǎn)換為其他engine,比如innodb
5、多條數(shù)據(jù)插入一個表,可以使用多記錄方式:insert into tablename values('xxx','xxx'),('yyy','yyy')...;
6、如果多個Ubuntu mysql執(zhí)行導入,可以使用delayedinsert delayed into tablename values('sss','ssss');
7、大文件sql文件可以用split分成多份再導
8、同等條件下,redhat比ubuntu強很多(幾乎肯定)
9 性能調(diào)整技巧(Performance tuning tips)
1. 如果 Unix top 或 Windows 任務管理器(Task Manager) 顯示服務的 CPU 占用率小于 70%,(shows that the CPU usage percentage with your workload is less than 70 %,)你的系統(tǒng)瓶頸可能在磁盤讀寫上?;蛟S你提交了大量的事務,或者是緩沖池(buffer pool)太小了。將緩沖池設大點會有所幫助,但一定要注意不能大于物理內(nèi)存的 80%。
2. 在一個事務中包含幾個修改。如果事務對數(shù)據(jù)庫進行了修改,那么在這個事務提交時 InnoDB 必須刷新日志到磁盤上。因為硬盤的旋轉(zhuǎn)速度通常至多為 167 轉(zhuǎn)/秒,那么只要磁盤不欺騙操作系統(tǒng),提交的事務數(shù)目限止也同樣為 167 次/秒·用戶。
3. 如果掉失最近的幾個事務無所謂的話,可以在 my.cnf 文件中將參數(shù) innodb_flush_log_at_trx_commit 設置為 0。InnoDB 無論如何總是嘗試一秒刷新(flush)一次日志,盡管刷新并不能得到保證。
4. 將日志文件(log files)設大一點,使日志文件的總和正好與緩沖池(buffer pool)一樣大。當 InnoDB 用光日志文件的空間時,它不得不在一個時間點上將緩沖池內(nèi)修改過的內(nèi)容寫到磁盤上。 小的日志文件可能引起不必要的磁盤寫操作。但是大的日志文件的缺點就是在數(shù)據(jù)恢復時將占用較長的時間。
5. 同樣 log buffer 盡量設大點,比如說 8 MB。
6. 如果要存儲變長的字符串或字段可能會包含大量的 NULLs,請使用 VARCHAR 型字段代替 CHAR 。一個 CHAR(n) 字段總是使用 n bytes 來存儲數(shù)據(jù),即使這個字符串很短或是一個 NULL 值。較小的表更加適合緩沖池同時能夠減少磁盤 I/O 。
7. (適合從 3.23.41 以上版本) 在某些版本的 Linux 和 Unixes 中,使用 Unix fsync 或其它類似的方法將文件刷新到磁盤是異常地慢的。InnoDB 默認的方法就是 fsync 。如果你對數(shù)據(jù)庫系統(tǒng)的磁盤寫性能不能感到滿意,你可以嘗試在 my.cnf 中將 innodb_flush_method 設置為 O_DSYNC,盡管 O_DSYNC 選項在多數(shù)的系統(tǒng)上看起來比較慢。
8. 在向 InnoDB 導入數(shù)據(jù)時,請確認 Ubuntu mysql 沒有打開 autocommit=1 。否則每個插入語句都要將 log 刷新到磁盤。在你的 SQL 導入文件的***行加入
set autocommit=0;并在***一行加入commit;如果使用 Ubuntu mysqldump 選項 --opt,你將會得到一個快速導入 InnoDB 表的轉(zhuǎn)儲(dump)文件,甚至可以不再使用上面所提的 set autocommit=0; ... commit; 。
9. 小心 insert 集全的大回滾(roolback):在插入時 InnoDB 使用插入緩沖來減少磁盤 I/O,但在相應的回滾中卻沒有使用這樣的機制。一個 disk-bound rollback 可能會花費相應插入時間的 30 倍。如果發(fā)生一個失控的回滾,你可以查看第 6.1 章節(jié)的技巧來停止它。
10. 同樣也要小心一個大的 disk-bound 的操作。使用 Drop TABLE 或 TRUNCATE (從 Ubuntu mysql-4.0 以上) 來清空一個表,而不要使用 Delete FROM yourtable。
11. 如果需要插入大量記錄行可以使用多行(multi-line)的 Insert 來減少客戶端與服務器端的通信開銷:Insert INTO yourtable VALUES (1, 2), (5, 5);這個技巧對插入任何表均有效,而不僅僅是 InnoDB。
12. 如果在輔鍵上有 UNIQUE 約束,從 3.23.52 和 4.0.3 開始,可以通過在一個導入會話中將唯一鍵檢查(uniqueness check)關閉來提高數(shù)據(jù)導入速度:SET UNIQUE_CHECKS=0;一個大的表導入這將減少大量的磁盤 I/O,因為這時 InnoDB 可能使用自身的插入緩沖來分批地記錄輔助索引。
13. 如果在表中有一個子 FOREIGN KEY 約束,從 3.23.52 和 4.0.3 開始,可以通過在一個導入會話中將外鍵檢查(foreign key check)關閉來提高數(shù)據(jù)導入速度:SET FOREIGN_KEY_CHECKS=0;對一個大的表導入這將減少大量的磁盤 I/O。
9.1 InnoDB 監(jiān)視器(Monitors)從版本 3.23.42 開始,InnoDB 中就包含了 InnoDB Monitors,它可以顯示出 InnoDB 的內(nèi)部狀態(tài)。從版本 3.23.52 和 4.0.3 開始,你可以使用一個新的 SQL 命令SHOW INNODB STATUS來讀取標準 InnoDB Monitor 給 SQL client 的輸出信息。這些信息對性能調(diào)整有益。
另外一個使用 InnoDB Monitors 方法就是讓它在服務程序 Ubuntu mysqld 的標準輸出上持續(xù)地寫出信息。當開關打開時,InnoDB Monitors 大約每 15 秒顯示一次數(shù)據(jù)(注意:Ubuntu mysql 的客戶端并不會顯示任何東西)。
一個簡單地使用它的方法就是以一個命令行方式執(zhí)行 Ubuntu mysqld 。否則輸出將會定向到 Ubuntu mysql 服務錯誤日志(error log file)中 'yourhostname'.err (在 Windows 下為 Ubuntu mysql.err),在 Windows 系統(tǒng)中必須在 MS-DOS 使用提示符下以 --console 選項運行 Ubuntu mysqld-max 來指令信息輸出在命令提示符窗口上。
顯示的信息包含下列信息:
* 每一個活動的事務(active transaction)保持的表和記錄鎖定
* 事務的鎖等待 (lock waits of a transactions)
* 線程的信號量等待 (semaphore waits of threads)
* 文件 I/O 的等待請求 (pending file i/o requests)
* 緩沖池(buffer pool)的統(tǒng)計信息
* InnoDB 主線程的 purge buffer 和 insert buffer 歸并活動(merge activity)
通過下列的 SQL 命令,可以使標準的 InnoDB Monitor 記錄到標準的 Ubuntu mysqld 的輸出上:Create TABLE innodb_monitor(a int) type = innodb;通過它來停止:Drop TABLE innodb_monitor;
Create TABLE 句法只不過是為了通過 Ubuntu mysql SQL 語法分析而提供給 InnoDB 引擎命令的一種方式:那個被創(chuàng)建的表根本與 InnoDB Monitor 無任何關系。如果你在監(jiān)視器運行著的狀態(tài)下關閉數(shù)據(jù)庫,并且你需要再次啟動監(jiān)視器, 那么你不得不在發(fā)出一個新的 Create TABLE 來啟動監(jiān)視器之前先移除(drop)這個表。
與之相類似的,你可以啟動 innodb_lock_monitor ,它在某些方面與 innodb_monitor 一致,但是它會顯示更多的鎖定信息。一個單獨的 innodb_tablespace_monitor 將顯示在現(xiàn)有表空間內(nèi)所建立的文件段列表以及可以分配數(shù)據(jù)結(jié)構(gòu)的有效表空間。從 3.23.44 開始,提供了 innodb_table_monitor ,通過它可以獲得 InnoDB 內(nèi)部數(shù)據(jù)字典的信息。
3.23.52 中 InnoDB 輸出的示例:
- 020805 22:07:41 INNODB MONITOR OUTPUT
- =====================================
- Per second averages calculated from the last 3 seconds
- ----------
- SEMAPHORES
- ----------
- OS WAIT ARRAY INFO: reservation count 194, signal count 193
- --Thread 7176 has waited at ../include/btr0btr.ic line 28 for 0.00 seconds the s
- emaphore:
- X-lock on RW-latch at 44d980bc created in file buf0buf.c line 354
- a writer (thread id 7176) has reserved it in mode wait exclusive
- number of readers 1, waiters flag 1
- Last time read locked in file ../include/btr0btr.ic line 28
- Last time write locked in file ../include/btr0btr.ic line 28
- Mutex spin waits 0, rounds 0, OS waits 0
- RW-shared spins 77, OS waits 33; RW-excl spins 188, OS waits 161
- ------------
- TRANSACTIONS
- ------------
- Trx id counter 0 657853517
- Purge done for trx's n < 0 657853429 undo n < 0 80
- Total number of lock structs in row lock hash table 22
- 020805 22:07:36 LATEST DETECTED DEADLOCK:
- *** (1) TRANSACTION:
- TRANSACTION 0 657853503, ACTIVE 0 sec, OS thread id 15373 inserting
- LOCK WAIT 3 lock struct(s), heap size 336
- Ubuntu mysql thread id 6, query id 3741 localhost heikki update
- insert into ibtest11b (D, B, C) values (5, 'khdkkkk' ,'khdkkkk')
- *** (1) WAITING FOR THIS LOCK TO BE GRANTED:
- RECORD LOCKS space id 0 page no 104865 n bits 208 table test/ibtest11b index PRI
- MARY trx id 0 657853503 lock_mode X waiting
- Record lock, heap no 1 RECORD: info bits 0 0: len 9; hex 73757072656d756d00; asc
- supremum.;;
- *** (2) TRANSACTION:
- TRANSACTION 0 657853500, ACTIVE 0 sec, OS thread id 11275 setting auto-inc lock
- 19 lock struct(s), heap size 2672, undo log entries 5
- Ubuntu mysql thread id 2, query id 3750 localhost heikki update
- insert into ibtest11b (D, B, C) values (5, 'khD' ,'khD')
- *** (2) HOLDS THE LOCK(S):
- RECORD LOCKS space id 0 page no 104865 n bits 200 table test/ibtest11b index PRI
- MARY trx id 0 657853500 lock_mode X
- Record lock, heap no 1 RECORD: info bits 0 0: len 9; hex 73757072656d756d00; asc
- supremum.;;
- *** (2) WAITING FOR THIS LOCK TO BE GRANTED:
- TABLE LOCK table test/ibtest11b trx id 0 657853500 lock_mode AUTO-INC waiting
- *** WE ROLL BACK TRANSACTION (2)
- LIST OF TRANSACTIONS FOR EACH SESSION:
- ---TRANSACTION 0 657853516, ACTIVE 5 sec, OS thread id 15373 setting auto-inc lo
- ck
- LOCK WAIT 1 lock struct(s), heap size 336
- Ubuntu mysql thread id 6, query id 3895 localhost heikki update
- insert into ibtest11b (D, B, C) values (5, 'khdkkkk' ,'khdkkkk')
- ------- TRX HAS BEEN WAITING 5 SEC FOR THIS LOCK TO BE GRANTED:
- TABLE LOCK table test/ibtest11b trx id 0 657853516 lock_mode AUTO-INC waiting
- ------------------
- ---TRANSACTION 0 657853514, ACTIVE 5 sec, OS thread id 11275 inserting
- LOCK WAIT 13 lock struct(s), heap size 2672, undo log entries 2
- Ubuntu mysql thread id 2, query id 3898 localhost heikki update
- insert into ibtest11d (D, B, C) values (5, 'khdkkkk' ,'khdkkkk')
- ------- TRX HAS BEEN WAITING 5 SEC FOR THIS LOCK TO BE GRANTED:
- RECORD LOCKS space id 0 page no 104879 n bits 384 table test/ibtest11d index B t
- rx id 0 657853514 lock_mode X gap type lock waiting
- Record lock, heap no 130 RECORD: info bits 32 0: len 9; hex 6b48646b6b6b6b6b6b;
- asc kHdkkkkkk;; 1:
- ------------------
- ---TRANSACTION 0 657853512, ACTIVE 5 sec, OS thread id 14348 updating or deletin
- g
- 20 lock struct(s), heap size 2672, undo log entries 175
- Ubuntu mysql thread id 5, query id 3874 localhost heikki updating
- delete from ibtest11a where A = 215
- --------
- FILE I/O
- --------
- I/O thread 0 state: waiting for i/o request
- I/O thread 1 state: waiting for i/o request
- I/O thread 2 state: waiting for i/o request
- I/O thread 3 state: waiting for i/o request
- Pending normal aio reads: 0, aio writes: 0,
- ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
- Pending flushes (fsync) log: 0; buffer pool: 0
- 272 OS file reads, 56 OS file writes, 29 OS fsyncs
- 0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
- -------------------------------------
- Insert BUFFER AND ADAPTIVE HASH INDEX
- -------------------------------------
- Ibuf for space 0: size 1, free list len 5, seg size 7,
- 0 inserts, 0 merged recs, 0 merges
- Hash table size 124633, used cells 1530, node heap has 4 buffer(s)
- 2895.70 hash searches/s, 126.62 non-hash searches/s
- ---
- LOG
- ---
- Log sequence number 19 3267291494
- Log flushed up to 19 3267283711
- Last checkpoint at 19 3266545677
- 0 pending log writes, 0 pending chkp writes
- 30 log i/o's done, 0.00 log i/o's/second
- ----------------------
- BUFFER POOL AND MEMORY
- ----------------------
- Total memory allocated 82593970; in additional pool allocated 1406336
- Buffer pool size 1920
- Free buffers 1711
- Database pages 205
- Modified db pages 39
- Pending reads 0
- Pending writes: LRU 0, flush list 0, single page 0
- Pages read 178, created 27, written 50
- 0.00 reads/s, 0.00 creates/s, 0.00 writes/s
- Buffer pool hit rate 1000 / 1000
- --------------
- ROW OPERATIONS
- --------------
- 1 queries inside InnoDB, 0 queries in queue; main thread: purging
- Number of rows inserted 2008, updated 264, deleted 162, read 9
- 0.00 inserts/s, 0.00 updates/s, 14.66 deletes/s, 0.00 reads/s
- ----------------------------
- END OF INNODB MONITOR OUTPUT
- ============================
輸出信息的某些注意點:
* 如果 TRANSACTIONS 部分報告鎖定等待(lock waits),那么你的應用程序可能有鎖爭用(lock contention)。輸出信息可以幫助跟蹤事務死鎖的原因。
* SEMAPHORES 部分報告線程等待信號量以及統(tǒng)計出線程需要旋轉(zhuǎn)(spin)或等待(wait)一個互斥(mutex)或 rw-lock 信號量的次數(shù)。一個較大的線程等待信號量的次數(shù)可能是由于磁盤 I/O 引起,或 InnoDB 內(nèi)部的爭用問題(contention problems)。爭用(Contention)可能是由于比較繁重的并發(fā)性查詢,或操作系統(tǒng)的線程調(diào)度的問題。 在這種情形下,可將 innodb_thread_concurrency 設置地小于默認的 8 。
* FILE I/O 部分列出了文件 I/O 的等待請求。過大的值就意味著磁盤 I/O 瓶頸。
* BUFFER POOL AND MEMORY 部分給出了頁面讀寫的統(tǒng)計。通過這些值可以計算出你的查詢通常所需的數(shù)據(jù)文件 I/O 量。
【編輯推薦】