I’m developing an app that takes raw transit data and displays the upcoming train schedule. I use the following query to retrieve the list of stations (from a table containing approximately 50,000 entries for stop times):
SELECT DISTINCT SUBSTR(stops._id, 0, LENGTH(stops._id)) as _id, stops.stop_name
FROM trips, stop_times, stops
WHERE trips._id = 'B'
AND trips.trip_id = stop_times._id
AND stop_times.stop_id = stops._id
I ran this query in an SQLite browser and it executed in about 0.8 seconds. But when I execute it in my DatabaseHelper class using rawQuery(), it takes in excess of 5 minutes on my physical device. I added some logging statements to see where the slowdown was occurring and it appears that it is the query itself which is taking a long time, not the inflation of the View. When I execute the same query without the DISTINCT option, it executes in about 1 second.
I could understand the Android library taking a bit longer to execute than the browser but it should not be several hundred times slower. Is there something about the DISTINCT option that is causing this?
DISTINCTtakes time to process as it is a matter of sorting and selection. In case if you have huge data, DISTINCT will just make it even worse. I would suggest you to breakdown your query into smaller parts and/or do the filtering pragmatically