Answer the question
In order to leave comments, you need to log in
SQLite doesn't use index by date
There is a simple sign:
CREATE TABLE "cashes" (
"id" INTEGER PRIMARY KEY NOT NULL,
"date" DATE NOT NULL,
"uid" INT(10) NOT NULL DEFAULT ('1'),
"visible" TINYINT(4) NOT NULL DEFAULT ('1')
)
CREATE INDEX "XIF_CASHES_USR" on cashes (uid ASC);
CREATE INDEX "XIF_CASHES_DUV" on cashes (date DESC, uid ASC, visible ASC);
EXPLAIN QUERY PLAN SELECT
c.id, c.uid, c.date
FROM cashes c
WHERE
c.date = '2013-04-01'
AND c.uid = 1 AND c.visible = 1
ORDER BY
c.date
SEARCH TABLE cashes AS c USING INDEX XIF_CASHES_DUV (date=?) (~2 rows)
EXPLAIN QUERY PLAN SELECT
c.id, c.uid, c.date
FROM cashes c
WHERE
c.date BETWEEN '2013-04-01' AND '2013-06-01'
AND c.uid = 1 AND c.visible = 1
ORDER BY
c.date
SEARCH TABLE cashes AS c USING INDEX XIF_CASHES_USR (uid=?) (~2 rows)
Answer the question
In order to leave comments, you need to log in
Try
WHERE c.`date` > '2013-04-01' AND c.`date` < '2013-06-01'
It selects that index because there is a where uid=1
, and this operation is supposedly faster than two comparisons. The standard approach here is analyze .
That is, cram data, execute analyze caches
, execute a query - the plan will be different depending on whether there are more different dates or different uids in the data (only this should be checked not on 5 records, but more).
More/less do not change the plan.
And here is a hint, yes, it helps.
But I wish I didn't have it...
EXPLAIN QUERY PLAN SELECT
c.id, c.uid, c.date
FROM cashes c INDEXED BY XIF_CASHES_DUV
WHERE
c.date >= '2013-04-01' AND c.date <= '2013-06-01'
AND c.uid = 1 AND c.visible = 1
ORDER BY
c.date
SEARCH TABLE cashes AS c USING INDEX XIF_CASHES_DUV (date>? AND date<?) (~600 rows)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question