nested-set
nested-set copied to clipboard
Optimise the use case where the two conditions overlap in an =
There is a special use case where the depth and the start overlap and create SQL that looks like (a <= x) & (a >= x)
... this is equivalent to (a=x)
and as a result allows better use of the table indexes, specifically the (id, revision_id, left_pos, right_pos, depth)
.
Before optimisation:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | parent | const | PRIMARY,IDX_E1907B87BF3967501DFA7C8F5E183DDBDEADD74FAA31C69,IDX_E1907B875E183DDBDEADD74,IDX_E1907B87BDEADD74 | PRIMARY | 8 | const,const | 1 | |
1 | SIMPLE | child | range | IDX_E1907B875E183DDBDEADD74,IDX_E1907B87BDEADD74,IDX_E1907B87FAA31C69 | IDX_E1907B87BDEADD74 | 20 | NULL | 145780 | Using index condition; Using where; Using filesort |
After optimisation:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | parent | const | PRIMARY,IDX_E1907B87BF3967501DFA7C8F5E183DDBDEADD74FAA31C69,IDX_E1907B875E183DDBDEADD74,IDX_E1907B87BDEADD74 | PRIMARY | 8 | const,const | 1 | |
1 | SIMPLE | child | ref,filter | IDX_E1907B875E183DDBDEADD74,IDX_E1907B87BDEADD74,IDX_E1907B87FAA31C69 | IDX_E1907B87FAA31C69,IDX_E1907B87BDEADD74 | 4,4 | const | 12148 | Using index condition; Using where; Using filesort; Using rowid filter |
All tests passed on this, but there might be a slight flaw in the math. Putting in Draft until I can review.