Guest User

Untitled

a guest
Sep 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. sqlite> .schema
  2. CREATE TABLE stories (
  3. _id INTEGER PRIMARY KEY AUTOINCREMENT,
  4. storyId TEXT UNIQUE,
  5. title TEXT,
  6. link TEXT UNIQUE,
  7. published TEXT,
  8. updated TEXT,
  9. image TEXT,
  10. summary TEXT,
  11. month TEXT
  12. );
  13.  
  14. CREATE TABLE storyTags (
  15. _id INTEGER PRIMARY KEY AUTOINCREMENT,
  16. _storyId INTEGER,
  17. _tagId INTEGER,
  18. relevance REAL,
  19. FOREIGN KEY(_storyId) REFERENCES stories(_id),
  20. FOREIGN KEY(_tagId) REFERENCES tags(_id)
  21. );
  22.  
  23. CREATE INDEX stories1 ON stories ( published ASC, _id );
  24.  
  25. CREATE INDEX storyTags1 ON storyTags ( _tagId ASC, _storyId ASC );
  26.  
  27. CREATE INDEX storyTags2 ON storyTags ( _storyId ASC, _tagId ASC );
  28.  
  29.  
  30.  
  31. Get all stories (without duplicates) that have one or more of the
  32. following tagIds: 870, 2012, 21395, ordered by publish date,
  33. oldest story first:
  34.  
  35. SELECT _id, storyId, title, link, published, updated, image, summary, month
  36. FROM stories
  37. WHERE _id IN (SELECT _storyId FROM storyTags WHERE _tagId IN (870, 2012, 21395)
  38. ORDER BY published;
  39.  
  40. is faster than joining stories and storyTags. EXPLAIN QUERY PLAN
  41. uses indexes for both selects, but in the joined version only
  42. on the storyTags table - it's actually doing a scan on the stories table.
Add Comment
Please, Sign In to add comment