-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathfulltext index demo.sql
55 lines (49 loc) · 1.74 KB
/
fulltext index demo.sql
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
drop table if exists dbo.fttest
go
create table dbo.fttest
(id int identity(1,1) not null constraint pk_fftest primary key
, text1 varchar(2000)
, dateinserted datetimeoffset(2) not null constraint df_fttest_dateinserted default (sysdatetimeoffset())
)
insert into dbo.fttest (Text1) values ( REPLICATE (CHAR((rand()*64)+64), FLOOR(RAND()*2000)))
go
insert into dbo.fttest (Text1)
select ( REPLICATE (CHAR((rand()*64)+64), FLOOR(RAND()*2000))) from fttest
go 14
select count(1) from fttest
GO
IF EXISTS ( SELECT * FROM sys.fulltext_catalogs WHERE name = N'ft_cat')
DROP FULLTEXT CATALOG ft_cat
GO
CREATE FULLTEXT CATALOG ft_cat
GO
CREATE FULLTEXT INDEX ON dbo.fttest (text1)
KEY INDEX pk_fftest
ON ft_cat
WITH (CHANGE_TRACKING = AUTO, STOPLIST = SYSTEM)
GO
--use fulltext index status.sql to observe. Wait for it to get caught up.
INSERT INTO dbo.fttest (Text1)
SELECT ( REPLICATE (CHAR((rand()*64)+64), FLOOR(RAND()*2000))) from fttest
GO --insert a ton of rows and get the fulltext catalog "behind"
DELETE FROM fttest where text1 = 'whatever';
GO
INSERT INTO dbo.fttest (Text1) OUTPUT inserted.dateinserted select 'whatever' --insert needle in haystack
GO
SELECT sysdatetimeoffset(), * from dbo.fttest t where text1 = 'whatever'
SELECT sysdatetimeoffset(), * from dbo.fttest t where CONTAINS (Text1, '"whatever"');
GO
WHILE NOT EXISTS (select text1 from dbo.fttest t where CONTAINS (Text1, '"whatever"'))
BEGIN
WAITFOR DELAY '00:00:01' --1s
print 'waiting 1s';
IF EXISTS (select text1 from dbo.fttest t where CONTAINS (Text1, '"whatever"'))
BEGIN
--Wait for haystack to show up in the FT index. Might be a while!!
select Found = sysdatetimeoffset(), * from dbo.fttest t where CONTAINS (Text1, '"whatever"');
BREAK;
END
ELSE
CONTINUE;
END
GO