Commit 3ddb6663 authored by Julien Muchembled's avatar Julien Muchembled

storage: a few "ORDER BY" were missing in backends

In practice, it may be only required for SQLite (because we don't force index)
on 'obj' (which as several indices).
parent 6ffafcbd
...@@ -475,6 +475,7 @@ class MySQLDatabaseManager(MVCCDatabaseManager): ...@@ -475,6 +475,7 @@ class MySQLDatabaseManager(MVCCDatabaseManager):
def _getPackOrders(self, min_completed): def _getPackOrders(self, min_completed):
return self.query( return self.query(
"SELECT * FROM pack WHERE tid >= %s AND tid %% %s IN (%s)" "SELECT * FROM pack WHERE tid >= %s AND tid %% %s IN (%s)"
" ORDER BY tid"
% (min_completed, self.np, ','.join(map(str, self._readable_set)))) % (min_completed, self.np, ','.join(map(str, self._readable_set))))
def getPackedIDs(self, up_to_date=False): def getPackedIDs(self, up_to_date=False):
...@@ -986,7 +987,7 @@ class MySQLDatabaseManager(MVCCDatabaseManager): ...@@ -986,7 +987,7 @@ class MySQLDatabaseManager(MVCCDatabaseManager):
"SELECT `partition`, oid, MAX(tid) AS tid" "SELECT `partition`, oid, MAX(tid) AS tid"
" FROM obj FORCE INDEX(PRIMARY)" " FROM obj FORCE INDEX(PRIMARY)"
" WHERE `partition`=%s AND oid>=%s AND tid<=%s" " WHERE `partition`=%s AND oid>=%s AND tid<=%s"
" GROUP BY oid LIMIT %s" " GROUP BY oid ORDER BY oid LIMIT %s"
") AS t USING (`partition`, oid, tid)" ") AS t USING (`partition`, oid, tid)"
% (partition, u64(min_oid), u64(tid), length)) % (partition, u64(min_oid), u64(tid), length))
return None if len(r) < length else p64(r[-1][0] + self.np), \ return None if len(r) < length else p64(r[-1][0] + self.np), \
...@@ -1005,17 +1006,18 @@ class MySQLDatabaseManager(MVCCDatabaseManager): ...@@ -1005,17 +1006,18 @@ class MySQLDatabaseManager(MVCCDatabaseManager):
def _pack(self, offset, oid, tid, limit=None): def _pack(self, offset, oid, tid, limit=None):
q = self.query q = self.query
data_id_set = set() data_id_set = set()
sql = ("SELECT obj.oid," sql = ("SELECT obj.oid AS oid,"
" IF(data_id IS NULL OR n>1, tid + (data_id IS NULL), NULL)" " IF(data_id IS NULL OR n>1, tid + (data_id IS NULL), NULL)"
" FROM (SELECT COUNT(*) AS n, oid, MAX(tid) AS max_tid" " FROM (SELECT COUNT(*) AS n, oid, MAX(tid) AS max_tid"
" FROM obj FORCE INDEX(PRIMARY)" " FROM obj FORCE INDEX(PRIMARY)"
" WHERE `partition`=%s AND oid%s AND tid<=%s" " WHERE `partition`=%s AND oid%s AND tid<=%s"
" GROUP BY oid%s) AS t" " GROUP BY oid%s) AS t"
" JOIN obj ON `partition`=%s AND t.oid=obj.oid AND tid=max_tid") % ( " JOIN obj ON `partition`=%s AND t.oid=obj.oid AND tid=max_tid"
" ORDER BY oid") % (
offset, offset,
">=%s" % oid if limit else " IN (%s)" % ','.join(map(str, oid)), ">=%s" % oid if limit else " IN (%s)" % ','.join(map(str, oid)),
tid, tid,
" LIMIT %s" % limit if limit else "", " ORDER BY oid LIMIT %s" % limit if limit else "",
offset) offset)
oid = None oid = None
for oid, tid in q(sql): for oid, tid in q(sql):
......
...@@ -361,6 +361,7 @@ class SQLiteDatabaseManager(DatabaseManager): ...@@ -361,6 +361,7 @@ class SQLiteDatabaseManager(DatabaseManager):
def _getPackOrders(self, min_completed): def _getPackOrders(self, min_completed):
return self.query( return self.query(
"SELECT * FROM pack WHERE tid >= ? AND tid %% %s IN (%s)" "SELECT * FROM pack WHERE tid >= ? AND tid %% %s IN (%s)"
" ORDER BY tid"
% (self.np, ','.join(map(str, self._readable_set))), % (self.np, ','.join(map(str, self._readable_set))),
(min_completed,)) (min_completed,))
...@@ -778,7 +779,7 @@ class SQLiteDatabaseManager(DatabaseManager): ...@@ -778,7 +779,7 @@ class SQLiteDatabaseManager(DatabaseManager):
r = self.query("SELECT oid, data_id FROM obj JOIN (" r = self.query("SELECT oid, data_id FROM obj JOIN ("
"SELECT `partition`, oid, MAX(tid) AS tid FROM obj" "SELECT `partition`, oid, MAX(tid) AS tid FROM obj"
" WHERE partition=? AND oid>=? AND tid<=?" " WHERE partition=? AND oid>=? AND tid<=?"
" GROUP BY oid LIMIT ?" " GROUP BY oid ORDER BY oid LIMIT ?"
") AS t USING (`partition`, oid, tid)", ") AS t USING (`partition`, oid, tid)",
(partition, u64(min_oid), u64(tid), length)).fetchall() (partition, u64(min_oid), u64(tid), length)).fetchall()
return None if len(r) < length else p64(r[-1][0] + self.np), \ return None if len(r) < length else p64(r[-1][0] + self.np), \
...@@ -808,7 +809,7 @@ class SQLiteDatabaseManager(DatabaseManager): ...@@ -808,7 +809,7 @@ class SQLiteDatabaseManager(DatabaseManager):
" WHERE partition=%s AND tid<=%s AND oid%s GROUP BY oid%s") % ( " WHERE partition=%s AND tid<=%s AND oid%s GROUP BY oid%s") % (
offset, tid, offset, tid,
">=%s" % oid if limit else " IN (%s)" % ','.join(map(str, oid)), ">=%s" % oid if limit else " IN (%s)" % ','.join(map(str, oid)),
" LIMIT %s" % limit if limit else "") " ORDER BY oid LIMIT %s" % limit if limit else "")
oid = None oid = None
for x, oid, max_tid in q(sql): for x, oid, max_tid in q(sql):
for x in q("SELECT tid + (data_id IS NULL) FROM obj" for x in q("SELECT tid + (data_id IS NULL) FROM obj"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment