My Query:
SELECT *
FROM t1, t2
WHERE (t1.ZID=t2.ZCHARACTERID AND t1.ZID IN (1,2,3,4) AND t2.Z_ENT=3)
ORDER BY t2.ZSTROKECOUNT
My Indices:
CREATE INDEX i1 on t1(ZID);
CREATE INDEX i2 on t2(ZCHARACTERID, Z_ENT, ZSTROKECOUNT);
Explain Query Plan
selectid order from detail
---------- ---------- ---------- ----------------------------------------------------------------- --------------
0 0 1 SEARCH TABLE t1 USING INDEX i1 (ZID=?) (~740 rows)
0 0 0 EXECUTE LIST SUBQUERY 1
0 1 0 SEARCH TABLE t2 USING INDEX i2 (ZCHARACTERID=? AND Z_ENT=?) (~9 row)
0 0 0 USE TEMP B-TREE FOR ORDER BY
From line 3 of the explain query plan, it says only two indexed columns are used and temp b-tree is used for order by. But from what I understand, I should be able to use i2 for order by as well.
Did I miss anything? Thanks.
because you forgot FROM
clause
try this way
SELECT * FROM your_table WHERE (t1.ZID=t2.ZCHARACTERID AND t1.ZID IN (1,2,3,4) AND t2.Z_ENT=3) ORDER BY t2.ZSTROKECOUNT
The index is not ordered by ZSTROKECOUNT
.
SQLite will use the index for sorting if you do this:
DROP INDEX i2;
CREATE INDEX i3 ON t2(ZSTROKECOUNT, ZCHARACTERID, Z_ENT);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.