mysql存儲引擎的知識
最經常使用的搜索引擎爲myisam5.5.5之前默認存儲引擎和innodb全文搜索引擎兩個都已經支持
.myd數據,.myi索引,.frm表的定義
[root@localhost mysql]# file user.MYI
user.MYI: MySQL MISAM compressed data file Version 1
支持事務innodb,ndb
事務介紹
原子性:要麼都成功,要麼都失敗
一致性:事務發生前和發生後,數據的完整性保持一致
隔離性:當併發執行數據庫時,一個正在執行的時候,在執行完成前別的
事務看不到當前狀態
持久性:一個事務一旦被提交,他對數據庫的數據改變是永久性的
事務的開啓
數據庫的默認事務是自動提交的
數據庫事務開啓命令
start transaction
rollback
commmit
set autocommit=0禁止自動提交
set autocommit=1開啓自動提交
1myisam不支持事務,
2表級鎖定
3讀寫相互阻塞
4只會緩存索引
5讀取速度快,佔用資源少
6不支持外鍵約束(鏈接查詢),但支持全文索引
生產場景
1.不支持事務的業務
2併發相對較低
3讀數據較多的應用,讀寫都頻繁的都不適合,讀多或者寫多適合
4以讀爲主的業務,列如blog
5對數據的一致性要求不是特別高的業務
6 硬件資源比較差的機器
myisam調優精要
設置合適的索引,緩存機制
調整讀寫優先級,根據實際需求確保重要操做更優先執行
3啓用延遲插入改善大批量寫入性能,儘量多條數據一次性寫入
4儘可能順序操做讓insert數據寫入尾部,減小阻塞,
5分解大的操做,下降單個操做的阻塞時間
6下降併發數減小對mysql訪問,某些高併發場景應使用排隊隊列機制
使用cache或者nosql
query_cache_size=2M
query_cache_limit=1M
query_chache_min_res_unit=2K
innodb引擎
優勢
1支持事務的四個級別
2行級鎖定,全表掃描表說
3,讀寫策越和事務隔離相關
4具備很是高的緩存,能夠緩存索引,也能夠緩存數據
5整個表和主鍵以cluster方式存儲,組成一個平衡樹
6全部secondary index都會保存主鍵信息
7支持分區表空間
8支持外鍵約束,不支持全文索引(5.5之前),之後支持了
9對硬件資源要求比較高
innodb生產場景
1須要支持事務的業務
2行級鎖定對高併發有較好的適應能力,
3數據讀寫及跟新都較爲頻繁的場景
4數據一致性比較高的業務
5硬件內存比較大的
mysql> show variables like '%innodb%';
+---------------------------------+------------------------+
| Variable_name | Value |
+---------------------------------+------------------------+
| have_innodb | YES |
| ignore_builtin_innodb | OFF |
| innodb_adaptive_flushing | ON |
| innodb_adaptive_hash_index | ON |
| innodb_additional_mem_pool_size | 16777216 |
| innodb_autoextend_increment | 8 |
| innodb_autoinc_lock_mode | 1 |
| innodb_buffer_pool_instances | 1 |
| innodb_buffer_pool_size | 209715200 |
| innodb_change_buffering | all |
| innodb_checksums | ON |
| innodb_commit_concurrency | 0 |
| innodb_concurrency_tickets | 500 |
| innodb_data_file_path | ibdata1:10M:autoextend |
| innodb_data_home_dir | |--
| innodb_doublewrite | ON |
| innodb_fast_shutdown | 1 |
| innodb_file_format | Antelope |
| innodb_file_format_check | ON |
| innodb_file_format_max | Antelope |
| innodb_file_per_table | ON |--
| innodb_flush_log_at_trx_commit | 1 |
| innodb_flush_method | |
| innodb_force_load_corrupted | OFF |
| innodb_force_recovery | 0 |
| innodb_io_capacity | 200 |
| innodb_large_prefix | OFF |
| innodb_lock_wait_timeout | 120 |
| innodb_locks_unsafe_for_binlog | OFF |
| innodb_log_buffer_size | 16777216 |
| innodb_log_file_size | 536870912 |
| innodb_log_files_in_group | 3 |
| innodb_log_group_home_dir | ./ |
| innodb_max_dirty_pages_pct | 60 |
| innodb_max_purge_lag | 0 |
| innodb_mirrored_log_groups | 1 |
| innodb_old_blocks_pct | 37 |
| innodb_old_blocks_time | 0 |
| innodb_open_files | 300 |
| innodb_print_all_deadlocks | OFF |
| innodb_purge_batch_size | 20 |
| innodb_purge_threads | 0 |
| innodb_random_read_ahead | OFF |
| innodb_read_ahead_threshold | 56 |
| innodb_read_io_threads | 4 |
| innodb_replication_delay | 0 |
| innodb_rollback_on_timeout | OFF |
| innodb_rollback_segments | 128 |
| innodb_spin_wait_delay | 6 |
| innodb_stats_method | nulls_equal |
| innodb_stats_on_metadata | ON |
| innodb_stats_sample_pages | 8 |
| innodb_strict_mode | OFF |
| innodb_support_xa | ON |
| innodb_sync_spin_loops | 30 |
| innodb_table_locks | ON |
| innodb_thread_concurrency | 16 |
| innodb_thread_sleep_delay | 10000 |
| innodb_use_native_aio | OFF |
| innodb_use_sys_malloc | ON |
| innodb_version | 5.5.32 |
| innodb_write_io_threads | 4 |
+---------------------------------+------------------------+
62 rows in set (0.08 sec)
表名.frm
調優
1主鍵儘量比較小
2避免全表掃描
3儘量緩存全部數據和索引,減小io
4在大批量小插入的時候,儘可能本身控制事務,不要使用autocommit提交
5合理使用innodb_flush_log_at_trx_commit參數值,不要過分最求安全
若爲0.log buffer 會刷新日誌文件到磁盤,提交事務的時候不會作任何操做
6避免主鍵跟新
查看哪些引擎可用
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
8 rows in set (0.04 sec)
生產環境如何批量改存儲引擎
alter table tt engine='MyISAM';
2.使用sed對備分內容進行替換
which mysql_convert_table_format --host --user= -type= -e
default_table_type=InnoDB
mysql