SQLite 日志操作和提升查询效率的索引操作 | 学习笔记

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 快速学习 SQLite 日志操作和提升查询效率的索引操作

开发者学堂课程【嵌入式之 RFID 开发与应用2020版:SQLite 日志操作和提升查询效率的索引操作】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/665/detail/11234


SQLite 日志操作和提升查询效率的索引操作

内容介绍:

一、日志

二、查询优化—索引

 

一、日志

主要是函数 date  time

Date 表示日期,time 表示时间

创建日志:

sqlitecreate table log (time text, data text);

举例:在已有表中增加日志

sqlite> alter table info add time text;

sqlite> .schema

CREATE TABLE info(id INT, name text , addr text, time text) ;

CREATE TABLE class(id INT, score INT , year text);

CREATE TRIGGER tg_del after delete on info begin delete from class where id=old.id;end;

CREATE TRIGGER tg_up after update on info begin update class set id=new.id where id=old.id;end;

sqlite> alter table info add date text;

sqlite> .schema

CREATE TABLE info(id INT, name text, addr text, time text, date text);

CREATE TABLE class(id INT, score INT , year text);

CREATE TRIGGER tg_del after delete on info begin delete from class where id=old.id;end;

CREATE TRIGGER tg_up after update on info begin update class set id=new.id where id=old.id;end;

sqlite> select * from info;

id     name    addr     time    date

----   --------  -------  --------  -------

101     zs      bj

102     ls       tj

110     ww     sh

104     zs       cd

105     xw      cq

107

sqlite> insert into info values(108, ' xx', 'ty ',time ( 'now' ) , date ( 'now' ) );

sqlite> select * from info;

id     name    addr     time    date

----   --------  -------  --------  -------

101     zs      bj

102     ls       tj

110     ww     sh

104     zs       cd

105     xw      cq

107

108     xx       ty    08:01:40   2020-04-28

时间是错误的,要加上时区,时间要加上 8 才是对的。

创建日志,设置为修改日志,如果之前表的内容修改,就触发日志内容追加。

sqlite> create table ch_log(time text,date text) ;

sqlite> create trigger tg_ch after update on info begin insert into ch_log values(time ( 'now') ,date( 'now'));end;

sqlite> alter table info add date text;

sqlite> .schema

CREATE TABLE info(id INT, name text, addr text, time text, date text);

CREATE TABLE class(id INT, score INT , year text);

CREATE TRIGGER tg_del after delete on info begin delete from class where id=old.id;end;

CREATE TRIGGER tg_up after update on info begin update class set id=new.id where id=old.id;end;

CREATE TABLE ch_log(time text,date text);

CREATE TRIGGER tg_ch after update on info begin insert into ch_log values(time('now') , date('now')) ;end;

sqlite> select * from info;

id     name    addr     time    date

----   --------  -------  --------  -------

101     zs      bj

102     ls       tj

110     ww     sh

104     zs       cd

105     sw      cq

107

108     xx       ty    08:01:40   2020-04-28

sqlite> select * from class;

id     score    year

----   --------  -------

101     40

102     40

110     60

104     60

105     90

107

现在日志表中为空;有两个触发器,首先触发第一个:

sqlite> update info set id=119 where id=101;

sqlite> select * from info;

id     name    addr     time    date

----   --------  -------  --------  -------

119     zs      bj

102     ls       tj

110     ww     sh

104     zs       cd

105     sw      cq

107

108     xx       ty    08:01:40   2020-04-28

sqlite> select * from class;

id     score    year

----   --------  -------

119     40

102     40

110     60

104     60

105     90

107

sqlite> select * from ch_log;

time          date

--------       --------

08:06:56     2d20-04-28

可以在日志中增加一列,这一列是具体操作,比如 update 操作、delete 操作。

更改 info 表中某个人的名字:

sqlite> update info set name='yy ' where id=119;

sqlite> select * from info;

id     name    addr     time    date

----   --------  -------  --------  -------

119     yy      bj

102     ls       tj

110     ww     sh

104     zs       cd

105     sw      cq

107

108     xx       ty    08:01:40   2020-04-28

/class 表没有变化

sqlite> select * from class;

id     score    year

----   --------  -------

119     40

102     40

110     60

104     60

105    

sqlite> select * from ch_log;

time          date

--------       --------

08:06:56     2020-04-28

08:08:02     2020-04-28

说明:创建多个触发器,满足条件就会自动触发,不满足条件就不会触发。

如果不会用物联网中的实时数据库传递数据,可以使用日志的方式记录上传的信息,然后根据时间轴绘制曲线,包括柱状图,并对时间轴进行分析。

 

二、查询优化—索引

数据库中往往存储了大量的数据,普通查询的默认方法是调用顺序扫描。例如这样一个查询: select * fromtablel where id=10000; 如果没有索引必须遍历整个表,直到 ID 等于 10000 的这一行被找到为止。

为了提高查询的效率,可以使用索引。

1.什么是索引?

索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息。

索引就是恰当的排序,经过某种算法优化,使查找次数要少的多的多

比如二分查找法,二分查找法的前提就是数据是有序的,如果无序就无法二分。

