Python database programming index


Experimental requirements

Purpose: To understand the role of index in the database, to understand the application of index

  1. Python SQL statement test
    Please create two identical student tables.
    Student table 1, contains primary key id (auto-increment, student id), student name nane, student score,
    student table 2, contains primary key id (auto-increment, student id), Student name nane, student score
    , please add index to the score of student table 2

The difference between adding an index and not adding an index in relational data:
creating an index for a field will generate a tree structure about the field.
That is, when using where conditions are: for example: where score> 90

  • Without index: Search in order------>O(n)
  • Add index: double search------>O(log2n)
  1. Python’s ORM method to perform the test
    Please use ORM method, insert the same data into the two student tables-100,000, the name is generated using a random combination of Chinese characters, and the score is randomly generated from 0-100 evenly distributed.

For students who perform queries on two tables with a score greater than 95 points at the same time, test whether creating an index on the score will bring the advantage of search time efficiency


One, SQL create student table

1.1 creat table

Code display:

#Python 数据库
import pymysql

db = pymysql.connect(user='test',host='localhost', password='123456',database='testdb',charset='utf8')
cursor = db.cursor() #创建游标  游标的每个动作会执行到数据库里面

sql = """create table student1(
        id int auto_increment primary key,
        name varchar(20) not null,
        score int) """
cursor.execute(sql)
sql = """create table student2(
        id int auto_increment primary key,
        name varchar(20) not null,
        score int) """
cursor.execute(sql)
#添加索引
sql = "create index score_index on student2(score)"
#删除索引
#sql = "creat index score_index on student2()"
cursor.execute(sql)
cursor.close()
db.close()

Result display:
Insert picture description here

1.2 show index

SHOW INDEX FROM student1 //No index

Insert picture description here

SHOW INDEX FROM student2 //Add index

Insert picture description here

Two, ORM way to create data

2.1 Build data

The key code is as follows (example):

#创建类
class Student1(Base):
    __tablename__ ='student1'#表名-->数据库中表student1

    id = Column(Integer, primary_key=True)
    name = Column(String)
    score = Column(Integer,default=60)#默认60

class Student2(Base):
    __tablename__ ='student2'#表名-->数据库中表student2

    id = Column(Integer, primary_key=True)
    name = Column(String)
.....
.....
.....
for i in range(100):
    #随机生成 两个字的姓名   分数 
    name  = ''.join(random.sample('张王天男别奥法妹大那几年安东尼的技能但是否能单反',2))
    score = random.randint(0,100)
    session.add(Student1(name=name, score=score))
    session.add(Student2(name=name, score=score)) 
    #每调用一次,塞入数据库执行一次    
    session.commit()
    '''
    由于插入数据需要消耗时间 当插入大量数据可作如下改动
    if i%10 == 0
    	session.commit()
    '''
    print(i)

2.2 Query data

The code is as follows (example):

#SqlAlchemy查询建立也需要时间
students = session.query(Student1).filter(Student1.score<50).all()
students = session.query(Student1).filter(Student2.score<50).all()
#SqlAlchemy真实测试的查询
#100条数据,不见索引查询的快
#1000条数据,建立索引查询的快
print(time.clock())
students = session.query(Student1).filter(Student1.score>90).all()
print(time.clock())
students = session.query(Student2).filter(Student2.score>90).all()
print(time.clock())
  • Summary: Index query is suitable for large amounts of data

to sum up

Insert picture description here

Guess you like

Origin blog.csdn.net/HG0724/article/details/112404629