Optimize SQL statement for android sqlite -


i'm developing application tracks user's current position , stores sqlite database. works fine, have problem when querying database track more 1000 records takes 1.5 minutes. on desktop takes 1 second.

i know it's query many subselects wasn't able right result way. in opinion belongs aggregate functions avg() , sum().

here's query:

cursor c = readabledb     .rawquery(             "select distinct t._id , title , strftime('%y-%m-%d' , starttime , 'unixepoch' , 'localtime') date , description, "             + "round((select sum(disttoprev)/1000 positions p p.trackid=t._id) , 2) distance , "              + "(select count(latitude) positions p p.trackid=t._id) waypoints, "             + "(select (avg(speed)*3.6) positions p p.trackid=t._id) avgspeed, "             + "(select (max(speed)*3.6) positions p p.trackid=t._id) maxspeed, "             + "(select sum(altitudeup) positions p p.trackid=t._id) climb , "             + "(select avg(heartbeat) heartbeats h h.trackid=t._id) avgheartbeat , "             + "(select max(heartbeat) heartbeats h h.trackid=t._id) maxheartbeat , "             + "(select avg(cadence) heartbeats h h.trackid=t._id) avgcadence "             + "from tracks t left join heartbeats h on t._id = h.trackid t._id = ?",             new string[]{string.valueof(trackid)});      c.movetofirst(); 

how can optimize query? tried way, result wrong , took same amount of time.

select t._id , title , strftime('%y-%m-%d' , starttime , 'unixepoch' , 'localtime') date , description, sum(disttoprev)/1000 distance , count(latitude) waypoints,  (avg(speed)*3.6) avgspeed,  (max(speed)*3.6) maxspeed,  sum(altitudeup) climb ,  avg(heartbeat)  avgheartbeat , max(heartbeat)  maxheartbeat , avg(cadence) avgcadence  tracks t  left join heartbeats h on t._id = h.trackid  inner join positions p on t._id = p.trackid       t._id = ? 

since 2 hours i'm looking solution , don't know i'm doing wrong. maybe have take break.

edit:

here create statements:

create table heartbeats(_id integer primary key autoincrement, trackid integer not null, heartbeat integer not null, cadence integer, timestamp timestamp);   create table positions(_id integer primary key autoincrement, trackid integer not null, longitude real not null, latitude real not null, altitude real, altitudeup real, speed real, accuracy real, disttoprev real, timestamp timestamp);   create table tracks(_id integer primary key autoincrement, title text not null, description text, starttime datetime not null, endtime datetime); 

this job:

select tid, date, waypoints, avg(heartbeat) avgheartbeat  (select t._id tid, strftime('%y-%m-%d' , starttime , 'unixepoch' , 'localtime') date, count(latitude) waypoints          tracks t left join positions p on t._id = p.trackid            t._id = ?          group t._id , strftime('%y-%m-%d' , starttime , 'unixepoch' , 'localtime'))     left join heartbeats h on tid = h.trackid group tid, date, waypoints 

add fields required.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -