Indexing mixed-type columns in SQLite
(1) By PChemGuy on 2022-10-29 14:07:18 [source]
I have a column in an SQLite table which contains both numeric and textual values. Is it possible to create partial indices, one for numeric values and one for textual values, so that queries against numeric and textual data would use an appropriate index? Alternatively, is there some "canonical" approach to this problem, that is indexing/querying text/numeric mixed-type columns?
Also, how does indexing of mixed-type columns works from the point of querying? I apologize if I missed explanation of this matter in the official docs, but I could not find anything.
(2) By Chris Locke (chrisjlocke1) on 2022-10-29 19:35:10 in reply to 1 [link] [source]
What have you defined the column as - text?
What is your application putting the data in as? numerically or textually?
If you have a database with a table called 'table1' and add a field as text, and add '11' to it both as numbers and text, you can only pull back one record.
You get the other record if you search for 11.
So, it doesn't matter how they're indexed. The index takes care of the index. Search for 11 and it'll be in the index. Search for '11' and it'll be in the index. My point is, you can't search for 11 and get both records, so the records are independent.
(3) By anonymous on 2022-10-29 20:13:09 in reply to 1 [link] [source]
As long as you only need to distinguish text from numbers, a single index will do:
sqlite> create table foo (fooid integer primary key, fooval any not null unique) strict; sqlite> insert into foo (fooval) values('1.0'); sqlite> insert into foo (fooval) values(1.0); sqlite> insert into foo (fooval) values('1'); sqlite> insert into foo (fooval) values(1); Runtime error: UNIQUE constraint failed: foo.fooval (19) sqlite> select * from foo where fooval='1.0'; +-------+--------+ | fooid | fooval | +-------+--------+ | 1 | '1.0' | +-------+--------+ sqlite> select * from foo where fooval=1.0; +-------+--------+ | fooid | fooval | +-------+--------+ | 2 | 1.0 | +-------+--------+ sqlite>
(4) By PChemGuy on 2022-10-29 20:47:14 in reply to 2 [link] [source]
My understanding is that I should probably define my data column as BLOB or NUMERIC. NUMERIC does type casting as opposed to BLOB, which mean that '11' is stored as integer. However, if I have a text literal containing a sufficiently long sequence of digits, it is converted to REAL, losing some of the information. For the BLOB column such conversion does not take place.
Now, from https://sqlite.org/datatype3.html#comparison_expressions and the following sections I understand that I can have a single index, which will properly work for both numeric and textual queries as if the column only had data type values relevant to the query.
(5) By Richard Hipp (drh) on 2022-10-29 21:39:00 in reply to 1 [link] [source]
I believe you can simply put your content in the database and then query for it and everything will work fine.
CREATE TABLE t1(a INTEGER PRIMARY KEY, data BLOB); CREATE INDEX t1data ON t1(data);
You don't need special indexes. You don't need to do anything special in your queries. It will just work.
Don't overthink this.
(6) By PChemGuy on 2022-10-30 05:12:33 in reply to 5 [link] [source]
Thank you.