-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdiskann-quickstart-azure-sql.sql
More file actions
115 lines (104 loc) · 3.43 KB
/
diskann-quickstart-azure-sql.sql
File metadata and controls
115 lines (104 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
-- Step 1: Create a sample table with a VECTOR(5) column
DROP TABLE IF EXISTS dbo.Articles;
CREATE TABLE dbo.Articles
(
id INT PRIMARY KEY,
title NVARCHAR(100),
content NVARCHAR(MAX),
embedding VECTOR(5)
);
-- Step 2: Insert sample data
INSERT INTO Articles (id, title, content, embedding)
VALUES
(1, 'Intro to AI', 'This article introduces AI concepts.', '[0.1, 0.2, 0.3, 0.4, 0.5]'),
(2, 'Deep Learning', 'Deep learning is a subset of ML.', '[0.2, 0.1, 0.4, 0.3, 0.6]'),
(3, 'Neural Networks', 'Neural networks are powerful models.', '[0.3, 0.3, 0.3, 0.5, 0.1]'),
(4, 'Machine Learning Basics', 'ML basics for beginners.', '[0.4, 0.5, 0.1, 0.7, 0.3]'),
(5, 'Advanced AI', 'Exploring advanced AI techniques.', '[0.5, 0.4, 0.1, 0.1, 0.2]'),
(6, 'AI in Healthcare', 'AI applications in healthcare.', '[0.6, 0.3, 0.4, 0.3, 0.2]'),
(7, 'AI Ethics', 'Ethical considerations in AI.', '[0.1, 0.9, 0.5, 0.4, 0.3]'),
(8, 'AI and Society', 'Impact of AI on society.', '[0.2, 0.3, 0.5, 0.5, 0.4]'),
(9, 'Future of AI', 'Predictions for the future of AI.', '[0.8, 0.4, 0.5, 0.1, 0.2]'),
(10, 'AI Innovations', 'Latest innovations in AI.', '[0.4, 0.7, 0.2, 0.3, 0.1]');
GO
-- Step 3: Create a vector index on the embedding column
CREATE VECTOR INDEX vec_idx ON Articles(embedding)
WITH (METRIC = 'Cosine', TYPE = 'DiskANN')
ON [PRIMARY];
GO
-- Step 4: Perform a vector similarity search
DECLARE @qv VECTOR(5) = (SELECT TOP(1) embedding FROM Articles WHERE id = 1);
SELECT
t.id,
t.title,
t.content,
s.distance
FROM
VECTOR_SEARCH(
TABLE = Articles AS t,
COLUMN = embedding,
SIMILAR_TO = @qv,
METRIC = 'Cosine',
TOP_N = 3
) AS s
ORDER BY s.distance, t.title;
GO
-- Step 5: View index details
SELECT index_id, [type], [type_desc], vector_index_type, distance_metric, build_parameters FROM sys.vector_indexes WHERE [name] = 'vec_idx';
GO
-- Step 6a: Data modification is disabled when DiskANN exist on a table
INSERT INTO Articles (id, title, content, embedding)
VALUES
(11, 'Vectors and Embeddings', 'Everything about vectors and embeddings.', '[0.1, 0.2, 0.3, 0.4, 0.6]');
GO
-- Step 6b: Allow index to go stale
ALTER DATABASE SCOPED CONFIGURATION
SET ALLOW_STALE_VECTOR_INDEX = ON
GO
-- Step 6c: Data modification is now works
INSERT INTO Articles (id, title, content, embedding)
VALUES
(11, 'Vectors and Embeddings', 'Everything about vectors and embeddings.', '[0.1, 0.2, 0.3, 0.4, 0.6]');
GO
-- Step 7: Perform a vector similarity search, new data not visible
DECLARE @qv VECTOR(5) = (SELECT TOP(1) embedding FROM Articles WHERE id = 1);
SELECT
t.id,
t.title,
t.content,
s.distance
FROM
VECTOR_SEARCH(
TABLE = Articles AS t,
COLUMN = embedding,
SIMILAR_TO = @qv,
METRIC = 'Cosine',
TOP_N = 3
) AS s
ORDER BY s.distance, t.title;
GO
-- Step 8: Re-Create a vector index on the embedding column
DROP INDEX vec_idx ON Articles;
CREATE VECTOR INDEX vec_idx ON Articles(embedding)
WITH (METRIC = 'Cosine', TYPE = 'DiskANN')
ON [PRIMARY];
GO
-- Step 9: Data now visible
DECLARE @qv VECTOR(5) = (SELECT TOP(1) embedding FROM Articles WHERE id = 1);
SELECT
t.id,
t.title,
t.content,
s.distance
FROM
VECTOR_SEARCH(
TABLE = Articles AS t,
COLUMN = embedding,
SIMILAR_TO = @qv,
METRIC = 'Cosine',
TOP_N = 3
) AS s
ORDER BY s.distance;
GO
-- Step 6: Clean up by dropping the table
DROP INDEX vec_idx ON Articles;