diff --git a/mysql-test/suite/fb_vectordb/r/vectordb_count.result b/mysql-test/suite/fb_vectordb/r/vectordb_count.result new file mode 100644 index 000000000000..6ad57bf0d5db --- /dev/null +++ b/mysql-test/suite/fb_vectordb/r/vectordb_count.result @@ -0,0 +1,263 @@ +CREATE TABLE t1 ( +id BIGINT NOT NULL PRIMARY KEY, +a int, +b int, +vector1 JSON, +INDEX vector_key_1(vector1) FB_VECTOR_INDEX_TYPE 'flat' fb_vector_dimension 3 +); +insert into t1 values (1, 2, 2, '[1,2,3]'), (2, 1, 2, '[2,22,31]'); +insert into t1 values (3, 1, 1, '[1,2,4]'), (4, 2, 1, '[2,22,33]'); +insert into t1 values (5, 3, 1, '[11,22,4]'), (6, 2, 3, '[7,22,33]'); +insert into t1 values (7, 3, 2, '[10,20,40]'), (8, 2, 1, '[20,22,41]'); +insert into t1 values (9, 1, 1, '[20,10,30]'), (10, 1, 2, '[25,25,41]'); + +1. Verify basic COUNT(*) on the table + +explain select COUNT(*) from t1; +id select_type table partitions type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 NULL index NULL PRIMARY 8 NULL ROWS FILTERED Using index + +SELECT COUNT(*) from t1; +COUNT(*) +10 + +2. Verify COUNT(*) with WHERE clause with REF + +explain select COUNT(*) from t1 WHERE id = 2; +id select_type table partitions type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 NULL const PRIMARY PRIMARY 8 const ROWS FILTERED Using index + +SELECT COUNT(*) from t1 WHERE id = 2; +COUNT(*) +1 + +3. Verify COUNT(*) with WHERE clause with RANGE + +explain select COUNT(*) from t1 WHERE id > 2 and id < 10; +id select_type table partitions type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 NULL index PRIMARY PRIMARY 8 NULL ROWS FILTERED Using where; Using index + +SELECT COUNT(*) from t1 WHERE id = 2 > 2 and id < 10; +COUNT(*) +0 + +4. Verify COUNT(*) with WHERE clause - other columns + +explain select COUNT(*) from t1 WHERE a > 1 and b < 3 and id > 4; +id select_type table partitions type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 NULL range PRIMARY PRIMARY 8 NULL ROWS FILTERED Using where + +SELECT COUNT(*) from t1 WHERE a > 1 and b < 3 and id > 4; +COUNT(*) +3 + +5. Verify COUNT(*) with GROUP BY on non-key column + +explain format=tree select a, COUNT(*) from t1 group by a; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t1 (rows=1) + + +SELECT a, COUNT(*) from t1 GROUP BY a; +a COUNT(*) +2 4 +1 4 +3 2 + +6. Verify COUNT(*) with GROUP BY on multiple non-key columns + +explain format=tree select a, b, COUNT(*) from t1 group by a, b; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t1 (rows=1) + + +SELECT a, b, COUNT(*) FROM t1 GROUP BY a, b; +a b COUNT(*) +2 2 1 +1 2 2 +1 1 2 +2 1 2 +3 1 1 +2 3 1 +3 2 1 + +7. Verify COUNT(*) with GROUP BY on non-key column, PK + +EXPLAIN FORMAT=TREE SELECT a, id, COUNT(*) FROM t1 GROUP BY a, id; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t1 (rows=1) + + +SELECT a, id, COUNT(*) FROM t1 GROUP BY a, id; +a id COUNT(*) +2 1 1 +1 2 1 +1 3 1 +2 4 1 +3 5 1 +2 6 1 +3 7 1 +2 8 1 +1 9 1 +1 10 1 + +8. Verify COUNT(*) with GROUP BY on PK, non-key column + +EXPLAIN FORMAT=TREE SELECT id, a, COUNT(*) from t1 GROUP BY id, a; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t1 (rows=1) + + +SELECT id, a, COUNT(*) from t1 GROUP BY id, a; +id a COUNT(*) +1 2 1 +2 1 1 +3 1 1 +4 2 1 +5 3 1 +6 2 1 +7 3 1 +8 2 1 +9 1 1 +10 1 1 + +9. Verify COUNT(*) with GROUP BY on non-key column, vector column + +EXPLAIN FORMAT=TREE SELECT a, vector1, COUNT(*) FROM t1 GROUP BY a, vector1; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t1 (rows=1) + + +SELECT a, vector1, COUNT(*) FROM t1 GROUP BY a, vector1; +a vector1 COUNT(*) +2 [1, 2, 3] 1 +1 [2, 22, 31] 1 +1 [1, 2, 4] 1 +2 [2, 22, 33] 1 +3 [11, 22, 4] 1 +2 [7, 22, 33] 1 +3 [10, 20, 40] 1 +2 [20, 22, 41] 1 +1 [20, 10, 30] 1 +1 [25, 25, 41] 1 + +10. Verify COUNT(*) with GROUP BY on vecror column, non-key column + +EXPLAIN FORMAT=TREE SELECT vector1, a, COUNT(*) FROM t1 GROUP BY vector1, a; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t1 (rows=1) + + +SELECT vector1, a, COUNT(*) FROM t1 GROUP BY vector1, a; +vector1 a COUNT(*) +[1, 2, 3] 2 1 +[2, 22, 31] 1 1 +[1, 2, 4] 1 1 +[2, 22, 33] 2 1 +[11, 22, 4] 3 1 +[7, 22, 33] 2 1 +[10, 20, 40] 3 1 +[20, 22, 41] 2 1 +[20, 10, 30] 1 1 +[25, 25, 41] 1 1 + +11. Verify COUNT(*) with GROUP BY on vector column, PK + +EXPLAIN FORMAT=TREE SELECT vector1, id, COUNT(*) FROM t1 GROUP BY vector1, id; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t1 (rows=1) + + +SELECT vector1, id, COUNT(*) FROM t1 GROUP BY vector1, id; +vector1 id COUNT(*) +[1, 2, 3] 1 1 +[2, 22, 31] 2 1 +[1, 2, 4] 3 1 +[2, 22, 33] 4 1 +[11, 22, 4] 5 1 +[7, 22, 33] 6 1 +[10, 20, 40] 7 1 +[20, 22, 41] 8 1 +[20, 10, 30] 9 1 +[25, 25, 41] 10 1 + +12. Verify COUNT(*) with GROUP BY on PK, vector column + +EXPLAIN FORMAT=TREE SELECT id, vector1, COUNT(*) FROM t1 GROUP BY id, vector1; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t1 (rows=1) + + +SELECT id, vector1, COUNT(*) FROM t1 GROUP BY id, vector1; +id vector1 COUNT(*) +1 [1, 2, 3] 1 +2 [2, 22, 31] 1 +3 [1, 2, 4] 1 +4 [2, 22, 33] 1 +5 [11, 22, 4] 1 +6 [7, 22, 33] 1 +7 [10, 20, 40] 1 +8 [20, 22, 41] 1 +9 [20, 10, 30] 1 +10 [25, 25, 41] 1 + +13. Verify COUNT(*) with GROUP BY on vector column, PK, non-key column + +EXPLAIN FORMAT=TREE SELECT vector1, id, a, COUNT(*) FROM t1 GROUP BY vector1, id, a; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t1 (rows=1) + + +SELECT vector1, id, a, COUNT(*) FROM t1 GROUP BY vector1, id, a; +vector1 id a COUNT(*) +[1, 2, 3] 1 2 1 +[2, 22, 31] 2 1 1 +[1, 2, 4] 3 1 1 +[2, 22, 33] 4 2 1 +[11, 22, 4] 5 3 1 +[7, 22, 33] 6 2 1 +[10, 20, 40] 7 3 1 +[20, 22, 41] 8 2 1 +[20, 10, 30] 9 1 1 +[25, 25, 41] 10 1 1 + +14. Verify COUNT(*) with GROUP BY on PK, vector column, non-key column + +EXPLAIN FORMAT=TREE SELECT id, vector1, a, COUNT(*) FROM t1 GROUP BY id, vector1, a; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t1 (rows=1) + + +SELECT id, vector1, a, COUNT(*) FROM t1 GROUP BY id, vector1, a; +id vector1 a COUNT(*) +1 [1, 2, 3] 2 1 +2 [2, 22, 31] 1 1 +3 [1, 2, 4] 1 1 +4 [2, 22, 33] 2 1 +5 [11, 22, 4] 3 1 +6 [7, 22, 33] 2 1 +7 [10, 20, 40] 3 1 +8 [20, 22, 41] 2 1 +9 [20, 10, 30] 1 1 +10 [25, 25, 41] 1 1 +drop table t1; diff --git a/mysql-test/suite/fb_vectordb/t/vectordb_count.test b/mysql-test/suite/fb_vectordb/t/vectordb_count.test new file mode 100644 index 000000000000..7be64d02ab36 --- /dev/null +++ b/mysql-test/suite/fb_vectordb/t/vectordb_count.test @@ -0,0 +1,176 @@ + +CREATE TABLE t1 ( + id BIGINT NOT NULL PRIMARY KEY, + a int, + b int, + vector1 JSON, + INDEX vector_key_1(vector1) FB_VECTOR_INDEX_TYPE 'flat' fb_vector_dimension 3 +); + +insert into t1 values (1, 2, 2, '[1,2,3]'), (2, 1, 2, '[2,22,31]'); +insert into t1 values (3, 1, 1, '[1,2,4]'), (4, 2, 1, '[2,22,33]'); +insert into t1 values (5, 3, 1, '[11,22,4]'), (6, 2, 3, '[7,22,33]'); +insert into t1 values (7, 3, 2, '[10,20,40]'), (8, 2, 1, '[20,22,41]'); +insert into t1 values (9, 1, 1, '[20,10,30]'), (10, 1, 2, '[25,25,41]'); + +disable_warnings; + +--echo +--echo 1. Verify basic COUNT(*) on the table +--echo + +--replace_column 10 ROWS 11 FILTERED +explain select COUNT(*) from t1; + +--echo + +SELECT COUNT(*) from t1; + +--echo +--echo 2. Verify COUNT(*) with WHERE clause with REF +--echo + +--replace_column 10 ROWS 11 FILTERED +explain select COUNT(*) from t1 WHERE id = 2; + +--echo + +SELECT COUNT(*) from t1 WHERE id = 2; + +--echo +--echo 3. Verify COUNT(*) with WHERE clause with RANGE +--echo + +--replace_column 10 ROWS 11 FILTERED +explain select COUNT(*) from t1 WHERE id > 2 and id < 10; + +--echo + +SELECT COUNT(*) from t1 WHERE id = 2 > 2 and id < 10; + + +--echo +--echo 4. Verify COUNT(*) with WHERE clause - other columns +--echo + +--replace_column 10 ROWS 11 FILTERED +explain select COUNT(*) from t1 WHERE a > 1 and b < 3 and id > 4; + +--echo + +SELECT COUNT(*) from t1 WHERE a > 1 and b < 3 and id > 4; + +enable_warnings; + +--echo +--echo 5. Verify COUNT(*) with GROUP BY on non-key column +--echo + +--replace_regex /\(cost=[0-9. ]*/(/ +explain format=tree select a, COUNT(*) from t1 group by a; + +--echo + +SELECT a, COUNT(*) from t1 GROUP BY a; + +--echo +--echo 6. Verify COUNT(*) with GROUP BY on multiple non-key columns +--echo + +--replace_regex /\(cost=[0-9. ]*/(/ +explain format=tree select a, b, COUNT(*) from t1 group by a, b; + +--echo + +SELECT a, b, COUNT(*) FROM t1 GROUP BY a, b; + +--echo +--echo 7. Verify COUNT(*) with GROUP BY on non-key column, PK +--echo + +--replace_regex /\(cost=[0-9. ]*/(/ +EXPLAIN FORMAT=TREE SELECT a, id, COUNT(*) FROM t1 GROUP BY a, id; + +--echo + +SELECT a, id, COUNT(*) FROM t1 GROUP BY a, id; + +--echo +--echo 8. Verify COUNT(*) with GROUP BY on PK, non-key column +--echo + +--replace_regex /\(cost=[0-9. ]*/(/ +EXPLAIN FORMAT=TREE SELECT id, a, COUNT(*) from t1 GROUP BY id, a; + +--echo + +SELECT id, a, COUNT(*) from t1 GROUP BY id, a; + +--echo +--echo 9. Verify COUNT(*) with GROUP BY on non-key column, vector column +--echo + +--replace_regex /\(cost=[0-9. ]*/(/ +EXPLAIN FORMAT=TREE SELECT a, vector1, COUNT(*) FROM t1 GROUP BY a, vector1; + +--echo + +SELECT a, vector1, COUNT(*) FROM t1 GROUP BY a, vector1; + +--echo +--echo 10. Verify COUNT(*) with GROUP BY on vecror column, non-key column +--echo + +--replace_regex /\(cost=[0-9. ]*/(/ +EXPLAIN FORMAT=TREE SELECT vector1, a, COUNT(*) FROM t1 GROUP BY vector1, a; + +--echo + +SELECT vector1, a, COUNT(*) FROM t1 GROUP BY vector1, a; + +--echo +--echo 11. Verify COUNT(*) with GROUP BY on vector column, PK +--echo + +--replace_regex /\(cost=[0-9. ]*/(/ +EXPLAIN FORMAT=TREE SELECT vector1, id, COUNT(*) FROM t1 GROUP BY vector1, id; + +--echo + +SELECT vector1, id, COUNT(*) FROM t1 GROUP BY vector1, id; + +--echo +--echo 12. Verify COUNT(*) with GROUP BY on PK, vector column +--echo + +--replace_regex /\(cost=[0-9. ]*/(/ +EXPLAIN FORMAT=TREE SELECT id, vector1, COUNT(*) FROM t1 GROUP BY id, vector1; + +--echo + +SELECT id, vector1, COUNT(*) FROM t1 GROUP BY id, vector1; + +--echo +--echo 13. Verify COUNT(*) with GROUP BY on vector column, PK, non-key column +--echo + +--replace_regex /\(cost=[0-9. ]*/(/ +EXPLAIN FORMAT=TREE SELECT vector1, id, a, COUNT(*) FROM t1 GROUP BY vector1, id, a; + +--echo + +SELECT vector1, id, a, COUNT(*) FROM t1 GROUP BY vector1, id, a; + +--echo +--echo 14. Verify COUNT(*) with GROUP BY on PK, vector column, non-key column +--echo + +--replace_regex /\(cost=[0-9. ]*/(/ +EXPLAIN FORMAT=TREE SELECT id, vector1, a, COUNT(*) FROM t1 GROUP BY id, vector1, a; + +--echo + +SELECT id, vector1, a, COUNT(*) FROM t1 GROUP BY id, vector1, a; + + +drop table t1; diff --git a/sql/sql_optimizer.cc b/sql/sql_optimizer.cc index 8776c1aad8ee..b928375ce736 100644 --- a/sql/sql_optimizer.cc +++ b/sql/sql_optimizer.cc @@ -2008,6 +2008,15 @@ uint find_shortest_key(TABLE *table, const Key_map *usable_keys) { uint min_length = (uint)~0; for (uint nr = 0; nr < table->s->keys; nr++) { if (nr == usable_clustered_pk) continue; + + /* + Do not consider the vector index for table scans in situations + where a shorter key heuristic might select the vector index because + it is technically shorter in length than a clustered primary index + */ + if (table->key_info[nr].is_fb_vector_index()) + continue; + if (usable_keys->is_set(nr)) { /* Can not do full index scan on rtree index because it is not