SQLite安装部署、增、删、改、查与memory模式的性能测试

SQLite安装部署、增、删、改、查与memory模式的性能测试

一、SQLite安装

1. 下载sqlite的安装包

链接:https://pan.baidu.com/s/1KBiOUTBx8sAY165RIFB5BQ 
提取码:7qt6 

2. 编译安装

mkdir -p /opt/soft
#把安装下载到/opt/soft
tar zxvf /opt/soft/sqlite-autoconf-3330000.tar.gz -C /opt/soft/
mv /opt/soft/sqlite-autoconf-3330000 /opt/soft/sqlite
cd /opt/soft/sqlite
./configure --prefix=/usr/local
make && make install

二、SQLite基本操作

1. 创建库

mkdir -p /opt/sqlite
cd /opt/sqlite
sqlite3 test.db

2. 创建表

sqlite3 test.db
sqlite> create table test(pno INTEGER PRIMARY KEY  AUTOINCREMENT ,num varchar(30) NOT NULL ,age INTEGER)

3. 插入数据

sqlite3 test.db
sqlite> insert into test(num,age) values(1,1);

4. 清空表

sqlite3 test.db
sqlite> delete from test;  //清空数据
sqlite> update sqlite_sequence SET seq = 0 where name ='test';//自增长ID为0

4. 删除表

sqlite3 test.db
sqlite> drop table test;

5. 改数据

sqlite3 test.db
sqlite> update test set age=8 where pno=8;

6. 查

sqlite3 test.db
sqlite> select * from test;

7. 查看表结构

sqlite3 test.db
sqlite> .schema test

8. 已sql语句的形式列出表内容

sqlite3 test.db
sqlite> .dump test

三 、性能测试

1. 磁盘模式插入和读取100万条数据所用时间

#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import sqlite3

con = sqlite3.connect(":memory:")
# 获取cursor对象
cur = con.cursor()
# 执行sql创建表
cur.execute("create table t_person(pno INTEGER PRIMARY KEY  AUTOINCREMENT ,pname varchar(30) NOT NULL ,age INTEGER)")
sql = 'insert into t_person(pname,age) values(1,1)'
start = time.time()
try:
    for i in range(1, 1000001):
	    cur.execute(sql)
    print('插入成功')
    
    #打印插入数据所用时间
    inster = time.time()
    charu = inster - start
    print("插入100万条数据总耗时为:%s" % charu)
    
    cur.execute("select * from t_person")
    print('读取成功')
     
	#打印读取100万条数据
    select = time.time()
    duqu = select - inster
    print("查询100万条数据总耗时为:%s" % duqu)
    
    #提交事务
    con.commit()
    
    print('结束')
    end = time.time()
    alltime = end - start
    print("插入读取100万条数据总耗时为:%s" % alltime)
except Exception as e:
    print('插入失败')
    con.rollback()
finally:
    # 关闭游标
     con.close()
    # 关闭连接
     con.close()

2. 内存模式插入和读取100万条数据所用时间

#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import sqlite3

con = sqlite3.connect("normal.db")
# 获取cursor对象
cur = con.cursor()
# 执行sql创建表
cur.execute("create table t_person(pno INTEGER PRIMARY KEY  AUTOINCREMENT ,pname varchar(30) NOT NULL ,age INTEGER)")
sql = 'insert into t_person(pname,age) values(1,1)'
start = time.time()
try:
    for i in range(1, 1000001):
	    cur.execute(sql)
    print('插入成功')
    
    #打印插入数据所用时间
    inster = time.time()
    charu = inster - start
    print("插入100万条数据总耗时为:%s" % charu)
    
    cur.execute("select * from t_person")
    print('读取成功')
     
	#打印读取100万条数据
    select = time.time()
    duqu = select - inster
    print("查询100万条数据总耗时为:%s" % duqu)
    
    #提交事务
    con.commit()
    
    print('结束')
    end = time.time()
    alltime = end - start
    print("插入读取100万条数据总耗时为:%s" % alltime)
except Exception as e:
    print('插入失败')
    con.rollback()
finally:
    # 关闭游标
     con.close()
    # 关闭连接
     con.close()

你可能感兴趣的:(sqlite3,python)