优化android sqlite的SQL语句
我正在开发一个应用程序来跟踪用户的当前位置并将其存储到 SQLite 数据库中。一切正常,但现在我在查询数据库中查找超过 1000 条记录的曲目时遇到问题,需要近 1.5 分钟。在我的桌面上只需要 1 秒。
我知道这是一个包含许多子选择的查询,但我无法以其他方式获得正确的结果。在我看来,这属于像 avg() 和 sum() 这样的聚合函数。
这是我的查询:
Cursor c = readableDB
.rawQuery(
"SELECT DISTINCT t._id , title , strftime('%Y-%m-%d' , starttime , 'unixepoch' , 'localtime') as date , description, "
+ "round((SELECT sum(distToPrev)/1000 FROM positions p WHERE p.trackid=t._id) , 2) as distance , "
+ "(SELECT count(latitude) FROM positions p WHERE p.trackid=t._id) as waypoints, "
+ "(SELECT (avg(speed)*3.6) FROM positions p WHERE p.trackid=t._id) as avgspeed, "
+ "(SELECT (max(speed)*3.6) FROM positions p WHERE p.trackid=t._id) as maxspeed, "
+ "(SELECT sum(altitudeUP) FROM positions p WHERE p.trackid=t._id) as climb , "
+ "(SELECT avg(heartbeat) FROM heartbeats h WHERE h.trackid=t._id) as avgheartbeat , "
+ "(SELECT max(heartbeat) FROM heartbeats h WHERE h.trackid=t._id) as maxheartbeat , "
+ "(SELECT avg(cadence) FROM heartbeats h WHERE h.trackid=t._id) as avgcadence "
+ "FROM tracks t LEFT JOIN heartbeats h ON t._id = h.trackid WHERE t._id = ?",
new String[]{String.valueOf(trackId)});
c.moveToFirst();
如何优化此查询?我已经尝试过这种方式,但结果是错误的,并且花费了相同的时间。
SELECT t._id , title , strftime('%Y-%m-%d' , starttime , 'unixepoch' , 'localtime') as date , description,
sum(distToPrev)/1000 as distance ,
count(latitude) as waypoints,
(avg(speed)*3.6) as avgspeed,
(max(speed)*3.6) as maxspeed,
sum(altitudeUP) as climb ,
avg(heartbeat) as avgheartbeat ,
max(heartbeat) as maxheartbeat ,
avg(cadence) as avgcadence
FROM tracks t
LEFT JOIN heartbeats h ON t._id = h.trackid
INNER JOIN positions p ON t._id = p.trackid
WHERE t._id = ?
两个小时以来我一直在寻找解决方案,但我不知道我做错了什么。也许我必须休息一下。
编辑:
这里是我的创建语句:
CREATE TABLE heartbeats(_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, heartbeat INTEGER NOT NULL, cadence INTEGER, timestamp TIMESTAMP);
CREATE TABLE positions(_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, longitude REAL NOT NULL, latitude REAL NOT NULL, altitude REAL, altitudeUP REAL, speed REAL, accuracy REAL, distToPrev REAL, timestamp TIMESTAMP);
CREATE TABLE tracks(_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, description TEXT, starttime DATETIME NOT NULL, endtime DATETIME);
I'm developing an application that tracks the user's current position and stores it into a SQLite database. Everything works fine, but now I have the problem when querying the database for a track with more than 1000 records it takes nearly 1.5 minutes. On my Desktop it just takes 1 second.
I know it's a query with many subselects but I wasn't able to get the right result another way. In my opinion this belongs to the aggregate functions like avg() and sum().
Here's my query:
Cursor c = readableDB
.rawQuery(
"SELECT DISTINCT t._id , title , strftime('%Y-%m-%d' , starttime , 'unixepoch' , 'localtime') as date , description, "
+ "round((SELECT sum(distToPrev)/1000 FROM positions p WHERE p.trackid=t._id) , 2) as distance , "
+ "(SELECT count(latitude) FROM positions p WHERE p.trackid=t._id) as waypoints, "
+ "(SELECT (avg(speed)*3.6) FROM positions p WHERE p.trackid=t._id) as avgspeed, "
+ "(SELECT (max(speed)*3.6) FROM positions p WHERE p.trackid=t._id) as maxspeed, "
+ "(SELECT sum(altitudeUP) FROM positions p WHERE p.trackid=t._id) as climb , "
+ "(SELECT avg(heartbeat) FROM heartbeats h WHERE h.trackid=t._id) as avgheartbeat , "
+ "(SELECT max(heartbeat) FROM heartbeats h WHERE h.trackid=t._id) as maxheartbeat , "
+ "(SELECT avg(cadence) FROM heartbeats h WHERE h.trackid=t._id) as avgcadence "
+ "FROM tracks t LEFT JOIN heartbeats h ON t._id = h.trackid WHERE t._id = ?",
new String[]{String.valueOf(trackId)});
c.moveToFirst();
How can I optimize this query? I tried it already this way, but then the result was wrong and it took the same amount of time.
SELECT t._id , title , strftime('%Y-%m-%d' , starttime , 'unixepoch' , 'localtime') as date , description,
sum(distToPrev)/1000 as distance ,
count(latitude) as waypoints,
(avg(speed)*3.6) as avgspeed,
(max(speed)*3.6) as maxspeed,
sum(altitudeUP) as climb ,
avg(heartbeat) as avgheartbeat ,
max(heartbeat) as maxheartbeat ,
avg(cadence) as avgcadence
FROM tracks t
LEFT JOIN heartbeats h ON t._id = h.trackid
INNER JOIN positions p ON t._id = p.trackid
WHERE t._id = ?
Since 2 hours I'm looking for a solution and I don't know what I'm doing wrong. Maybe I have to take a break.
EDIT:
Here my create statements:
CREATE TABLE heartbeats(_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, heartbeat INTEGER NOT NULL, cadence INTEGER, timestamp TIMESTAMP);
CREATE TABLE positions(_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, longitude REAL NOT NULL, latitude REAL NOT NULL, altitude REAL, altitudeUP REAL, speed REAL, accuracy REAL, distToPrev REAL, timestamp TIMESTAMP);
CREATE TABLE tracks(_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, description TEXT, starttime DATETIME NOT NULL, endtime DATETIME);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要过度简化,但我认为您可以通过简单地对查询期间扫描的字段建立索引来节省大量查询时间。
通过对字段建立索引,您将减轻随着记录数量的增长而减慢查询速度的所有表扫描。
索引的缺点是需要空间,但根据我在 Android 上的经验,这很少是一个因素。
Not to oversimplify, but I think you could shave a ton of time off your queries by simply indexing the fields which you are scanning during the query.
By indexing the fields, you will alleviate all of the table scanning that slows down queries as the number of records grow.
The downside of Indexes is the space requirement, but in my experience on Android, it's rarely a factor.
这将完成这项工作:
根据需要添加字段。
This will do the job:
Add fields as required.
像这样的事情可能会让你接近。请注意,我已在查询中多次包含该参数:
Something like this might get you close. Notice that I've included the parameter multiple times in the query: