air sqlite for the real world

33
AIR SQLite for the Real World Paul Robertson Sr. User Experience Developer Dedo Interactive, Inc. D-Flex May 19, 2011

Upload: hpaulrobertson

Post on 04-Mar-2015

161 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AIR SQLite for the Real World

AIR SQLite for the Real World

Paul RobertsonSr. User Experience DeveloperDedo Interactive, Inc.

D-FlexMay 19, 2011

Page 2: AIR SQLite for the Real World

Hello.

Who is this guy, anyway?!

Page 3: AIR SQLite for the Real World

Hello.

So who do you think you are?

Page 4: AIR SQLite for the Real World

Background and concepts

! Synchronous/asynchronous

! Transaction

! Index

! “optimization”

Page 5: AIR SQLite for the Real World

Performance

(application performance)

0

13

25

38

50

1st Qtr 2nd Qtr 3rd Qtr

Page 6: AIR SQLite for the Real World

Application Performance

Actual performance

vs.

Perceived performance

(is there really a di!erence?)

Page 7: AIR SQLite for the Real World

Application Performance

Actual performance

Page 8: AIR SQLite for the Real World

Application Performance > Actual performance

Write faster SQL! Favor JOIN over subquery

! Minimize or avoid statements that can’t use indexes:

! aggregate functions in subquery

! UNION in subquery

! ORDER BY in UNION

! Avoid LIKE – especially LIKE (‘%blah%’)

! Avoid IN – use AND/OR

! Avoid JOINing to the same table multiple times (careful with views)

! Avoid needless lookups

! Qualify table (and other object) names with database name (usually “main”)

! Explicitly specify column names in SELECT and INSERT

Page 9: AIR SQLite for the Real World

Application Performance > Actual performance

Use (and re-use) prepared statements

Page 10: AIR SQLite for the Real World

Application Performance > Actual performance

Use (and re-use) prepared statements (bad example)

Page 11: AIR SQLite for the Real World

Application Performance > Actual performance

Use (and re-use) prepared statements (good example)

Page 12: AIR SQLite for the Real World

Application Performance > Actual performance

Use indexes

! …but use them wisely…! Index columns that are used in joining tables or in WHERE or ORDER BY clauses

! Used together – index together

! Specify COLLATE NOCASE for columns that will be sorted alphabetically

! Indexes boost data retrieval (SELECT) performance at the cost of data change (INSERT, UPDATE) performance

! …and/or use analyze()

Page 13: AIR SQLite for the Real World

Application Performance > Actual performance

Create table structure before adding data

Page 14: AIR SQLite for the Real World

Application Performance > Actual performance

Create table structure before adding data! On-disk, database "le uses “pages”

Database "le

sqlite_master table1 table2

Page 15: AIR SQLite for the Real World

Application Performance > Actual performance

Create table structure before adding data! On-disk, database "le uses “pages”

sqlite_master table1 table2 table1

Database "le

Page 16: AIR SQLite for the Real World

Application Performance > Actual performance

Create table structure before adding data! On-disk, database "le uses “pages”

sqlite_master table1 table2 table1

Database "le

sqlite_master

Page 17: AIR SQLite for the Real World

Application Performance > Actual performance

Create table structure before adding data! On-disk, database "le uses “pages”

…or use SQLConnection.compact()

! but be careful with autoCompact!

sqlite_master sqlite_master table1 table2table1

Database "le

Page 18: AIR SQLite for the Real World

Application Performance > Actual performance

Use transactions for batch INSERT/UPDATE/DELETE operations

Page 19: AIR SQLite for the Real World

Application Performance > Actual performance

Use transactions for batch INSERT/UPDATE/DELETE operations

! Example: Inserting 85489 rows of data from a CSV "le! No explicit transaction: 751510 ms (12.5 minutes)

! Explicit transaction: 15662 ms (16 seconds – about 48x faster!)

Page 20: AIR SQLite for the Real World

Application Performance > Actual performance

Use transactions for batch INSERT/UPDATE/DELETE operations (bad example)

Load data

“Loop”

Finish

Page 21: AIR SQLite for the Real World

Application Performance > Actual performance

Use transactions for batch INSERT/UPDATE/DELETE operations (good example)

Load data

Begin transaction

“Loop”

End transaction

Finish

Page 22: AIR SQLite for the Real World

Application Performance

Perceived performance

Page 23: AIR SQLite for the Real World

Application Performance > Perceived performance

Bottleneck #1:

! “Batch” statements! Running multiple statements in sequence, such as a bulk INSERT

! Solution:

! Use asynchronous execution mode

Page 24: AIR SQLite for the Real World

Application Performance > Perceived performance

Bottleneck #2:

! Large SELECT result set

! Solution:! Break it into chunks using execute() with prefetch parameter, and next()

Page 25: AIR SQLite for the Real World

Application Performance > Perceived performance

Bottleneck #3:

! Fast query in the queue behind slow query

! Solution:

! Use multiple SQLConnection objects

Page 26: AIR SQLite for the Real World

Productivity

(developer productivity)

0

13

25

38

50

1st Qtr 2nd Qtr 3rd Qtr

Page 27: AIR SQLite for the Real World

Developer Productivity

Tools

Page 28: AIR SQLite for the Real World

Developer Productivity

Code libraries

Page 29: AIR SQLite for the Real World

Developer Productivity > Application architecture

Goals:

! Minimize duplicate/boilerplate code

! Reduce number of event handlers needed for asynchronous execution! Data binding

! Abstraction layer

! Easily reuse SQLStatement objects

! Easily “chain” dependent database operations

! Queue up multiple instances of the same statement

! Execute multiple independent statements “simultaneously”

Page 30: AIR SQLite for the Real World

Developer Productivity > Application architecture

Solution #1: SQLite MXML wrapper classes (Peter Elst)

http://www.peterelst.com/blog/2008/04/07/introduction-to-sqlite-in-adobe-air/

Page 31: AIR SQLite for the Real World

Developer Productivity > Application architecture

Solution #2: FlexORM

http://www.adobe.com/newsletters/edge/october2009/articles/article7/

http://www.adobe.com/newsletters/edge/december2009/articles/article7/

Page 32: AIR SQLite for the Real World

Developer Productivity > Application architecture

Solution #3: SQLRunner

http://probertson.com/projects/air-sqlite/

Page 33: AIR SQLite for the Real World

Thanks!

Thanks for listening and sharing!

H. Paul Robertson

http://probertson.com/

@probertson