MySQL基本指令3 和 索引 、分页
-创建
create view 视图名称 as SQL
ps:虚拟
-修改
alter view 视图名称 as SQL
-删除
drop view 视图名称
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;
伪代码
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 和 索引 、分页的更多相关文章
- mysql大数据量下的分页
mysql大数据量使用limit分页,随着页码的增大,查询效率越低下. 测试实验 1. 直接用limit start, count分页语句, 也是我程序中用的方法: select * from p ...
- MySQL 第五篇:索引原理与慢查询优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- MySQL Desc指令相关
MySQL Desc指令相关 2011-08-09 11:25:50| 分类: my基本命令 |举报 |字号 订阅 1.desc tablename; 例如 :mysql> desc jo ...
- mysql数据库补充知识7 索引原理与慢查询优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- Ubuntu Mysql 常用指令
mysql 常用指令及中文乱码解决 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- MySQL学习(二)索引原理及其背后的数据结构
首先区分几个概念: 聚集索引 主索引和辅助索引(即二级索引) innodb中每个表都有一个聚簇索引(clustered index ),除此之外的表上的每个非聚簇索引都是二级索引,又叫辅助索引(sec ...
- MySQL数据库(七)--索引
一 .介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语 ...
- MySQL学习-MySQL内置功能_索引与慢查询
1.索引基础 1.1 介绍 (1.)为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂 ...
- MySQL如何创建一个好索引?创建索引的5条建议【宇哥带你玩转MySQL 索引篇(三)】
MySQL如何创建一个好索引?创建索引的5条建议 过滤效率高的放前面 对于一个多列索引,它的存储顺序是先按第一列进行比较,然后是第二列,第三列...这样.查询时,如果第一列能够排除的越多,那么后面列需 ...
随机推荐
- 第三方库:logger,自定义日志封装模块
为了使用方便,二次封装logger. import os import datetime from loguru import logger class Logings: __instance = N ...
- k8s-2-集成apollo配置中心
主题: 在k8s中集成Apollo配置中心 架构图 一.配置中心概述 配置的几种方式 本课讲得是基于配置中心数据库实现 配置管理的现状 常见的配置中心 主讲:k8s configmap,apollo ...
- 实战交付一套dubbo微服务到k8s集群(2)之Jenkins部署
Jenkins官网:https://www.jenkins.io/zh/ Jenkins 2.190.3 镜像地址:docker pull jenkins/jenkins:2.190.3 1.下载Je ...
- 爬虫入门三 scrapy
title: 爬虫入门三 scrapy date: 2020-03-14 14:49:00 categories: python tags: crawler scrapy框架入门 1 scrapy简介 ...
- C++ part2
为什么析构函数必须是虚函数?为什么C++默认的析构函数不是虚函数? references: nowcoder 将可能会被继承的父类的析构函数设置为虚函数,可以保证当我们new一个子类,然后使用基类指针 ...
- cobaltstrike的使用
0x01 介绍 Cobalt Strike是一款渗透测试神器,常被业界人称为CS神器.Cobalt Strike已经不再使用MSF而是作为单独的平台使用,它分为客户端与服务端,服务端是一个,客户端可以 ...
- u-boot 移植 --->6、引导Linux启动测试
在引导Linux开机之前需要先清楚Linux启动的必要或者说是先决条件,这里就是提到了u-boot的作用了引用百度云---主要用于嵌入式系统的引导加载,其实在我调试下来总结一下就是初始化硬件这里的硬件 ...
- v-for & for...in vs for...of
v-for & for...in vs for...of for..in vs for...of for (const key in object) { if (Object.hasOwnPr ...
- HTML5 Template in Action
HTML5 Template in Action https://developer.mozilla.org/es/docs/Web/HTML/Elemento/template https://de ...
- MacBook Pro 关闭触控板
MacBook Pro 关闭触控板 https://support.apple.com/zh-cn/HT204895 https://support.apple.com/zh-cn/HT203171 ...