Domanda

Given a source table with an column containing arbitrary text, which contains alphabetic hashtags (#example, #alsoAnExample, #this-is-not-an-example), what is required to create an Indexed View in SQL server which

Hashtag regex definition:

/\B#\w*[a-zA-Z]+\w*/

Desired view example output:

PostId     |    Tag
   1           example
   1           alsoAnExample
   2           yetanothertag

This would remove the application tier from the responsibility to create entries for a regular table upon insert, and allow for data entry from other sources without knowledge of the tagging.

If this is possible, can it be done in such as way that inserts (the Posts table is insert heavy) will not cause a significant burden as the View Index will need to frequently updated.

È stato utile?

Soluzione

Because you can have multiple hash tags per row you obviously can't just use a computed column index. Instead, what you conceptually want to have is an indexed view of the base table cross-applied with a table-valued function that computes the hash tags. I would love to have that in SQL Server, but alas indexed views are extremely limited in what they support. Just forget them for your use case (and look up the restrictions in books online to understand why).

Instead, I recommend that you create a separate table to hold the hash tags (of the form (PostId INT PRIMARY KEY, Tag NVARCHAR(400) PRIMARY KEY, ... or similar). Maintain that table in the application tier, or using triggers.

You can also maintain the table in a delayed way so that you can do batch updates and do them in the background. That of course means that the app has to tolerate stale data, and it means more development work.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top