减少Android中SQl选择查询的执行时间

时间:2014-12-04 10:43:52

标签: android sql performance sqlite

我正在尝试从我的Android代码中使用独立的 SQLite数据库,因此我决定使用SQLiteAssetHelper。我创建了我的类extends SQLiteAssetsHelper,并编写了几个从SQLite数据库中获取数据的方法。这些方法中的每一个都创建与db的数据库连接,并使用Cursor从数据库中检索值。值是单一的,它不是数据集,但 sql 查询有点重,类似这样

select 
round(
sum(cnt / 1000 * price + 
    cnt_arm / 1000 * price_arm1 + 
    cnt_arm / 1000 * price_arm2 + 
    cnt_arm / 1000 * price_arm3)
, 2) price 
from (
select ifnull((2000.0 + 2 * o1.w7), 0) cnt, 
ifnull((2000.0 - 2 * o1.kor_wzm45), 0) cnt_arm, (w1.kurs / 10000) * nh1.info price, ifnull((w_arm1.kurs / 10000) * nh_arm1.info, 0) price_arm1, 
ifnull((w_arm2.kurs / 10000) * nh_arm2.info, 0) price_arm2, 
ifnull((w_arm1.kurs / 10000) * nh_arm3.info, 0) price_arm3, 
osciez1 nr_art_prof, nh1.nazwa, nh1.waluta 
from typyp t1 
inner join Osciezp o1 on t1.osciez1 = o1.nr_art 
left join wzmocn wz1 on wz1.nr_art = o1.wzm1 and 'a' = 'a' 
left join wzmocn wz2 on wz2.nr_art = o1.wzm2 and 'a' = 'b' 
left join wzmocn wz3 on wz3.nr_art = o1.wzm3  and 'a' = 'c' 
inner join profhand ph1 on ph1.nr_art = o1.nr_art 
inner join koloryp k1 on k1.indeks = ph1.idx_koloru 
inner join nhandlo1 nh1 on nh1.nr_art = ph1.nr_art_han 
left join nhandlo1 nh_arm1 on nh_arm1.nr_art = wz1.nr_art 
left join nhandlo1 nh_arm2 on nh_arm2.nr_art = wz2.nr_art 
left join nhandlo1 nh_arm3 on nh_arm3.nr_art = wz3.nr_art 
inner join waluty w1 on w1.nr_art = nh1.waluta 
left join waluty w_arm1 on w_arm1.nr_art = nh_arm1.waluta 
left join waluty w_arm2 on w_arm2.nr_art = nh_arm2.waluta 
left join waluty w_arm3 on w_arm3.nr_art = nh_arm3.waluta 
where t1.indeks = 1 and k1.indeks = 1   
union all  
select 
ifnull((1000.0 + 2 * o2.w7), 0) cnt, 
ifnull((1000.0 - 2 * o2.kor_wzm45), 0) cnt_arm, 
(w2.kurs / 10000) * nh2.info price, 
ifnull((w_arm1.kurs / 10000) * nh_arm1.info, 0) price_arm1, 
ifnull((w_arm2.kurs / 10000) * nh_arm2.info, 0) price_arm2, 
ifnull((w_arm1.kurs / 10000) * nh_arm3.info, 0) price_arm3, 
osciez2 nr_art_prof, nh2.nazwa, nh2.waluta 
from typyp t2 
inner join Osciezp o2 on t2.osciez2 = o2.nr_art 
left join wzmocn wz1 on wz1.nr_art = o2.wzm1 and 'a' = 'a' 
left join wzmocn wz2 on wz2.nr_art = o2.wzm2 and 'a' = 'b' 
left join wzmocn wz3 on wz3.nr_art = o2.wzm3   and 'a' = 'c' 
inner join profhand ph2 on ph2.nr_art = o2.nr_art 
inner join koloryp k2 on k2.indeks = ph2.idx_koloru 
inner join nhandlo1 nh2 on nh2.nr_art = ph2.nr_art_han 
left join nhandlo1 nh_arm1 on nh_arm1.nr_art = wz1.nr_art 
left join nhandlo1 nh_arm2 on nh_arm2.nr_art = wz2.nr_art 
left join nhandlo1 nh_arm3 on nh_arm3.nr_art = wz3.nr_art 
inner join waluty w2 on w2.nr_art = nh2.waluta 
left join waluty w_arm1 on w_arm1.nr_art = nh_arm1.waluta 
left join waluty w_arm2 on w_arm2.nr_art = nh_arm2.waluta 
left join waluty w_arm3 on w_arm3.nr_art = nh_arm3.waluta 
where t2.indeks = 1 and k2.indeks = 1   
union all  
select 
ifnull((2000.0 + 2 * o3.w7), 0) cnt, 
ifnull((2000.0 - 2 * o3.kor_wzm45), 0) cnt_arm, 
(w3.kurs / 10000) * nh3.info price, 
ifnull((w_arm1.kurs / 10000) * nh_arm1.info, 0) price_arm1, 
ifnull((w_arm2.kurs / 10000) * nh_arm2.info, 0) price_arm2, 
ifnull((w_arm1.kurs / 10000) * nh_arm3.info, 0) price_arm3, 
osciez3 nr_art_prof, nh3.nazwa, nh3.waluta 
from typyp t3 
inner join Osciezp o3 on t3.osciez3 = o3.nr_art 
left join wzmocn wz1 on wz1.nr_art = o3.wzm1 and 'a' = 'a' 
left join wzmocn wz2 on wz2.nr_art = o3.wzm2 and 'a' = 'b' 
left join wzmocn wz3 on wz3.nr_art = o3.wzm3   and 'a' = 'c' 
inner join profhand ph3 on ph3.nr_art = o3.nr_art 
inner join koloryp k3 on k3.indeks = ph3.idx_koloru 
inner join nhandlo1 nh3 on nh3.nr_art = ph3.nr_art_han 
left join nhandlo1 nh_arm1 on nh_arm1.nr_art = wz1.nr_art 
left join nhandlo1 nh_arm2 on nh_arm2.nr_art = wz2.nr_art 
left join nhandlo1 nh_arm3 on nh_arm3.nr_art = wz3.nr_art 
inner join waluty w3 on w3.nr_art = nh3.waluta 
left join waluty w_arm1 on w_arm1.nr_art = nh_arm1.waluta 
left join waluty w_arm2 on w_arm2.nr_art = nh_arm2.waluta 
left join waluty w_arm3 on w_arm3.nr_art = nh_arm3.waluta 
where t3.indeks = 1 and k3.indeks = 1   
union all  
select 
ifnull((1000.0 + 2 * o4.w7), 0) cnt, 
ifnull((1000.0 - 2 * o4.kor_wzm45), 0) cnt_arm, (w4.kurs / 10000) * nh4.info price, ifnull((w_arm1.kurs / 10000) * nh_arm1.info, 0) price_arm1, 
ifnull((w_arm2.kurs / 10000) * nh_arm2.info, 0) price_arm2, 
ifnull((w_arm1.kurs / 10000) * nh_arm3.info, 0) price_arm3, 
osciez4 nr_art_prof, nh4.nazwa, nh4.waluta 
from typyp t4 
inner join Osciezp o4 on t4.osciez4 = o4.nr_art 
left join wzmocn wz1 on wz1.nr_art = o4.wzm1 and 'a' = 'a' 
left join wzmocn wz2 on wz2.nr_art = o4.wzm2 and 'a' = 'b' 
left join wzmocn wz3 on wz3.nr_art = o4.wzm3   and 'a' = 'c' 
inner join profhand ph4 on ph4.nr_art = o4.nr_art 
inner join koloryp k4 on k4.indeks = ph4.idx_koloru 
inner join nhandlo1 nh4 on nh4.nr_art = ph4.nr_art_han 
left join nhandlo1 nh_arm1 on nh_arm1.nr_art = wz1.nr_art 
left join nhandlo1 nh_arm2 on nh_arm2.nr_art = wz2.nr_art 
left join nhandlo1 nh_arm3 on nh_arm3.nr_art = wz3.nr_art 
inner join waluty w4 on w4.nr_art = nh4.waluta 
left join waluty w_arm1 on w_arm1.nr_art = nh_arm1.waluta 
left join waluty w_arm2 on w_arm2.nr_art = nh_arm2.waluta 
left join waluty w_arm3 on w_arm3.nr_art = nh_arm3.waluta 
where t4.indeks = 1 and k4.indeks = 1   ) t;

