1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-18 00:08:17 +00:00

Fix slow labels queries (#762)

* Add PRAGMA temp_store=2 to write temporary tables in memory

* Only use DISTINCT when filtering labels

* Log long queries to debug

* Add unit test
This commit is contained in:
Tom Lebreux
2025-08-06 20:07:09 -04:00
committed by GitHub
parent 5f08feeb5f
commit a020084518
3 changed files with 103 additions and 7 deletions

View File

@@ -507,6 +507,62 @@ func TestNewListOptionIndexerEasy(t *testing.T) {
expectedContToken: "",
expectedErr: nil,
})
tests = append(tests, testCase{
description: "ListByOptions with single object matching many labels with AND",
listOptions: sqltypes.ListOptions{Filters: []sqltypes.OrFilter{
{
[]sqltypes.Filter{
{
Field: []string{"metadata", "labels", "cows"},
Matches: []string{"milk"},
Op: sqltypes.Eq,
},
},
},
{
[]sqltypes.Filter{
{
Field: []string{"metadata", "labels", "horses"},
Matches: []string{"shoes"},
Op: sqltypes.Eq,
},
},
},
},
},
partitions: []partition.Partition{{All: true}},
ns: "",
expectedList: makeList(t, obj02b_milk_shoes),
expectedTotal: 1,
expectedContToken: "",
expectedErr: nil,
})
tests = append(tests, testCase{
description: "ListByOptions with many objects matching many labels with OR",
listOptions: sqltypes.ListOptions{Filters: []sqltypes.OrFilter{
{
[]sqltypes.Filter{
{
Field: []string{"metadata", "labels", "cows"},
Matches: []string{"milk"},
Op: sqltypes.Eq,
},
{
Field: []string{"metadata", "labels", "horses"},
Matches: []string{"shoes"},
Op: sqltypes.Eq,
},
},
},
},
},
partitions: []partition.Partition{{All: true}},
ns: "",
expectedList: makeList(t, obj02_milk_saddles, obj02b_milk_shoes, obj03a_shoes, obj04_milk),
expectedTotal: 4,
expectedContToken: "",
expectedErr: nil,
})
tests = append(tests, testCase{
description: "ListByOptions with 1 OrFilter set with 1 filter should select where that filter is true",
listOptions: sqltypes.ListOptions{Filters: []sqltypes.OrFilter{
@@ -835,6 +891,25 @@ func TestNewListOptionIndexerEasy(t *testing.T) {
expectedContToken: "",
expectedErr: nil,
})
tests = append(tests, testCase{
description: "ListByOptions sorting on two existing labels, with no label filters, should sort correctly",
listOptions: sqltypes.ListOptions{
SortList: sqltypes.SortList{
SortDirectives: []sqltypes.Sort{
{
Fields: []string{"metadata", "labels", "horses"},
},
{
Fields: []string{"metadata", "labels", "cows"},
},
},
},
},
partitions: []partition.Partition{{All: true}},
expectedList: makeList(t, obj02a_beef_saddles, obj02_milk_saddles, obj03_saddles,
obj02b_milk_shoes, obj03a_shoes, obj04_milk, obj01_no_labels, obj05__guard_lodgepole),
expectedTotal: len(allObjects),
})
tests = append(tests, testCase{
description: "ListByOptions with Pagination.PageSize set should set limit to PageSize",
listOptions: sqltypes.ListOptions{
@@ -1270,7 +1345,7 @@ func TestConstructQuery(t *testing.T) {
},
partitions: []partition.Partition{},
ns: "",
expectedStmt: `SELECT DISTINCT o.object, o.objectnonce, o.dekid FROM "something" o
expectedStmt: `SELECT o.object, o.objectnonce, o.dekid FROM "something" o
JOIN "something_fields" f ON o.key = f.key
WHERE
(f."metadata.queryField1" IN (?)) AND
@@ -1295,7 +1370,7 @@ func TestConstructQuery(t *testing.T) {
},
partitions: []partition.Partition{},
ns: "",
expectedStmt: `SELECT DISTINCT o.object, o.objectnonce, o.dekid FROM "something" o
expectedStmt: `SELECT o.object, o.objectnonce, o.dekid FROM "something" o
JOIN "something_fields" f ON o.key = f.key
WHERE
(f."metadata.queryField1" NOT IN (?)) AND
@@ -1680,7 +1755,7 @@ func TestConstructQuery(t *testing.T) {
},
partitions: []partition.Partition{},
ns: "",
expectedStmt: `SELECT DISTINCT o.object, o.objectnonce, o.dekid FROM "something" o
expectedStmt: `SELECT o.object, o.objectnonce, o.dekid FROM "something" o
JOIN "something_fields" f ON o.key = f.key
WHERE
(extractBarredValue(f."spec.containers.image", "3") = ?) AND
@@ -1703,7 +1778,7 @@ func TestConstructQuery(t *testing.T) {
},
partitions: []partition.Partition{},
ns: "",
expectedStmt: `SELECT DISTINCT o.object, o.objectnonce, o.dekid FROM "something" o
expectedStmt: `SELECT o.object, o.objectnonce, o.dekid FROM "something" o
JOIN "something_fields" f ON o.key = f.key
WHERE
(FALSE)
@@ -1736,7 +1811,7 @@ func TestConstructQuery(t *testing.T) {
},
partitions: []partition.Partition{},
ns: "",
expectedStmt: `SELECT DISTINCT o.object, o.objectnonce, o.dekid FROM "something" o
expectedStmt: `SELECT o.object, o.objectnonce, o.dekid FROM "something" o
JOIN "something_fields" f ON o.key = f.key
WHERE
(extractBarredValue(f."spec.containers.image", "3") = ?) AND
@@ -1883,7 +1958,7 @@ func TestConstructQuery(t *testing.T) {
SELECT key, value FROM "something_labels"
WHERE label = ?
)
SELECT DISTINCT o.object, o.objectnonce, o.dekid FROM "something" o
SELECT o.object, o.objectnonce, o.dekid FROM "something" o
JOIN "something_fields" f ON o.key = f.key
LEFT OUTER JOIN lt1 ON o.key = lt1.key
WHERE