2.缺点:

索引数据可能要占用大量的存储空间,因此并非所有数据都适合索引索引改善检索操作的性能,但降低了数据插入、修改和删除的性能

是否要创建索引取决于数据库是否大部分时间处于查询状态。

3.创建索引:

语法: create index 索引名 on 表名(列名);

查看索引:. indices

删除索引:  drop index 索引名;

索引创建注意:

①在作为主键的列上,因为主键一般是不重复的。

②在经常需要排序的列上创建索引

③在经常使用在 WHERE 子句中的列上面创建索引,加快条件的判断速度

举例:对已有表 info 创建索引

sqlite> create index id_index on info(id);

sqlite> .indices

id_index                 sqlite_autoindex _ntbl_2

sqlite_autoindex_ntbl_1   sqlite_autoindex_tbl_1

也可以利用 .schema 查看索引

sqlite> .shcema

CREATE TABLE info(id INT , name text, addr text) ;

CREATE TABLE class(id INT, score INT , year text) ;

CREATE TRIGGER tg_del after .delete on info begin delete from class where id=old.id;end;

CREATE TRIGGER tg_up after update on info begin update class set id=new.id where id=old.id;end;

CREATE INDEX id_index on info(id);

如果要创建倒序索引,可以加上 desc, create index id_index on info(id desc);

还可以创建多个索引,创建多个索引要加上逗号。

/索引创建完成之后进行查询

sqlite> select*from info where id=105;

id     name    addr     time    date

----   --------  -------  --------  -------

105     xw      cq

/删除索引

sqlite>drop index id_index;

sqlite> .shcema

CREATE TABLE info(id INT , name text, addr text) ;

CREATE TABLE class(id INT, score INT , year text) ;

CREATE TRIGGER tg_del after .delete on info begin delete from class where id=old.id;end;

CREATE TRIGGER tg_up after update on info begin update class set id=new.id where id=old.id;end;

总结:

索引是为了在大数据库中,实现搜索、查找、条件查找等

索引避免使用情况:

①表的数据量不大

②表的大部分操作不是查询

③大量出现 NULL 值的情况

删除操作:

删除表: drop table tab;

删除触发器: drop trigger tg;

删除索引: drop index idx;

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
2月前
|
存储 监控 Serverless
阿里泛日志设计与实践问题之Grafana Loki在日志查询方案中存在哪些设计限制,如何解决
阿里泛日志设计与实践问题之Grafana Loki在日志查询方案中存在哪些设计限制,如何解决
|
2月前
|
SQL 存储 JSON
更快更强,SLS 推出高性能 SPL 日志查询模式
从海量的日志数据中,按照各种灵活的条件进行即时查询搜索,是可观测场景下的基本需求。本文介绍了 SLS 新推出的高性能 SPL 日志查询模式,支持 Unix 风格级联管道式语法,以及各种丰富的 SQL 处理函数。同时通过计算下推、向量化计算等优化,使得 SPL 查询可以在数秒内处理亿级数据,并支持 SPL 过滤结果分布图、随机翻页等特性。
11588 99
|
2月前
|
存储 消息中间件 人工智能
AI大模型独角兽 MiniMax 基于阿里云数据库 SelectDB 版内核 Apache Doris 升级日志系统,PB 数据秒级查询响应
早期 MiniMax 基于 Grafana Loki 构建了日志系统,在资源消耗、写入性能及系统稳定性上都面临巨大的挑战。为此 MiniMax 开始寻找全新的日志系统方案,并基于阿里云数据库 SelectDB 版内核 Apache Doris 升级了日志系统,新系统已接入 MiniMax 内部所有业务线日志数据,数据规模为 PB 级, 整体可用性达到 99.9% 以上,10 亿级日志数据的检索速度可实现秒级响应。
AI大模型独角兽 MiniMax 基于阿里云数据库 SelectDB 版内核 Apache Doris 升级日志系统,PB 数据秒级查询响应
|
2月前
|
jenkins 持续交付
jenkins学习笔记之三:使用jenkins共享库实现日志格式化输出
jenkins学习笔记之三:使用jenkins共享库实现日志格式化输出
jenkins学习笔记之三:使用jenkins共享库实现日志格式化输出
|
29天前
|
SQL 存储 缓存
高基数 GroupBy 在 SLS SQL 中的查询加速
本文详细介绍了SLS中的高基数GroupBy查询加速技术。
|
2月前
|
SQL JavaScript 前端开发
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
|
3月前
|
Java Serverless 应用服务中间件
函数计算操作报错合集之JVM启动时找不到指定的日志目录,该如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
|
3月前
|
监控 数据管理 关系型数据库
数据管理DMS使用问题之是否支持将操作日志导出至阿里云日志服务(SLS)
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。
|
2月前
|
存储 运维 Kubernetes
在k8S中,日志索引的作用是什么?
在k8S中,日志索引的作用是什么?
|
3月前
|
SQL 关系型数据库 MySQL
Mysql 开启慢日志查询及查看慢日志 sql
Mysql 开启慢日志查询及查看慢日志 sql
34 0
下一篇
无影云桌面