当我尝试执行此查询时,需要更多时间来执行(在Android Emulator上大约2分钟)。如何减少执行时间。我可以使用compileStatement来满足这种需求吗?它是否与游标选择语句兼容?

以下是查询的执行计划:

4|0|0|SEARCH TABLE typyp AS t1 USING INTEGER PRIMARY KEY (rowid=?)
4|1|1|SEARCH TABLE Osciezp AS o1 USING INDEX osciezp_nr_art (nr_art=?)
4|2|2|SEARCH TABLE wzmocn AS wz1 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
4|3|3|SEARCH TABLE wzmocn AS wz2 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
4|4|4|SEARCH TABLE wzmocn AS wz3 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
4|5|5|SEARCH TABLE profhand AS ph1 USING INDEX profhand_NrArtKolor (nr_art=? AND
 idx_koloru=?)
4|6|6|SEARCH TABLE koloryp AS k1 USING INTEGER PRIMARY KEY (rowid=?)
4|7|7|SEARCH TABLE nhandlo1 AS nh1 USING INDEX nhandlo1_nr_art (nr_art=?)
4|8|8|SEARCH TABLE nhandlo1 AS nh_arm1 USING INDEX nhandlo1_nr_art (nr_art=?)
4|9|9|SEARCH TABLE nhandlo1 AS nh_arm2 USING INDEX nhandlo1_nr_art (nr_art=?)
4|10|10|SEARCH TABLE nhandlo1 AS nh_arm3 USING INDEX nhandlo1_nr_art (nr_art=?)
4|11|11|SEARCH TABLE waluty AS w1 USING INDEX waluty_nr_art (nr_art=?)
4|12|12|SEARCH TABLE waluty AS w_arm1 USING INDEX waluty_nr_art (nr_art=?)
4|13|13|SEARCH TABLE waluty AS w_arm2 USING INDEX waluty_nr_art (nr_art=?)
4|14|14|SEARCH TABLE waluty AS w_arm3 USING COVERING INDEX waluty_nr_art (nr_art
=?)
5|0|0|SEARCH TABLE typyp AS t2 USING INTEGER PRIMARY KEY (rowid=?)
5|1|1|SEARCH TABLE Osciezp AS o2 USING INDEX osciezp_nr_art (nr_art=?)
5|2|2|SEARCH TABLE wzmocn AS wz1 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
5|3|3|SEARCH TABLE wzmocn AS wz2 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
5|4|4|SEARCH TABLE wzmocn AS wz3 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
5|5|5|SEARCH TABLE profhand AS ph2 USING INDEX profhand_NrArtKolor (nr_art=? AND
 idx_koloru=?)
