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

简介: 快速学习 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;

相关实践学习
通过日志服务实现云资源OSS的安全审计
本实验介绍如何通过日志服务实现云资源OSS的安全审计。
相关文章
|
9月前
|
存储 缓存 Apache
StarRocks+Paimon 落地阿里日志采集:万亿级实时数据秒级查询
本文介绍了阿里集团A+流量分析平台的日志查询优化方案,针对万亿级日志数据的写入与查询挑战,提出基于Flink、Paimon和StarRocks的技术架构。通过Paimon存储日志数据,结合StarRocks高效计算能力,实现秒级查询性能。具体包括分桶表设计、数据缓存优化及文件大小控制等措施,解决高并发、大数据量下的查询效率问题。最终,日志查询耗时从分钟级降至秒级,显著提升业务响应速度,并为未来更低存储成本、更高性能及更多业务场景覆盖奠定基础。
|
9月前
|
自然语言处理 监控 安全
阿里云发布可观测MCP!支持自然语言查询和分析多模态日志
阿里云可观测官方发布了Observable MCP Server,提供了一系列访问阿里云可观测各产品的工具能力,包含阿里云日志服务SLS、阿里云应用实时监控服务ARMS等,支持用户通过自然语言形式查询
1304 0
阿里云发布可观测MCP!支持自然语言查询和分析多模态日志
|
Web App开发 存储 监控
iLogtail 开源两周年:UC 工程师分享日志查询服务建设实践案例
本文为 iLogtail 开源两周年的实践案例分享,讨论了 iLogtail 作为日志采集工具的优势,包括它在性能上超越 Filebeat 的能力,并通过一系列优化解决了在生产环境中替换 Filebeat 和 Logstash 时遇到的挑战。
548 104
|
11月前
|
SQL 存储 自然语言处理
让跨 project 联查更轻松,SLS StoreView 查询和分析实践
让跨 project 联查更轻松,SLS StoreView 查询和分析实践
229 1
|
存储 人工智能 JSON
RAG Logger:专为检索增强生成(RAG)应用设计的开源日志工具,支持查询跟踪、性能监控
RAG Logger 是一款专为检索增强生成(RAG)应用设计的开源日志工具,支持查询跟踪、检索结果记录、LLM 交互记录和性能监控等功能。
533 7
RAG Logger:专为检索增强生成(RAG)应用设计的开源日志工具,支持查询跟踪、性能监控
|
PyTorch 算法框架/工具
Pytorch学习笔记(七):F.softmax()和F.log_softmax函数详解
本文介绍了PyTorch中的F.softmax()和F.log_softmax()函数的语法、参数和使用示例,解释了它们在进行归一化处理时的作用和区别。
1521 1
Pytorch学习笔记(七):F.softmax()和F.log_softmax函数详解
|
监控 网络协议 CDN
阿里云国际监控查询流量、用量查询流量与日志统计流量有差异?
阿里云国际监控查询流量、用量查询流量与日志统计流量有差异?
|
9月前
|
监控 容灾 算法
阿里云 SLS 多云日志接入最佳实践:链路、成本与高可用性优化
本文探讨了如何高效、经济且可靠地将海外应用与基础设施日志统一采集至阿里云日志服务(SLS),解决全球化业务扩展中的关键挑战。重点介绍了高性能日志采集Agent(iLogtail/LoongCollector)在海外场景的应用,推荐使用LoongCollector以获得更优的稳定性和网络容错能力。同时分析了多种网络接入方案,包括公网直连、全球加速优化、阿里云内网及专线/CEN/VPN接入等,并提供了成本优化策略和多目标发送配置指导,帮助企业构建稳定、低成本、高可用的全球日志系统。
980 54