Does the order of field constraints matter in the performance of a select query?
Short answer: Yes, put the most restrictive constraint first.
Although expressions in q are usually evaluated from right to left, there is one exception: the constraints in a where clause are evaluated from left to right. Consider the following example:
q)t: ([] x: `a`b`c; y: 1 2 3)
q)select from t where x < `c, y > 1
x y
---
b 2
q)
The comma separating x < `c from y > 1 is not the join operator; instead, it delimits the constraints of the where clause. If you need to use the join operator in a where clause, use parentheses:
q)select from t where x in (`a, `b), y > 1
x y
---
b 2
q)
Each constraint is evaluated in the usual order, i.e., right-to-left:
q)select from t where x < first -1 # `p`c, y > 1
x y
---
b 2
q)
Because constraints are evaluated from left to right, putting the most restrictive constraint first will speed up queries on large tables. On a date partitioned database, for example, the placement of a constraint on the virtual date column will make a marked performance difference:
select from large_table where date = 2011.02.25, name = `JOE
is significantly faster than
select from large_table where name = `JOE, date = 2011.02.25
The latter query will attempt to load the entire contents of table for rows matching name `JOE prior to narrowing the result set down to a single date. kdb does not provide behind-the-scenes optimization in cases like these.