The use of selectors can have a significant impact on the performance of your application. It’s difficult to put an exact cost on the expense of using selectors since it varies with the complexity of the selector expression, but the more you can do to eliminate or simplify selectors the better.
One way to eliminate (or simplify) selectors is to use multiple destinations to sort messages. This has the additional benefit of spreading the message load over more than one producer, which can improve the scalability of your application. For those cases when it is not possible to do that, here are some techniques that you can use to improve the performance of your application when using selectors:
Have consumers share selectors. As of version 3.5 of Message Queue, message consumers with identical selectors “share” that selector in the broker, which can significantly improve performance. So if there is a way to structure your application to have some selector sharing, consider doing so.
Use IN instead of multiple string comparisons. For example, expression number 1 is much more efficient than expression number 2, especially if expression 2 usually evaluates to false.
color IN (’red’, ’green’, ’white’) \\ Expression 1
color = ’red’ OR color = ’green’ OR color = ’white’ \\Expression 2
Use BETWEEN instead of multiple integer comparisons. For example, expression 1 is more efficient than expression 2, especially if expression 2 usually evaluates to true.
size BETWEEN 6 AND 10 \\Expression 1
size >= 6 AND size <= 10 \\Expression 2
Order the selector expression so that MQ can short circuit the evaluation. The short circuiting of selector evaluation was added in MQ 3.5 and can easily double or triple performance when using selectors depending on the complexity of the expression.
If you have two expressions joined by an OR, put the expression that is most likely to evaluate to TRUE first.
If you have two expressions joined by an AND, put the expression that is most likely to evaluate to FALSE first.
For example, if size is usually greater than 6, but color is rarely red you would want the order of an OR expression to be the following.
size > 6 OR color = ’red’
If you are using AND, use the following order.
color = ’red’ AND size > 6