I have some sqlite querys that are slowding my iPhone App.
I have 2 pattern that I repeat in my app. The first is with substr:
SELECT Id FROM [Table] WHERE UPPER(substr(Name,1,1))='A' ORDER BY Name"
(This run for the mayority of the alphabet. Is for a contact list)
The other is a complex one with date:
SELECT Id,customerCode,
case
when DueDate>=datetime('now','-100 years') AND DueDate<=datetime('now','-1 days') then 'Past due'
when DueDate<datetime('now') then 'Today'
when DueDate>=datetime('now') AND DueDate<=datetime('now','15 days') then '15'
when DueDate>=datetime('now','16 days') AND DueDate<=datetime('now','30 days') then '30'
when DueDate>=datetime('now','31 days') AND DueDate<=datetime('now','45 days') then '45'
when DueDate>=datetime('now','46 days') AND DueDate<=datetime('now','60 days') then '60'
when DueDate>=datetime('now','61 days') AND DueDate<=datetime('now','90 days') then '90'
when DueDate>=datetime('now','91 days') then '> 90'
end As Key,
COUNT(*) As Total, TimeStamp
FROM debt
GROUP BY customerCode,
case
when DueDate>=datetime('now','-100 years') AND DueDate<=datetime('now','-1 days') then '-1'
when DueDate<datetime('now') then 'Today'
when DueDate>=datetime('now') AND DueDate<=datetime('now','15 days') then '15'
when DueDate>=datetime('now','16 days') AND DueDate<=datetime('now','30 days') then '30'
when DueDate>=datetime('now','31 days') AND DueDate<=datetime('now','45 days') then '45'
when DueDate>=datetime('now','46 days') AND DueDate<=datetime('now','60 days') then '60'
when DueDate>=datetime('now','61 days') AND DueDate<=datetime('now','90 days') then '90'
when DueDate>=datetime('now','91 days') then '> 90'
end
In both case I have index for the date & the varchar field.
The 1st run ok in the simulator, and take 11 seconds in the iPod touch 2 GEN
The second query is slow too in the iPhone simulator (3 sec) & 1 min in the device. The dates are using the ISO date form.
I have tougth of build a cache table (and put trigers for the updates. The data is live and the user can modify it) for the data, but wonder if exist a better way.