mysqld —initialized
window下mysql的服务管理
mysql --version
mysqld install mysql_server_name
SC query server_name
net start server_name
mysql -u username -p
net stop server_name
SC delete server_name
mysql 用户管理
**create user
username@
hostnameidentified by 'password';**
**set password [for
username@
hostname] = password('password');**
**grant all [pivileges] on db_test.storage_file to
admin@
localhost;**
**grant select,update,insert on db_test.storage_file to
admin@
localhost;**
**revoke insert,update,delete on db_test.* from
admin@
localhost;**
**revoke all on db_test.storage_file from
admin@
localhost;**
**drop user
admin@
localhost;**
mysql 数据库管理
mysql 数据表管理
join / left join / right join
join,内连接,是 inner join 的简写,返回的结果集是两张连接表都满足连接条件的数据。
left join, 左外连接,是 left outer join的简写, 返回的结果集是连接表左表全部数据 + 右表满足连接条件的数据。
right join, 右外连接,是 right outer join的简写, 返回的结果集是连接表右表全部数据 + 左表满足连接条件的数据。
full join,mysql不支持,全连接 返回两张表连接的所有数据 select from a left join b on condition union select from a right join b on condition
复杂查询
- 查询所有(某种)材料最近三次入库时间的库房库存信息
复杂业务简单描述:在一个材料入库业务中不同机构的不同材料入库在不同的库房(货架)中
假设只有两张表(实际不止,还有材料库存预设等):表A 入库明细信息,表B 库房库存明细信息
涉及:连接查询、分组查询
索引
建索引的几大原则
https://tech.meituan.com/2014/06/30/mysql-index.html
1. 最左前缀匹配原则
非常重要的原则,mysql会一直向右匹配直到遇到范围查询(>、<、between、like)就停止匹配,比如a = 1 and b = 2 and c > 3 and d = 4 如果建立(a,b,c,d)顺序的索引,d是用不到索引的,如果建立(a,b,d,c)的索引则都可以用到,a,b,d的顺序可以任意调整。
2. 尽量选择区分度高的列作为索引
区分度的公式是count(distinct col)/count(*),表示字段不重复的比例,比例越大我们扫描的记录数越少,唯一键的区分度是1,而一些状态、性别字段可能在大数据面前区分度就是0,那可能有人会问,这个比例有什么经验值吗?使用场景不同,这个值也很难确定,一般需要join的字段我们都要求是0.1以上,即平均1条扫描10条记录。但在一些特殊的情况下,这种理论是有局限性的
3. 索引列不能参与计算
保持列“干净”,比如from_unixtime(create_time) = ‘2014-05-29’ 就不能使用到索引,原因很简单,b+树中存的都是数据表中的字段值,但进行检索时,需要把所有元素都应用函数才能比较,显然成本太大。所以语句应该写成 create_time = unix_timestamp(‘2014-05-29’)。
4. 尽量的扩展索引,不要新建索引
比如表中已经有a的索引,现在要加(a,b)的索引,那么只需要修改原来的索引即可
查询优化
- 建索引需要把这个表的所有相关查询都找到,综合分析;
- 建索引按原则来
查询优化神器 - explain命令
https://dev.mysql.com/doc/refman/8.0/en/explain-output.html
MySQL SQL Error: 1064, SQLState: 42000 错误
在 MySQL 中,你不小心使用了一些关键字,那么就会报这个错误。(我创建了表字段current_user,遇见该错误)
1 | 2 | 3 |
---|---|---|
ADD | ALL | ALTER |
ANALYZE | AND | AS |
ASC | ASENSITIVE | BEFORE |
BETWEEN | BIGINT | BINARY |
BLOB | BOTH | BY |
CALL | CASCADE | CASE |
CHANGE | CHAR | CHARACTER |
CHECK | COLLATE | COLUMN |
CONDITION | CONNECTION | CONSTRAINT |
CONTINUE | CONVERT | CREATE |
CROSS | CURRENT_DATE | CURRENT_TIME |
CURRENT_TIMESTAMP | CURRENT_USER | CURSOR |
DATABASE | DATABASES | DAY_HOUR |
DAY_MICROSECOND | DAY_MINUTE | DAY_SECOND |
DEC | DECIMAL | DECLARE |
DEFAULT | DELAYED | DELETE |
DESC | DESCRIBE | DETERMINISTIC |
DISTINCT | DISTINCTROW | DIV |
DOUBLE | DROP | DUAL |
EACH | ELSE | ELSEIF |
ENCLOSED | ESCAPED | EXISTS |
EXIT | EXPLAIN | FALSE |
FETCH | FLOAT | FLOAT4 |
FLOAT8 | FOR | FORCE |
FOREIGN | FROM | FULLTEXT |
GOTO | GRANT | GROUP |
HAVING | HIGH_PRIORITY | HOUR_MICROSECOND |
HOUR_MINUTE | HOUR_SECOND | IF |
IGNORE | IN | INDEX |
INFILE | INNER | INOUT |
INSENSITIVE | INSERT | INT |
INT1 | INT2 | INT3 |
INT4 | INT8 | INTEGER |
INTERVAL | INTO | IS |
ITERATE | JOIN | KEY |
KEYS | KILL | LABEL |
LEADING | LEAVE | LEFT |
LIKE | LIMIT | LINEAR |
LINES | LOAD | LOCALTIME |
LOCALTIMESTAMP | LOCK | LONG |
LONGBLOB | LONGTEXT | LOOP |
LOW_PRIORITY | MATCH | MEDIUMBLOB |
MEDIUMINT | MEDIUMTEXT | MIDDLEINT |
MINUTE_MICROSECOND | MINUTE_SECOND | MOD |
MODIFIES | NATURAL | NOT |
NO_WRITE_TO_BINLOG | NULL | NUMERIC |
ON | OPTIMIZE | OPTION |
OPTIONALLY | OR | ORDER |
OUT | OUTER | OUTFILE |
PRECISION | PRIMARY | PROCEDURE |
PURGE | RAID0 | RANGE |
READ | READS | REAL |
REFERENCES | REGEXP | RELEASE |
RENAME | REPEAT | REPLACE |
REQUIRE | RESTRICT | RETURN |
REVOKE | RIGHT | RLIKE |
SCHEMA | SCHEMAS | SECOND_MICROSECOND |
SELECT | SENSITIVE | SEPARATOR |
SET | SHOW | SMALLINT |
SPATIAL | SPECIFIC | SQL |
SQLEXCEPTION | SQLSTATE | SQLWARNING |
SQL_BIG_RESULT | SQL_CALC_FOUND_ROWS | SQL_SMALL_RESULT |
SSL | STARTING | STRAIGHT_JOIN |
TABLE | TERMINATED | THEN |
TINYBLOB | TINYINT | TINYTEXT |
TO | TRAILING | TRIGGER |
TRUE | UNDO | UNION |
UNIQUE | UNLOCK | UNSIGNED |
UPDATE | USAGE | USE |
USING | UTC_DATE | UTC_TIME |
UTC_TIMESTAMP | VALUES | VARBINARY |
VARCHAR | VARCHARACTER | VARYING |
WHEN | WHERE | WHILE |
WITH | WRITE | X509 |
XOR | YEAR_MONTH | ZEROFILL |