0

i want to store a great amount of strings into my sqlite database. I want them to be always in the same order when i read them as i add them to the database. I know i could give them an autoincrementing primary key and sort by that but since there can be up to 100.000 strings this is a performance issue. Besides the order should NEVER change or be sorted in any different way.

short example:

sql insert "hghtzdz12g" sql insert "jut65bdt" sql insert "lkk7676nbgt"

sql select * should give ALWAYS this order {"hghtzdz12g", "jut65bdt", "lkk7676nbgt" }

Any ideas how to achive this ?

Thanks

2 Answers2

2

In a query like

SELECT * FROM MyTable ORDER BY MyColumn

the database does not need to sort the results if the column is indexed, because it can just scan through the index entries in order.

The rowid (or whatever you call the autoincrementing column) is an index, and is even more efficient than a separate index.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • If i understand you right this "CREATE TABLE IF NOT EXISTS strings (name TEXT)" should be enough ? I am just starting to learn about database handling and i am no shure if i understand the rowid thing correct. Does any table have a rowid or do i have to specify that when i create the table? – Tristan Heitzinger Jun 13 '14 at 07:53
  • You'd have to say [WITHOUT ROWID](http://www.sqlite.org/withoutrowid.html) to omit the rowid. – CL. Jun 13 '14 at 08:09
0

If you are sure you will never need anything but exactly this array in exactly this order, you can cheat the database and put in a single blob field.

But then you should ask yourself why you chose a database in the first place.

The correct database solution is indeed a table using a key that you can sort by.

If this performance is not enough, you can have a look here for performance hints.

If you need ultra-fast performance, maybe a database is not the best tool for the job. Databases are used for their ACID abilities and speed is not one of them but rather a secondary objective of everything in software.

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142