zoukankan      html  css  js  c++  java
  • mysql(4)

    mysql 用户的创建:必须是root用户

      1,进入到mysql数据库下

          mysql> use mysql
          Database changed

      2,对新用户的增删改查:

        创建新用户;

          # 指定ip:192.118.1.1的mjj用户登录
            create user 'mjj'@'192.118.1.1' identified by '123';
          # 指定ip:192.118.1.开头的mjj用户登录
            create user 'mjj'@'192.118.1.%' identified by '123';
          # 指定任何ip的mjj用户登录

             create user 'mjj'@'%' identified by '123';

      3,删除用户:

        drop user '用户名'@'ip地址'

      4,修改密码:

        set password for '用户名'@'ip地址'=password('新密码');

    授权用户:

      show grants for '用户'@'ip地址'  查看用户的权限

     连接数据库: mysql -h 192.168.19.200 -utest -p

    #授权 mjj用户仅对db1.t1文件有查询、插入和更新的操作
    grant select ,insert,update on db1.t1 to "mjj"@'%';
    
    # 表示有所有的权限,除了grant这个命令,这个命令是root才有的。mjj用户对db1下的t1文件有任意操作
    grant all privileges  on db1.t1 to "mjj"@'%';
    #mjj用户对db1数据库中的文件执行任何操作
    grant all privileges  on db1.* to "mjj"@'%';
    #mjj用户对所有数据库中文件有任何操作
    grant all privileges  on *.*  to "mjj"@'%';
     
    #取消权限
     
    # 取消mjj用户对db1的t1文件的任意操作
    revoke all on db1.t1 from 'mjj'@"%";  

    # 取消来自远程服务器的mjj用户对数据库db1的所有表的所有权限

    revoke all on db1.* from 'mjj'@"%";  

    取消来自远程服务器的mjj用户所有数据库的所有的表的权限 revoke all privileges on *.* from 'mjj'@'%';

    4,mysql备份命令行操作 默认会存到自己前方的地址

      数据结构 + 数据

      mysqldump -u root db1> db1.sql -p

      数据表结构

      mysqldump -u root -d db1>db1.sql -p 

      导入现有的某个数据库

      create database db10  新创建

      mysql -u root  db10 < db1.aql -p

    4索引 index

      

    无索引: 从前往后一条一条查询
    有索引:创建索引的本质,就是创建额外的文件(某种格式存储,查询的时候,先去格外的文件找,定好位置,然后再去原始表中直接查询。但是创建索引越多,会对硬盘也是有损耗。
    建立索引的目的:
    a.额外的文件保存特殊的数据结构
    b.查询快,但是插入更新删除依然慢
    c.创建索引之后,必须命中索引才能有效
    无索引和有索引的区别以及建立索引的目的

      作用:约束和加速查询

      300w套数据

      select name from user where id =200000;

      设置索引之后,会额外生成一个额外的索引文件夹

    1,普通索引:

      作用:加速查询

      创建   create index ix_name on userinfo(name)

      或者:create table userinfo(
                       nid int not null auto_increment primary key,
                       name varchar(32) not null,
                       email varchar(64) not null,
                       index ix_name(name)
                   );

      删除 : drop 索引的名字 on 表名

      查看 index from 表名

      唯一的索引

        加速查找和唯一约束

      create unique index ix_emial on userinfo(email)

      drop index ix_email on userinfo

     主键索引

      约束 加速查询

       索引名词 : 

        覆盖索引(直接从索引文件中获取数据)

          select name from userinfo where name = 'alex111'    name 和后边的条件

        索引合并:(两个单列索引) 

          select * form userinfo where id=2 and name = 'alex2'

      

    create table userinfo(
    
                       id int not null auto_increment primary key,
                       name varchar(32) not null,
                       email varchar(64) not null,
                       unique  index  ix_name(name)
               )
              or
    
               create table userinfo(
    
                       id int not null auto_increment,
                       name varchar(32) not null,
                       email varchar(64) not null,
                       primary key(nid),
                       unique  index  ix_name(name)
             )
    
    创建表+主键索引
    

     创建 alter table 表名 add primary key(列名)

      联合索引

        联合普通索引

          create index ix_name_emial on userinfo (name,email)

           在左边的大于右边的,单独拿出来的话两个可以一起更为快

     正确使用索引:

      

    - like '%xx'
                select * from userinfo where name like '%al';
            - 使用函数
                select * from userinfo where reverse(name) = 'alex333';
            - or
                select * from userinfo where id = 1 or email = 'alex122@oldbody';
                特别的:当or条件中有未建立索引的列才失效,以下会走索引
                        select * from userinfo where id = 1 or name = 'alex1222';
                        select * from userinfo where id = 1 or email = 'alex122@oldbody' and name = 'alex112'
            - 类型不一致
                如果列是字符串类型,传入条件是必须用引号引起来,不然...
                select * from userinfo where name = 999;
            - !=
                select count(*) from userinfo where name != 'alex'
                特别的:如果是主键,则还是会走索引
                    select count(*) from userinfo where id != 123
            - >
                select * from userinfo where name > 'alex'
                特别的:如果是主键或索引是整数类型,则还是会走索引
                    select * from userinfo where id > 123
                    select * from userinfo where num > 123
            - order by
                select email from userinfo order by name desc;
                当根据索引排序时候,选择的映射如果不是索引,则不走索引
                特别的:如果对主键排序,则还是走索引:
                    select * from userinfo order by nid desc;
             
            - 组合索引最左前缀
                如果组合索引为:(name,email)
                name and email       -- 使用索引
                name                 -- 使用索引
                email                -- 不使用索引
    

      左前缀:

    最左前缀匹配:
            create index ix_name_email on userinfo(name,email);
                     select * from userinfo where name = 'alex';
                     select * from userinfo where name = 'alex' and email='alex@oldBody';
                     select * from userinfo where  email='alex@oldBody';
                 如果使用组合索引如上,name和email组合索引之后,查询
                 (1)name和email ---使用索引
                 (2)name        ---使用索引
                 (3)email       ---不适用索引
                  对于同时搜索n个条件时,组合索引的性能好于多个单列索引
            ******组合索引的性能>索引合并的性能*********
    (1)避免使用select *
           (2)count(1)或count(列) 代替count(*)
           (3)创建表时尽量使用char代替varchar
           (4)表的字段顺序固定长度的字段优先
           (5)组合索引代替多个单列索引(经常使用多个条件查询时)
           (6)尽量使用短索引 (create index ix_title on tb(title(16));特殊的数据类型 text类型)
           (7)使用连接(join)来代替子查询
           (8)连表时注意条件类型需一致
           (9)索引散列(重复少)不适用于建索引,例如:性别不合适
    

      

  • 相关阅读:
    iOS APM性能统计
    iOS promise
    静态代码检测工具Infer的部署
    ES读写数据的工作原理
    关于 Elasticsearch 内存占用及分配
    Elasticsearch中数据是如何存储的
    ES中的选举机制
    .net core 3.1 webapi解决跨域问题 GET DELETE POST PUT等
    .net framework WEBAPI跨域问题
    Angular前端项目发布到IIS
  • 原文地址:https://www.cnblogs.com/lnrick/p/9566730.html
Copyright © 2011-2022 走看看