Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Advanced Core Data

Advanced Core Data

Sing Jie Lee

November 20, 2015
Tweet

More Decks by Sing Jie Lee

Other Decks in Technology

Transcript

  1. COMMAND LINE BROWSER $ sqlite3 /Users/singjie/present/ main-25142226 SQLite version 3.8.5

    2014-08-15 22:37:57 Enter ".help" for usage hints. sqlite>
  2. sqlite> explain query plan select * from ZBTMessage where ZMSGID

    = 2; 0|0|0|SEARCH TABLE ZBTMessage USING INDEX ZBTMESSAGE_ZMSGID_INDEX (ZMSGID=?) USING EXPLAIN QUERY PLAN
  3. sqlite> explain query plan select * from ZBTMessage where ZMSGID

    = 2; 0|0|0|SEARCH TABLE ZBTMessage USING INDEX ZBTMESSAGE_ZMSGID_INDEX (ZMSGID=?) USING EXPLAIN QUERY PLAN
  4. sqlite> explain query plan select * from ZBTInvite where ZINVITEID

    = 2; 0|0|0|SCAN TABLE ZBTInvite USING EXPLAIN QUERY PLAN
  5. SUBCLASSING - Z_ENT ZBTBuzzClub - 50k rows (Z_ENT == 16)

    ZBTBuzzUser - 50k rows (Z_ENT == 15) ZBTBuzz - ~100k rows Indexes: ‣ Z_ENT ‣ Z_BUZZ_ID sqlite will always use Z_ENT index SELECT * from ZBTBuzz where Z_ENT = 16 AND Z_BUZZ_ID = 123;
  6. SUBCLASSING - Z_ENT Indexes: ‣ Z_ENT ‣ Z_BUZZ_ID sqlite> explain

    query plan select * from ZBTBuzz where Z_ENT = 16 AND Z_BUZZ_ID = 123; 0|0|0|SEARCH TABLE ZBTBuzz USING INDEX ZBTBuzz_Z_ENT_INDEX (Z_ENT=?) SELECT * from ZBTBuzz where Z_ENT = 16 AND Z_BUZZ_ID = 123;
  7. SUBCLASSING - Z_ENT INDEX ZBTBuzzClub - 50k rows (Z_ENT ==

    16) ZBTBuzzUser - 50k rows (Z_ENT == 15) ZBTBuzz - ~100k rows ZBTBuzzClub - 50k rows (Z_ENT == 16) ZBTBuzzClub - 1 row (Z_BUZZ_ID = 123) 1 2 3 SELECT * from ZBTBuzz where Z_ENT = 16 AND Z_BUZZ_ID = 123;
  8. SUBCLASSING - Z_BUZZ_ID INDEX ZBTBuzzClub - 50k rows (Z_ENT ==

    16) ZBTBuzzUser - 50k rows (Z_ENT == 15) ZBTBuzz - ~100k rows ZBTBuzzClub - 2 rows (Z_BUZZ_ID == 123) ZBTBuzzClub - 1 row (Z_ENT == 16) 1 2 3 SELECT * from ZBTBuzz where Z_ENT = 16 AND Z_BUZZ_ID = 123;
  9. SUBCLASSING - Z_ENT, Z_BUZZ_ID COMPOUND INDEX ZBTBuzzClub - 50k rows

    (Z_ENT == 16) ZBTBuzzUser - 50k rows (Z_ENT == 15) ZBTBuzz - ~100k rows ZBTBuzzClub - 1 row (Z_ENT == 16 && Z_BUZZ_ID == 123) 1 2 SELECT * from ZBTBuzz where Z_ENT = 16 AND Z_BUZZ_ID = 123;
  10. SUBCLASSING - Z_ENT, Z_BUZZ_ID COMPOUND INDEX ZBTBuzzClub - 50k rows

    (Z_ENT == 16) ZBTBuzzUser - 50k rows (Z_ENT == 15) ZBTBuzz - ~100k rows ZBTBuzzClub - 1 row (Z_ENT == 16 && Z_BUZZ_ID == 123) 1 2 SELECT * from ZBTBuzz where Z_ENT = 16 AND Z_BUZZ_ID = 123;
  11. SUBCLASSING ZBTBuzzClub - 50k rows (Z_ENT == 16) ZBTBuzzUser -

    50k rows (Z_ENT == 15) ZBTBuzz - ~100k rows ZBTBuzzClub - 2 rows (Z_BUZZ_ID == 123) 1 2 for (id result in results) {
 if ([result isKindOfClass:[BTBuzzClub class]]) {
 …
 }
 } SELECT * from ZBTBuzz where Z_BUZZ_ID = 123;
  12. MANAGE TYPES YOURSELF ZBTBuzz(Club) - 50k rows (Z_TYPE == BUZZCLUB)

    ZBTBuzz(User) - 50k rows (Z_TYPE == BUZZUSER) ZBTBuzz - ~100k rows select * from ZBTBuzz where Z_TYPE = BUZZCLUB AND Z_BUZZ_ID = 123; Compound: ‣ Z_TYPE, Z_BUZZ_ID
  13. PROFILING 2015-11-20 10:09:48.593 BeeTalk[3579:1652444] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT,

    t0.ZDATA, t0.ZDISPLAYINDEX, t0.ZISOUTGOING, t0.ZISWHISPER, t0.ZMEDIASTATUS, t0.ZMSGID, t0.ZREADCOUNT, t0.ZSTATUS, t0.ZTIMESTAMP, t0.ZTYPE, t0.ZWHISPERDURATION, t0.ZWHISPERSTARTTIME, t0.ZBUDDYINFO, t0.ZCHAT, t0.Z5_CHAT, t0.ZINVCHAT, t0.Z5_INVCHAT, t0.ZLASTREADCHAT, t0.Z5_LASTREADCHAT, t0.ZUSER FROM ZBTMESSAGE t0 WHERE (( t0.ZMEDIASTATUS = ? OR t0.ZMEDIASTATUS = ?) AND t0.ZTYPE = ?) 2015-11-20 10:09:48.594 BeeTalk[3579:1652444] CoreData: annotation: sql connection fetch time: 0.0002s
  14. PROFILING + (void)load { #if defined DEBUG || defined INTRELEASE

    Method original, swizzled; original = class_getInstanceMethod(self, @selector(executeFetchRequest:error:)); swizzled = class_getInstanceMethod(self, @selector(executeFetchRequest_Swizzled:error:)); method_exchangeImplementations(original, swizzled); #endif } - (NSArray *)executeFetchRequest_Swizzled:(NSFetchRequest *)request error:(NSError *__autoreleasing *)error { NSString *logFormat = [NSString stringWithFormat:@"[%@]: %@", request.entityName, request.predicate.tracerPredicate]; CFAbsoluteTime start = CFAbsoluteTimeGetCurrent(); NSArray *results = [self executeFetchRequest_Swizzled:request error:error]; CFAbsoluteTime end = CFAbsoluteTimeGetCurrent() - start; // 100ms if (end - start > 0.1) { NSLog(@"%@", logFormat); NSParameterAssert(NO); } return results; }
  15. Q&A