dimanche 8 mars 2015

How to create and operate indexes in sqlit database

I have a database with the following table: MessageType1, where messages are kept with unixtime and their sender userid.


I am looping through all userid's, and then looping through all messages for each userid. I want the messages sorted by the unixtime in ascending order - so I do the following query:



for index in range(len(userID)):
#Get all MSG with UserID = UserID sorted by UNIXTIME
cur.execute('SELECT unixtime FROM MessageType1 WHERE userID ='+str(userID[index]+'ORDER BY unixtime asc'))


I noticed that the time to perform this query vanished as I removed the ORDER BY, which led me to want to optimize that part. That brings me to the question: How can I implement indexes to optimize it? I have tried CREATE INDEX times ON MessageType1 (unixtime asc)


But am clueless to where I should go from there. Will the same query, without the ORDER BY now return the messages sorted ascending?


Aucun commentaire:

Enregistrer un commentaire