New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve performance of select() and addSelect() #17344
Conversation
@samdark Sorry for being a little trigger happy on this PR, but it’s good to go now :) |
Merged. Thank you! |
I've tested on some projects I have without issues. |
Awesome! The only change we had to make in Craft was due to an craftcms/cms@1b26171#diff-bc0e9f065f7ab3360d875013c322bb79L2147 Doubt many people are doing things like that with normal queries. Probably worth mentioning in the upgrade notes that |
We just profiled a 25.8s request with Blackfire, and found that 11.5s / 44.5% was spent on
yii\db\Query::getUniqueColumns()
(called byselect()
andaddSelect()
), and its callee,getUnaliasedColumnsFromSelect()
.View on Blackfire ➡️
We rewrote
select()
andaddSelect()
to be more efficient, avoidinggetUniqueColumns()
altogether and ensuring uniqueness by normalizing SELECT columns similar to the wayorderBy()
/normalizeOrderBy()
works. Now aliases are always normalized into the['alias' => 'column']
syntax, and even un-aliased column names (excluding expressions, sub-selects, and strings with parentheses) are aliased to themselves (['column' => 'column']
).The result:
select()
isn’t even showing up on Blackfire’s radar anymore, andaddSelect()
is only taking up 663ms (17.3x faster), bringing the request time down to 14.1s (1.8x faster).View on Blackfire ➡️
We hope that is a performance improvement worth considering :)