Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sqlite> .schema
- CREATE TABLE stories (
- _id INTEGER PRIMARY KEY AUTOINCREMENT,
- storyId TEXT UNIQUE,
- title TEXT,
- link TEXT UNIQUE,
- published TEXT,
- updated TEXT,
- image TEXT,
- summary TEXT,
- month TEXT
- );
- CREATE TABLE storyTags (
- _id INTEGER PRIMARY KEY AUTOINCREMENT,
- _storyId INTEGER,
- _tagId INTEGER,
- relevance REAL,
- FOREIGN KEY(_storyId) REFERENCES stories(_id),
- FOREIGN KEY(_tagId) REFERENCES tags(_id)
- );
- CREATE INDEX stories1 ON stories ( published ASC, _id );
- CREATE INDEX storyTags1 ON storyTags ( _tagId ASC, _storyId ASC );
- CREATE INDEX storyTags2 ON storyTags ( _storyId ASC, _tagId ASC );
- Get all stories (without duplicates) that have one or more of the
- following tagIds: 870, 2012, 21395, ordered by publish date,
- oldest story first:
- SELECT _id, storyId, title, link, published, updated, image, summary, month
- FROM stories
- WHERE _id IN (SELECT _storyId FROM storyTags WHERE _tagId IN (870, 2012, 21395)
- ORDER BY published;
- is faster than joining stories and storyTags. EXPLAIN QUERY PLAN
- uses indexes for both selects, but in the joined version only
- on the storyTags table - it's actually doing a scan on the stories table.
Add Comment
Please, Sign In to add comment