| 1 | # Building Scalar Index |
| 2 | |
| 3 | Similar to many SQL databases, LanceDB supports several types of Scalar indices to accelerate search |
| 4 | over scalar columns. |
| 5 | |
| 6 | - `BTREE`: The most common type is BTREE. This index is inspired by the btree data structure |
| 7 | although only the first few layers of the btree are cached in memory. |
| 8 | It will perform well on columns with a large number of unique values and few rows per value. |
| 9 | - `BITMAP`: this index stores a bitmap for each unique value in the column. |
| 10 | This index is useful for columns with a finite number of unique values and many rows per value. |
| 11 | For example, columns that represent "categories", "labels", or "tags" |
| 12 | - `LABEL_LIST`: a special index that is used to index list columns whose values have a finite set of possibilities. |
| 13 | For example, a column that contains lists of tags (e.g. `["tag1", "tag2", "tag3"]`) can be indexed with a `LABEL_LIST` index. |
| 14 | |
| 15 | | Data Type | Filter | Index Type | |
| 16 | | --------------------------------------------------------------- | ----------------------------------------- | ------------ | |
| 17 | | Numeric, String, Temporal | `<`, `=`, `>`, `in`, `between`, `is null` | `BTREE` | |
| 18 | | Boolean, numbers or strings with fewer than 1,000 unique values | `<`, `=`, `>`, `in`, `between`, `is null` | `BITMAP` | |
| 19 | | List of low cardinality of numbers or strings | `array_has_any`, `array_has_all` | `LABEL_LIST` | |
| 20 | |
| 21 | === "Python" |
| 22 | |
| 23 | ```python |
| 24 | import lancedb |
| 25 | books = [ |
| 26 | {"book_id": 1, "publisher": "plenty of books", "tags": ["fantasy", "adventure"]}, |
| 27 | {"book_id": 2, "publisher": "book town", "tags": ["non-fiction"]}, |
| 28 | {"book_id": 3, "publisher": "oreilly", "tags": ["textbook"]} |
| 29 | ] |
| 30 | |
| 31 | db = lancedb.connect("./db") |
| 32 | table = db.create_table("books", books) |
| 33 | table.create_scalar_index("book_id") # BTree by default |
| 34 | table.create_scalar_index("publisher", index_type="BITMAP") |
| 35 | ``` |
| 36 | |
| 37 | === "Typescript" |
| 38 | |
| 39 | === "@lancedb/lancedb" |
| 40 | |
| 41 | ```js |
| 42 | const db = await lancedb.connect("data"); |
| 43 | const tbl = await db.openTable("my_vectors"); |
| 44 | |
| 45 | await tbl.create_index("book_id"); |
| 46 | await tlb.create_index("publisher", { config: lancedb.Index.bitmap() }) |
| 47 | ``` |
| 48 | |
| 49 | For example, the following scan will be faster if the column `my_col` has a scalar index: |
| 50 | |
| 51 | === "Python" |
| 52 | |
| 53 | ```python |
| 54 | import lancedb |
| 55 | |
| 56 | table = db.open_table("books") |
| 57 | my_df = table.search().where("book_id = 2").to_pandas() |
| 58 | ``` |
| 59 | |
| 60 | === "Typescript" |
| 61 | |
| 62 | === "@lancedb/lancedb" |
| 63 | |
| 64 | ```js |
| 65 | const db = await lancedb.connect("data"); |
| 66 | const tbl = await db.openTable("books"); |
| 67 | |
| 68 | await tbl |
| 69 | .query() |
| 70 | .where("book_id = 2") |
| 71 | .limit(10) |
| 72 | .toArray(); |
| 73 | ``` |
| 74 | |
| 75 | Scalar indices can also speed up scans containing a vector search or full text search, and a prefilter: |
| 76 | |
| 77 | === "Python" |
| 78 | |
| 79 | ```python |
| 80 | import lancedb |
| 81 | |
| 82 | data = [ |
| 83 | {"book_id": 1, "vector": [1, 2]}, |
| 84 | {"book_id": 2, "vector": [3, 4]}, |
| 85 | {"book_id": 3, "vector": [5, 6]} |
| 86 | ] |
| 87 | table = db.create_table("book_with_embeddings", data) |
| 88 | |
| 89 | ( |
| 90 | table.search([1, 2]) |
| 91 | .where("book_id != 3", prefilter=True) |
| 92 | .to_pandas() |
| 93 | ) |
| 94 | ``` |
| 95 | |
| 96 | === "Typescript" |
| 97 | |
| 98 | === "@lancedb/lancedb" |
| 99 | |
| 100 | ```js |
| 101 | const db = await lancedb.connect("data/lance"); |
| 102 | const tbl = await db.openTable("book_with_embeddings"); |
| 103 | |
| 104 | await tbl.search(Array(1536).fill(1.2)) |
| 105 | .where("book_id != 3") // prefilter is default behavior. |
| 106 | .limit(10) |
| 107 | .toArray(); |
| 108 | ``` |