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;

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
3月前
|
开发框架 前端开发 .NET
七天.NET 8操作SQLite入门到实战 - (1)第七天BootstrapBlazor UI组件库引入
七天.NET 8操作SQLite入门到实战 - (1)第七天BootstrapBlazor UI组件库引入
|
4月前
|
存储 监控 安全
带你读《Apache Doris 案例集》——07查询平均提速700% ,奇安信基于 Apache Doris 升级日志安全分析系统(1)
带你读《Apache Doris 案例集》——07查询平均提速700% ,奇安信基于 Apache Doris 升级日志安全分析系统(1)
|
4月前
|
SQL 存储 安全
带你读《Apache Doris 案例集》——07查询平均提速700% ,奇安信基于 Apache Doris 升级日志安全分析系统(2)
带你读《Apache Doris 案例集》——07查询平均提速700% ,奇安信基于 Apache Doris 升级日志安全分析系统(2)
107 0
|
1月前
|
分布式计算 DataWorks 关系型数据库
DataWorks报错问题之报错“查询运行日志失败"如何解决
DataWorks是阿里云提供的一站式大数据开发与管理平台,支持数据集成、数据开发、数据治理等功能;在本汇总中,我们梳理了DataWorks产品在使用过程中经常遇到的问题及解答,以助用户在数据处理和分析工作中提高效率,降低难度。
|
1月前
|
SQL NoSQL Java
【七】springboot整合AOP实现日志操作
【七】springboot整合AOP实现日志操作
41 0
|
1月前
|
SQL 缓存 关系型数据库
MySQL的万字总结(缓存,索引,Explain,事务,redo日志等)
MySQL的万字总结(缓存,索引,Explain,事务,redo日志等)
66 0
|
2月前
|
Java
SpringAop实现记录用户操作日志
java实现记录用户操作日志功能
|
2月前
|
存储 监控 BI
OSS日志查询
实时日志查询功能将OSS与日志服务SLS相结合,允许您在OSS控制台直接查询OSS的访问日志
29 1
|
2月前
|
存储 Prometheus Cloud Native
Grafana 系列文章(十一):Loki 中的标签如何使日志查询更快更方便
Grafana 系列文章(十一):Loki 中的标签如何使日志查询更快更方便
|
3月前
|
小程序 Linux 数据安全/隐私保护
Linux学习笔记十六:日志管理
Linux学习笔记十六:日志管理

热门文章

最新文章