14.1.2 Best Practices for InnoDB Tables

This section describes best practices when using InnoDB tables.
下面介紹了innodb表的最佳實踐

  • Specifying a primary key for every table using the most frequently queried column or columns, or an auto-increment value if there is no obvious primary key.
  • 爲每個表指定使用最頻繁的一列或多列爲主鍵索引,如果沒有明顯的主鍵,則指定自動增量值。

  • Using joins wherever data is pulled from multiple tables based on identical ID values from those tables. For fast join performance, define foreign keys on the join columns, and declare those columns with the same data type in each table. Adding foreign keys ensures that referenced columns are indexed, which can improve performance. Foreign keys also propagate deletes or updates to all affected tables, and prevent insertion of data in a child table if the corresponding IDs are not present in the parent table.
  • 使用joins,根據這些表中的相同ID值從多個表中提取數據。爲了提高join性能,可以在join的列上定義外鍵,並且將這些字段設置爲相同的數據類型,添加外鍵確保引用列被索引,這樣的話,會提高性能,外鍵通常會轉播deletes或updates操作在所有受影響的表中,並且如果父表中沒有相應的ID,可以防止在子表中插入數據。
  • Turning off autocommit. Committing hundreds of times a second puts a cap on performance (limited by the write speed of your storage device).
  • 關閉autocommit,每秒提交幾百次會限制性能(受限於存儲設備的寫入速度)
  • Grouping sets of related DML operations into transactions, by bracketing them with START TRANSACTION and COMMIT statements. While you don't want to commit too often, you also don't want to issue huge batches of INSERT, UPDATE, or DELETE statements that run for hours without committing.
  • 如果你不想經常提交,也不想大批量的執行insert,updte,delte操作而導致數小時不提交,那麼你可以將想相關的DML 語句分組,然後放入到事務中執行,事務通常用START TRANSACTION語句開頭和COMMIT語句結尾
  • Not using LOCK TABLES statements. InnoDB can handle multiple sessions all reading and writing to the same table at once, without sacrificing reliability or high performance. To get exclusive write access to a set of rows, use the SELECT ... FOR UPDATE syntax to lock just the rows you intend to update.
  • 不要使用LOCK_TABLES語句,InnoDB可以同時處理所有讀取和寫入到同一個表的會話。,而不會犧牲可靠性和高性能。要獲得對行集合的獨佔寫入訪問,請使用 SELECT ... FOR UPDATE語法鎖定您打算更新的行。
  • Enabling the innodb_file_per_table option to put the data and indexes for individual tables into separate files, instead of in a single giant system tablespace. This setting is required to use some of the other features, such as table compression and fast truncation.
  • 開啓innodb_file_per_table選項,將單個表的數據和索引放入單獨的文件中,而不是在一個單一的大的系統表空間中,此設置需要使用其他一些特性,例如表壓縮和快速truncation.
  • The innodb_file_per_table option is enabled by default as of MySQL 5.6.6.
  • innodb_file_per_table 選項在MySQL 5.6.6中是默認開啓的
  • Evaluating whether your data and access patterns benefit from the InnoDB table compression feature (ROW_FORMAT=COMPRESSED) on the CREATE TABLE statement. You can compress InnoDB tables without sacrificing read/write capability.
  • 可以在CREATE TABLE 語句後面加上ROW_FORMAT=COMPRESSED來評估您的數據和訪問模式是否有益,你可以壓縮innodb表並且不會犧牲讀寫能力
  • Running your server with the option --sql_mode=NO_ENGINE_SUBSTITUTION to prevent tables being created with a different storage engine if there is an issue with the engine specified in the ENGINE= clause of CREATE TABLE.
  • 啓動服務的時候加上--sql_mode=NO_ENGINE_SUBSTITUTION參數,如果在 CREATE TABLE.的 ENGINE=字句 中指定的引擎存在問題,可以防止使用其他存儲引擎創建表
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章