Create an index for the message type:
CREATE INDEX IX_Messages_MessageType ON Messages (MessageType)
Then, to get a list of unique message types , you run:
SELECT DISTINCT MessageType FROM Messages ORDER BY MessageType
Because the index is physically sorted in MessageType order, SQL Server can very quickly and efficiently scan the index by selecting a list of unique message types.
This works well - this is what SQL Server does well.
Admittedly, you can save some space by specifying a message type table. And if you show only a few messages at a time: then finding a bookmark , as it joins the MessageTypes table, will not be a problem. But if you show hundreds or thousands of messages at the same time, then connecting to MessageTypes can become quite expensive and unnecessary, and it will be faster to have a MessageType stored with the message.
But I would not have the problem of creating an index in the MessageType column and selecting distinct . SQL Server loves such things. But if you find that this is the real load on your server, as soon as you receive dozens of hits per second, then follow another suggestion and cache them in memory.
My personal solution would be:
- create index
- select separate
and if I still had problems
- a cache in memory that expires after 30 seconds.
Regarding the normalized / denormalized problem. Normalization saves space at the cost of a processor when connections are constantly being made. But the logical point of refusing copying is to avoid data duplication, which can lead to inconsistent data.
Are you planning to change the text of the message type, which, if you saved the messages, you would need to update all the lines?
Or is there something that can be said that during the message the message type was “Client response was requested”?