perf(db): Flip order of columns in join table indexes for much faster querying

This commit is contained in:
Ben Gotow 2015-11-24 14:36:55 -08:00
parent aa27692b03
commit f5fff1322b
2 changed files with 3 additions and 3 deletions

View file

@ -32,7 +32,7 @@ describe "DatabaseSetupQueryBuilder", ->
'CREATE TABLE IF NOT EXISTS `TestModel` (id TEXT PRIMARY KEY,data BLOB,client_id TEXT,server_id TEXT)',
'CREATE UNIQUE INDEX IF NOT EXISTS `TestModel_id` ON `TestModel` (`id`)',
'CREATE TABLE IF NOT EXISTS `TestModel-Label` (id TEXT KEY, `value` TEXT)'
'CREATE UNIQUE INDEX IF NOT EXISTS `TestModel_Label_id_val` ON `TestModel-Label` (`id`,`value`)',
'CREATE UNIQUE INDEX IF NOT EXISTS `TestModel_Label_val_id` ON `TestModel-Label` (`value` ASC, `id` ASC)',
]
for query,i in queries
expect(query).toBe(expected[i])

View file

@ -45,9 +45,9 @@ class DatabaseSetupQueryBuilder
attr.queryable && attr instanceof AttributeCollection
collectionAttributes.forEach (attribute) ->
joinTable = tableNameForJoin(klass, attribute.itemClass)
joinIndexName = "#{joinTable.replace('-', '_')}_id_val"
joinIndexName = "#{joinTable.replace('-', '_')}_val_id"
queries.push("CREATE TABLE IF NOT EXISTS `#{joinTable}` (id TEXT KEY, `value` TEXT)")
queries.push("CREATE UNIQUE INDEX IF NOT EXISTS `#{joinIndexName}` ON `#{joinTable}` (`id`,`value`)")
queries.push("CREATE UNIQUE INDEX IF NOT EXISTS `#{joinIndexName}` ON `#{joinTable}` (`value` ASC, `id` ASC)")
joinedDataAttributes = _.filter attributes, (attr) ->
attr instanceof AttributeJoinedData