ctu
ctu

Reputation: 318

Android SQLite Performance: Multiple selects in single query Vs single select in multiple queries?

Performance wise, which one is better (for a single table) ?:

  1. Multiple select in single query or
  2. Single select in multiple queries?

Example:

/*table schema*/
create table ACTIVITY_TABLE (
ID integer primary key autoincrement,
INTERVAL_ID integer not null, 
ACTIVITY_TYPE text , 
WIFI_LIST text, 
COUNT integer not null );



/*Multipe selects in single query*/
select * from 
    (select sum(COUNT)/60 as Physical from ACTIVITY_TABLE where  INTERVAL_ID >=1  and  INTERVAL_ID <=3 and (ACTIVITY_TYPE="Still" or ACTIVITY_TYPE = "Walking" or ACTIVITY_TYPE = "Running"  ) ),
    (select sum(COUNT)/60 as vehicular from ACTIVITY_TABLE where  INTERVAL_ID >=1  and  INTERVAL_ID <=3 and (ACTIVITY_TYPE="InVehicle" ) );

Upvotes: 0

Views: 173

Answers (1)

Mohanraja
Mohanraja

Reputation: 188

For your example, you can modify the query in the following way which will be much more efficient, I didn't tested the query.

select
sum(case when activitytype='walking' or activitytype='still' or activitytype='running' then COUNT else 0 end )/60 as physical,

sum(case when activitytype='InVehicle' then COUNT else 0 end)/60 as vehicular

from

activity_table

where interval_id>=1 and interval_id<=3

Upvotes: 1

Related Questions