diff --git a/go/vt/vtgate/planbuilder/symtab.go b/go/vt/vtgate/planbuilder/symtab.go index a676d9bfbc0..0127661ffbe 100644 --- a/go/vt/vtgate/planbuilder/symtab.go +++ b/go/vt/vtgate/planbuilder/symtab.go @@ -130,7 +130,9 @@ func (st *symtab) AddVSchemaTable(alias sqlparser.TableName, vschemaTables []*vi if vindexMap == nil { vindexMap = make(map[*column]vindexes.Vindex) } - vindexMap[col] = cv.Vindex + if vindexMap[col] == nil || vindexMap[col].Cost() > cv.Vindex.Cost() { + vindexMap[col] = cv.Vindex + } } } } diff --git a/go/vt/vtgate/planbuilder/testdata/schema_test.json b/go/vt/vtgate/planbuilder/testdata/schema_test.json index adae208d040..b3dac7176b0 100644 --- a/go/vt/vtgate/planbuilder/testdata/schema_test.json +++ b/go/vt/vtgate/planbuilder/testdata/schema_test.json @@ -66,6 +66,14 @@ "hash_dup": { "type": "hash_test", "owner": "user" + }, + "vindex1": { + "type": "hash_test", + "owner": "samecolvin" + }, + "vindex2": { + "type": "lookup_test", + "owner": "samecolvin" } }, "tables": { @@ -170,6 +178,24 @@ ], "column_list_authoritative": true }, + "samecolvin": { + "column_vindexes": [ + { + "column": "col", + "name": "vindex1" + }, + { + "column": "col", + "name": "vindex2" + } + ], + "columns": [ + { + "name": "col" + } + ], + "column_list_authoritative": true + }, "multicolvin": { "column_vindexes": [ { diff --git a/go/vt/vtgate/planbuilder/testdata/vindex_func_cases.txt b/go/vt/vtgate/planbuilder/testdata/vindex_func_cases.txt index a1af3ec9deb..c889858c8aa 100644 --- a/go/vt/vtgate/planbuilder/testdata/vindex_func_cases.txt +++ b/go/vt/vtgate/planbuilder/testdata/vindex_func_cases.txt @@ -331,3 +331,22 @@ "select none from user_index where id = :id" "symbol none not found in table or subquery" + +"select * from samecolvin where col = :col" +{ + "Original": "select * from samecolvin where col = :col", + "Instructions": { + "Opcode": "SelectEqualUnique", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "Query": "select col from samecolvin where col = :col", + "FieldQuery": "select col from samecolvin where 1 != 1", + "Vindex": "vindex1", + "Values": [ + ":col" + ], + "Table": "samecolvin" + } +}