Android SQLite性能:单个查询中的多个选择在多个查询中单选?

时间:2016-11-09 13:08:34

标签: android sqlite query-performance sqlperformance

性能方面,哪一个更好(对于单个表)?:

  1. 在单个查询中进行多项选择或
  2. 在多个查询中单选?
  3. 示例:

    /*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" ) );
    

1 个答案:

答案 0 :(得分:1)

对于您的示例,您可以通过以下方式修改查询,这将更有效,我没有测试查询。

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