添加索引
1.添加PRIMARY KEY(主键索引)
ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` )
2.添加UNIQUE(唯一索引)
ALTER TABLE `table_name` ADD UNIQUE ( `column` )
3.添加INDEX(普通索引)
ALTER TABLE `table_name` ADD INDEX index_name ( `column` )
4.添加FULLTEXT(全文索引)
ALTER TABLE `table_name` ADD FULLTEXT ( `column`)
5.添加多列索引
ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )
在一个表而不在另一个表中的数据
1. 使用 NOT IN ,容易理解,效率低
SELECT DISTINCT A.ID FROM A WHERE A.ID NOT IN (SELECT ID FROM B)
2. 使用 LEFT JOIN...ON... , “B.ID IS NULL”表示左连接之后在B.ID字段为NULL的记录
SELECT A.ID FROM A LEFT JOIN B ON A.ID=B.ID WHERE B.ID IS NULL
3. 逻辑相对复杂,但是速度最快
SELECT * FROM B WHERE (SELECT COUNT(1) AS NUM FROM A WHERE A.ID = B.ID) = 0
导出多张表的表结构
mysqldump -uxxx -p --set-gtid-purged=OFF -d -B TABLE_1 TABLE_2 TABLE_3 > /home/xxx/table.sql
只导出数据不导出表结构
mysqldump -uxxx -p --set-gtid-purged=OFF -t database tablename > /home/xxx/data.sql
导出数据和表结构
mysqldump -uxxx -p database > /home/xxx/export.sql
MySQL安装为Windows服务
mysqld install --defaults-file="C:\ProgramData\MySQL\MySQLg\my.ini" MySQL
查看和修改当前连接数
show processlist
修改my.ini,max_connections=xxx
查看Event执行情况
SELECT * FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_NAME = 'EVT_NAME' AND EVENT_SCHEMA = 'SCHEMA';
在Mac OS X启停MySQL服务
sudo /usr/local/mysql/support-files/mysql.server start/stop/restart