I’m working on SQLite3 indices.
Here’s a table COMAPNY:
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Allen', 25, 'Texas', 15000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (5, 'David', 27, 'Texas', 85000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (6, 'Kim', 22, 'South-Hall', 45000.00 );
INSERT INTO COMPANY VALUES (7, 'James', 24, 'Houston', 10000.00 );
=======================================================
SELECT * FROM COMPANY;
Results:
1|Paul|32|California|20000.0
2|Allen|25|Texas|15000.0
3|Teddy|23|Norway|20000.0
4|Mark|25|Rich-Mond |65000.0
5|David|27|Texas|85000.0
6|Kim|22|South-Hall|45000.0
7|James|24|Houston|10000.0
Let’s create an index salary_index,
CREATE INDEX IF NOT EXISTS salary_index on COMPANY (SALARY);
What does it do and how do I use it?
This time I make an index like this after dropping the older one:
CREATE INDEX IF NOT EXISTS salary_index on COMPANY (SALARY)
WHERE SALARY > 50000;
After I added the index, I did:
SELECT * FROM COMPANY;
expecting that I’d see only the ones with salary higher than 50000, but I saw people lower than that.
And I also tried to do this:
SELECT * FROM COMPANY INDEXED BY salary_index;
Then I get Error: no query solution Apparently I have to do: SELECT * FROM COMPANY INDEXED BY salary_index WHERE SALARY > 50000; Where the condition must be the same as in the index.
So… how do I use indices?
salary
will be equally useful in responding to your query as a partial index (and will be able to satisfy queries that your partial index cannot satisfy). – Lap