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 imqbrokerd 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, the following expression:
color IN (’red’, ’green’, ’white’)
is much more efficient than this expression
color = ’red’ OR color = ’green’ OR color = ’white’
especially if the above expression usually evaluates to false.
Use BETWEEN instead of multiple integer comparisons. For example:
size BETWEEN 6 AND 10
is generally more efficient than
size >= 6 AND size <= 10
especially if the above expression usually evaluates to true.
Order the selector expression so that Message Queue can determine its evaluation as soon as possible. (Evaluation proceeds from left to right.) This 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’d want the order of an OR expression to be:
size > 6 OR color = ’red’
If you are using AND:
color = ’red’ AND size > 6