5|6|6|SEARCH TABLE koloryp AS k2 USING INTEGER PRIMARY KEY (rowid=?)
5|7|7|SEARCH TABLE nhandlo1 AS nh2 USING INDEX nhandlo1_nr_art (nr_art=?)
5|8|8|SEARCH TABLE nhandlo1 AS nh_arm1 USING INDEX nhandlo1_nr_art (nr_art=?)
5|9|9|SEARCH TABLE nhandlo1 AS nh_arm2 USING INDEX nhandlo1_nr_art (nr_art=?)
5|10|10|SEARCH TABLE nhandlo1 AS nh_arm3 USING INDEX nhandlo1_nr_art (nr_art=?)
5|11|11|SEARCH TABLE waluty AS w2 USING INDEX waluty_nr_art (nr_art=?)
5|12|12|SEARCH TABLE waluty AS w_arm1 USING INDEX waluty_nr_art (nr_art=?)
5|13|13|SEARCH TABLE waluty AS w_arm2 USING INDEX waluty_nr_art (nr_art=?)
5|14|14|SEARCH TABLE waluty AS w_arm3 USING COVERING INDEX waluty_nr_art (nr_art
=?)
3|0|0|COMPOUND SUBQUERIES 4 AND 5 (UNION ALL)
6|0|0|SEARCH TABLE typyp AS t3 USING INTEGER PRIMARY KEY (rowid=?)
6|1|1|SEARCH TABLE Osciezp AS o3 USING INDEX osciezp_nr_art (nr_art=?)
6|2|2|SEARCH TABLE wzmocn AS wz1 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
6|3|3|SEARCH TABLE wzmocn AS wz2 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
6|4|4|SEARCH TABLE wzmocn AS wz3 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
6|5|5|SEARCH TABLE profhand AS ph3 USING INDEX profhand_NrArtKolor (nr_art=? AND
 idx_koloru=?)
