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条建议 过滤效率高的放前面 对于一个多列索引,它的存储顺序是先按第一列进行比较,然后是第二列,第三列...这样.查询时,如果第一列能够排除的越多,那么后面列需 ...
随机推荐
- poj3260 The Fewest Coins
Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...
- [Angular] 删除旧版本,升级安装最新版本
目录 删除旧版本 清除未卸载干净的angular-cli缓存 对于Linux 对于Windows 安装最新版本 查看安装版本 创建新项目 删除旧版本 npm uninstall -g angular- ...
- k8s二进制部署 - master节点安装
下载kubernetes服务端 [root@hdss7-21 ~]# cd /opt/src [root@hdss7-21 src]# wget https://dl.k8s.io/v1.15.2/k ...
- GO - LLT
GoConvey: https://www.jianshu.com/p/e3b2b1194830 GoMonkey: https://www.jianshu.com/p/2f675d5e334e Go ...
- leetcode 1 两数之和 hashmap
主要是hashmap.还有边插入边查找,提高效率和降低空间复杂度. 之前一直用map,结果发现还有hashmap,效率更高. 注意名称空间为 using namespace __gnu_cxx; 问题 ...
- VS2010下如何查看类的内存布局
用VS2010查看类的内存布局,这里用两种方法 (1)MSVC有个隐藏的"/d1"开关,通过这个开关可以查看项目中类的内存布局情况. 修改项目属性,添加"/d1 repo ...
- codeforces 858A
A. k-rounding time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 开源软件ffmpeg使用中的问题
error while decoding MB 20 10, bytestream -13 经过调试,发现这部是 int ret = avcodec_decode_video2(pCodecConte ...
- HDU 6390 GuGuFishtion(莫比乌斯反演 + 欧拉函数性质 + 积性函数)题解
题意: 给定\(n,m,p\),求 \[\sum_{a=1}^n\sum_{b=1}^m\frac{\varphi(ab)}{\varphi(a)\varphi(b)}\mod p \] 思路: 由欧 ...
- canvas绘制五星红旗
代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...