简体   繁体   中英

sqlite3 index table in python

I have created this table in python 2.7 . I use it to store unique pairs name and value. In some queries I search for names and in others I search for values. Lets say that SELECT queries are 50-50. Is there any way to create a table that will be double index (one index on names and another for values) so my program will seek faster the data ?

Here is the database and table creation:

import sqlite3
#-------------------------db creation ---------------------------------------#
db1 = sqlite3.connect('/my_db.db')
cursor = db1.cursor()

cursor.execute("DROP TABLE IF EXISTS my_table")

sql = '''CREATE TABLE my_table (
        name TEXT DEFAULT NULL,
        value INT
        );'''
cursor.execute(sql)

sql = ("CREATE INDEX index_my_table ON my_table (name);")
cursor.execute(sql)

Or is there any other faster struct for faster value seek ?

You can create another index...

sql = ("CREATE INDEX index_my_table2 ON my_table (value);")
cursor.execute(sql)

I think the best way for faster research is to create a index on the 2 fields. like: sql = ("CREATE INDEX index_my_table ON my_table (Field1, field2)")

Multi-Column Indices or Covering Indices. see the (great) doc here: https://www.sqlite.org/queryplanner.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM