Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force a refresh of a fulltext index within a transaction in mssql?

I am trying to figure out how to get the following code to return the row that it just inserted - a co-worker pointed out and suggested running ALTER FULLTEXT CATALOG uiris_production REBUILD but that cannot be run within a user transaction.

The reason this has to be in a transaction is that this is coming from a test framework where the test is wrapped in a transaction and then rolled back.

declare @search varchar(64)
set @login_test = 'foobar'
set @search = '"' + @login_test + '*"'

begin transaction
   insert into users(login) values (@login_test)
   select login from users where contains(login, @search)
commit
like image 240
Ransom Briggs Avatar asked Aug 23 '10 14:08

Ransom Briggs


People also ask

Do SQL indexes update automatically?

The SQL Server Database Engine automatically maintains indexes whenever insert, update, or delete operations are made to the underlying data. Over time these modifications can cause the information in the index to become scattered in the database (fragmented).

How do I defrag an index in SQL Server?

Rebuild and Reorganize Index using SQL Server Management Studio (SSMS) Find and expand the table in Object Explorer >> Open Indexes >> Right-click on the target index >> Rebuild or Reorganize.

How do I speed up index rebuild in SQL Server?

By changing the number of processors SQL Server can use in parallel, in other words the maximum degree of parallelism (MAXDOP), we can improve index rebuild performance. This option is by default set to zero instance-wide on SQL Server, it does not mean use zero processors.

Can we update index in SQL Server?

Using SQL Server Management StudioRight-click the index that you want to modify and then click Properties. In the Index Properties dialog box, make the desired changes. For example, you can add or remove a column from the index key, or change the setting of an index option.


1 Answers

First, make sure your full text index is set up for automatic change tracking. change_tracking_state should be 'A' for this query.

select t.name, fi.change_tracking_state 
    from sys.fulltext_indexes fi
        inner join sys.tables t
            on fi.object_id = t.object_id
    where t.name = 'users'

But, even with automatic change tracking, there is a certain degree of latency in updating fulltext. You may need to build a WAITFOR into your unit test to accommodate this.

like image 148
Joe Stefanelli Avatar answered Sep 20 '22 22:09

Joe Stefanelli