6|6|6|SEARCH TABLE koloryp AS k3 USING INTEGER PRIMARY KEY (rowid=?)
6|7|7|SEARCH TABLE nhandlo1 AS nh3 USING INDEX nhandlo1_nr_art (nr_art=?)
6|8|8|SEARCH TABLE nhandlo1 AS nh_arm1 USING INDEX nhandlo1_nr_art (nr_art=?)
6|9|9|SEARCH TABLE nhandlo1 AS nh_arm2 USING INDEX nhandlo1_nr_art (nr_art=?)
6|10|10|SEARCH TABLE nhandlo1 AS nh_arm3 USING INDEX nhandlo1_nr_art (nr_art=?)
6|11|11|SEARCH TABLE waluty AS w3 USING INDEX waluty_nr_art (nr_art=?)
6|12|12|SEARCH TABLE waluty AS w_arm1 USING INDEX waluty_nr_art (nr_art=?)
6|13|13|SEARCH TABLE waluty AS w_arm2 USING INDEX waluty_nr_art (nr_art=?)
6|14|14|SEARCH TABLE waluty AS w_arm3 USING COVERING INDEX waluty_nr_art (nr_art
=?)
2|0|0|COMPOUND SUBQUERIES 3 AND 6 (UNION ALL)
7|0|0|SEARCH TABLE typyp AS t4 USING INTEGER PRIMARY KEY (rowid=?)
7|1|1|SEARCH TABLE Osciezp AS o4 USING INDEX osciezp_nr_art (nr_art=?)
7|2|2|SEARCH TABLE wzmocn AS wz1 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
7|3|3|SEARCH TABLE wzmocn AS wz2 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
7|4|4|SEARCH TABLE wzmocn AS wz3 USING COVERING INDEX wzmocn_nr_art (nr_art=?)
7|5|5|SEARCH TABLE profhand AS ph4 USING INDEX profhand_NrArtKolor (nr_art=? AND
 idx_koloru=?)
7|6|6|SEARCH TABLE koloryp AS k4 USING INTEGER PRIMARY KEY (rowid=?)
7|7|7|SEARCH TABLE nhandlo1 AS nh4 USING INDEX nhandlo1_nr_art (nr_art=?)
7|8|8|SEARCH TABLE nhandlo1 AS nh_arm1 USING INDEX nhandlo1_nr_art (nr_art=?)
7|9|9|SEARCH TABLE nhandlo1 AS nh_arm2 USING INDEX nhandlo1_nr_art (nr_art=?)
7|10|10|SEARCH TABLE nhandlo1 AS nh_arm3 USING INDEX nhandlo1_nr_art (nr_art=?)
7|11|11|SEARCH TABLE waluty AS w4 USING INDEX waluty_nr_art (nr_art=?)
7|12|12|SEARCH TABLE waluty AS w_arm1 USING INDEX waluty_nr_art (nr_art=?)
7|13|13|SEARCH TABLE waluty AS w_arm2 USING INDEX waluty_nr_art (nr_art=?)
7|14|14|SEARCH TABLE waluty AS w_arm3 USING COVERING INDEX waluty_nr_art (nr_art
=?)
1|0|0|COMPOUND SUBQUERIES 2 AND 7 (UNION ALL)
0|0|0|SCAN SUBQUERY 1 AS t

0 个答案:

没有答案