Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

MySQL: เปลี่ยน Storage Engine จาก InnoDB เป็น MyISAM

สร้างตารางตัวอย่าง

mysql> create table DemoTable1982 ( StudentId int, StudentName varchar(20), StudentAge int ); Query OK, 0 rows affected (0.00 sec)

ตรวจสอบ Engine ปัจจุบัน

mysql> show create table DemoTable1982;
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table         | Create Table                                                                                                                                                                                                                           |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DemoTable1982 | CREATE TABLE `demotable1982` (`StudentId` int(11) DEFAULT NULL, `StudentName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, `StudentAge` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

เห็นว่า Engine เริ่มต้นคือ InnoDB

เปลี่ยน Engine เป็น MyISAM

mysql> alter table DemoTable1982 ENGINE='MyISAM'; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0

ตรวจสอบอีกครั้ง

mysql> show create table DemoTable1982;
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table         | Create Table                                                                                                                                                                                                                           |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DemoTable1982 | CREATE TABLE `demotable1982` ( `StudentId` int(11) DEFAULT NULL, `StudentName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, `StudentAge` int(11) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

ตอนนี้ Engine เปลี่ยนเป็น MyISAM แล้ว

ข้อควรพิจารณา

  • InnoDB: รองรับ Transaction, Foreign Key, Row-level Locking เหมาะสำหรับระบบที่ต้องการความสมบูรณ์ของข้อมูลสูง
  • MyISAM: เร็วกว่าในการอ่าน (Read-heavy), รองรับ Full-text Search เดิม แต่ไม่รองรับ Transaction และ Foreign Key
  • ควรสำรองข้อมูลก่อนเปลี่ยน Engine เพราะอาจส่งผลต่อประสิทธิภาพและฟีเจอร์ที่ใช้งานอยู่