1视图:
 -创建
  create view 视图名称 as SQL
  ps:虚拟
 -修改
  alter view 视图名称 as SQL
 -删除
  drop view 视图名称
2触发器
  
3自定义函数:
 
 delimiter
  create function f1(
   i1 int,
   i2 int)
  returns int
  BEGIN
   declare num int default 0;
   set num = i1 + i2
   return (num);
 delimiter;
 
 select f1(1,100);
  
  )
  
4存储过程:
 1.简单
  Create procedure p1()
  BEGIN
   select * from student;
   INSERT into teacher(tname) values('ct');
  END
  call p1()
  cursor.callproc('p1')
 2.传参数(in, out, inout)
  delimiter//
  create procedure p2(
   in n1 int,
   in n2 int
  )
  BEGIN
   select * from student where sid > n1;
  END //
  delimiter;
  
  call p2(12,2)
  cursor.callproc('p2', (12,2))
 3.参数 out
  delimiter//
  create procedure p2(
   in n1 int,
   out n2 int
  )
  BEGIN
   set n2 = 123123;
   select * from student where sid > n1;
  END//
  delimiter;
  
  set @v1 = 0;
  call p2(12,@v1)
  @v1
  
 4.事务
  
  delimiter //
  create procedure p4()
  BEGIN
   1.声明如果出现异常则执行{
    set status = 1
    rollback;
   }
   开始事务
    -- 由a账户减去100
    -- b 账户加90
    -- c 账户加10
    commit;
   结束
   
   set status = 2;
  END//
  delimiter;
  ================
  
  delimiter\\
  create PROCEDURE p5(
   OUT p_return_code tinyint
  )
  BEGIN
   -- ERROR
   set p_return_code = 1;
   rollback;
  END;
  START TRANSACTION;
   DELETE from tb1;
   insert into tb2 (name) values('seven');
  COMMIT;
  -- SUCCESS
  set p_return_code = 2;
  END\\
  delimiter;
 
 5.游标
  delimiter//
  create procedure p6()
  begin
   declare row_id int;  --自定义变量1
   declare row_num int;  --自定义变量2
   declare done INT DEFAULT FALSE;
   declare temp int;
   declare my_cursor CURSOR FOR select id,num from A;
   declare CONTINUE HANDLER FOR NOT FOUND SET done = RRUE;
   open my_cursor;
    xxoo: LOOP
     fetch my_cursor into row_id, row_num;
     if done then
      leave xxoo;
     END IF;
     set temp = row_id + row_num;
     insert into B(number) values(temp);
    end loop xxoo;
   close my_cursor;
 6.动态执行SQL(防止SQL注入)
 
 
 伪代码
  delimiter//
  create procedure p7(
   in tpl varchar(255),
   in arg int
  )
  begin
   1.预检测 SQL语句合法性
   2. SQL = 格式化 tpl + arg
   3. 执行SQL语句
   
   set @xo = arg;
   EXECUTE xxx FROM 'select * from student where sid >?';
   PREPARE  xxx USING @xo;
   DEALLOCATE prepare prod;
  end//
  delimter;
  call p7("select * from tb where id>?",9)
  
 真代码
  delimiter\\
  CREATE PROCEDURE p8(
   in nid int
  )
  BEGIN
   PREPARE prod FROM 'select * from student where sid > ?';
   EXECUTE prod USING @nid;
   DEALLOCATE prepare prod;
  END\\
  delimiter;
   
5索引
 作用
  -约束
  -加速查找
 索引种类:
  -普通所以:加速查找
  -主键索引:加速查找+不能为空+不能重复
  -唯一索引:加速查找+不能重复
  -联合索引(多列)
   -联合主键索引
   -联合唯一索引
   -联合普通索引
  
 hash索引:
  ps:按哈希值排序
  单值快
  
 btree索引
  ps:按二叉树查找
  二叉树
       
   创建索引:
    - 额外的文件保存特殊的数据结构
    - 查询快;插入更新慢
    - 命中索引
    普通索引:
     create index ix_name on userinfo1(email);   建
     drop index 索引名 on 表名(列名)   删
    唯一索引:
     create unique 索引名称 on 表名(列名)
    联合索引(组合): 最左前缀匹配
     create index 索引名称 on 表名(列名,列名)
    
    覆盖索引:
     - 在索引文件中直接获取数据
    索引合并:
     - 把多个单列索引合并使用
    
慢日志:
 -执行时间>10
 -未命中索引
 -日志文件路径
