Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

SQLite performance issue workaround

$
0
0

Everybody who works with Ti.Database already worked with code similar to:

var rs = db.execute(sql);
var values = [], fc=0;
while (rs.isValidRow()) {
    var o = {};
    if (!fc) {
        fc = _.isFunction(rs.fieldCount) ? rs.fieldCount() : rs.fieldCount;
    }
    _.times(fc, function(c) {
        var fn = rs.fieldName(c);
        o[fn] = rs.fieldByName(fn);
    });
    values.push(o);
    rs.next();
}
but you can see that in this case we have to "cross the ridge" four times for each row. On iPhone 4 fetch 1000 rows takes up to 4 seconds. Its terrible.

with TiAdvancedDatabase module you can do the same with following code:

//at top
var TiAdvancedDatabase = require('ti.advanceddatabase'); //replace standard module for easy integration
 
TiAdvancedDatabase.install(dbName);
 
var rs = db.execute(sql);
var values = rs.fetchAllAsJSON();
in this case we have to "cross the bridge" only once and this operation for 1000 records on iPhone 4 takes about 0.7 seconds!!!

Moreover... there were a lot of question about DISTANCE function for SQL statements like SELECT CYTY_NAME, DISTANCE(LON, LAT, 23.444, 49.333) as DIST FROM CITIES and I decided implement this feature too.

You can download module here: https://github.com/darknos/TiAdvancedDatabase

Notice that these features already requested in JIRA and I hope will be implemented in next releases of Titanium SDK but this module is for people who can't wait like me :)

P.S. Android module will be done in a week (I hope)


Viewing all articles
Browse latest Browse all 8068

Trending Articles