配置
 -内存
  show variables like '%query%'
  set gobal 变量名 = 值
 -配置文件
  mysqld --defaults-file='文件路径'
  my.conf内容:
   slow_query_log = ON
   slow_query_log_file = D:/...
  注意:修改配置文件之后,需要重启服务
  
 ===分页===
 
 a.select * from userinfo3 limit 20,10;
 b.
  -不让看
  -索引表中扫:
   select * from userinfo3 where id in(select id from userinfo3 limit 200000,10);
  -方案:
   记录当前页最大或最小ID
   1.页面只有上一页, 下一页
    # max_id
    # min_id
    下一页:
     select * from userinfo3 where id> max_id limit 10;
    上一页:
     select * from userinfo3 where id< min_id order by id desc limit 10;
   
   2.上一页 192 193 [196] 197 198 199 下一页:
    select * from userinfo3 where id in(
     select id from (select id from userinfo3 where id> max_id limit 30) as N order by N.id desc limit 10
    )
     
 c.

MySQL基本指令3 和 索引 、分页的更多相关文章

  1. mysql大数据量下的分页

    mysql大数据量使用limit分页,随着页码的增大,查询效率越低下. 测试实验 1.   直接用limit start, count分页语句, 也是我程序中用的方法: select * from p ...

  2. MySQL 第五篇:索引原理与慢查询优化

    一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...

  3. MySQL Desc指令相关

    MySQL Desc指令相关   2011-08-09 11:25:50|  分类: my基本命令 |举报 |字号 订阅 1.desc tablename; 例如 :mysql> desc jo ...

  4. mysql数据库补充知识7 索引原理与慢查询优化

    一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...

  5. Ubuntu Mysql 常用指令

    mysql 常用指令及中文乱码解决 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  6. MySQL学习(二)索引原理及其背后的数据结构

    首先区分几个概念: 聚集索引 主索引和辅助索引(即二级索引) innodb中每个表都有一个聚簇索引(clustered index ),除此之外的表上的每个非聚簇索引都是二级索引,又叫辅助索引(sec ...

  7. MySQL数据库(七)--索引

    一 .介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语 ...

  8. MySQL学习-MySQL内置功能_索引与慢查询

    1.索引基础 1.1 介绍 (1.)为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂 ...

  9. MySQL如何创建一个好索引?创建索引的5条建议【宇哥带你玩转MySQL 索引篇(三)】

    MySQL如何创建一个好索引?创建索引的5条建议 过滤效率高的放前面 对于一个多列索引,它的存储顺序是先按第一列进行比较,然后是第二列,第三列...这样.查询时,如果第一列能够排除的越多,那么后面列需 ...

随机推荐

  1. zoj3299 Fall the Brick

    Time Limit: 3 Seconds      Memory Limit: 32768 KB Now the God is very angry, so he wants to punish t ...

  2. python to exe

    使用pyinstaller 打包 文件结构如下 命令行cd 进入 project文件夹 ,然后 命令 pyinstaller -F main.py 即可打包文件为一个完整的exe.(不含DLL等)

  3. leetcode 22. 括号生成 dfs

    先思考符合要求的串是什么样子的 任意时刻,(数量大于),且最后(==)==n即可 考虑下一个加入string的字符时(或者)即可 dfs class Solution { public: vector ...

  4. (转载)RTMP协议中的AMF数据 http://blog.csdn.net/yeyumin89/article/details/7932585

    为梦飞翔   (转载)RTMP协议中的AMF数据 http://blog.csdn.net/yeyumin89/article/details/7932585 这里有一个连接,amf0和amf3的库, ...

  5. RT-Thread学习笔记1-启动顺序与线程创建

    目录 1. 启动顺序 2. 堆范围 3. 线程创建 3.1 线程代码(入口函数) 3.2 线程控制块 3.3 线程栈 4. 系统滴答时钟 5. GPIO驱动架构操作IO 6. 线程优先级 & ...

  6. Alexa website ranking

    Alexa website ranking The top 500 sites on the web https://www.alexa.com/topsites https://www.alexa. ...

  7. css skeleton loading & skeleton components

    css skeleton loading css & :empty See the Pen Skeleton Screen with CSS by xgqfrms (@xgqfrms) on ...

  8. js repeatify & no for loop

    js repeatify & no for loop js repeatify https://www.sitepoint.com/5-typical-javascript-interview ...

  9. React Hooks & useCallback & useMemo

    React Hooks & useCallback & useMemo https://reactjs.org/docs/hooks-reference.html#usecallbac ...

  10. svg & stroke & style & class

    svg & stroke & style & class svg selected style methods style class, !important fill, st ...