Skip to content
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

Merged
merged 17 commits into from Jun 11, 2019

Conversation

brandonkelly
Copy link
Contributor

@brandonkelly brandonkelly commented Jun 6, 2019

We just profiled a 25.8s request with Blackfire, and found that 11.5s / 44.5% was spent on yii\db\Query::getUniqueColumns() (called by select() and addSelect()), and its callee, getUnaliasedColumnsFromSelect().

Screen Shot 2019-06-06 at 3 21 25 PM

View on Blackfire ➡️

We rewrote select() and addSelect() to be more efficient, avoiding getUniqueColumns() altogether and ensuring uniqueness by normalizing SELECT columns similar to the way orderBy() / 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, and addSelect() is only taking up 663ms (17.3x faster), bringing the request time down to 14.1s (1.8x faster).

Screen Shot 2019-06-06 at 3 53 03 PM

View on Blackfire ➡️

We hope that is a performance improvement worth considering :)

Q A
Is bugfix? no
New feature? no
Breaks BC? no
Tests pass? yes

@brandonkelly brandonkelly changed the title Select boost Improve performance of select() and addSelect() Jun 6, 2019
@samdark samdark added the status:under development Someone is working on a pull request. label Jun 7, 2019
@brandonkelly
Copy link
Contributor Author

@samdark Sorry for being a little trigger happy on this PR, but it’s good to go now :)

@samdark samdark added status:code review The pull request needs review. and removed status:under development Someone is working on a pull request. labels Jun 7, 2019
@samdark samdark added this to the 2.0.21 milestone Jun 7, 2019
@samdark samdark added status:ready for merge The pull request is OK to be merged. and removed status:code review The pull request needs review. labels Jun 7, 2019
brandonkelly added a commit to craftcms/cms that referenced this pull request Jun 8, 2019
@samdark samdark merged commit 54b3d6d into yiisoft:master Jun 11, 2019
@samdark
Copy link
Member

samdark commented Jun 11, 2019

Merged. Thank you!

@samdark
Copy link
Member

samdark commented Jun 11, 2019

I've tested on some projects I have without issues.

@brandonkelly
Copy link
Contributor Author

brandonkelly commented Jun 11, 2019

Awesome! The only change we had to make in Craft was due to an array_splice() call on $query->select, as the key that was passed in (retrieved from array_search()) was no longer numeric.

craftcms/cms@1b26171#diff-bc0e9f065f7ab3360d875013c322bb79L2147

Doubt many people are doing things like that with normal queries. Probably worth mentioning in the upgrade notes that yii\db\Query::select() and addSelect() now normalize the columns when saving them to $this->select, so code that works directly with that property may need to be modified.

@brandonkelly brandonkelly deleted the select-boost branch June 11, 2019 14:13
samdark added a commit that referenced this pull request Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for merge The pull request is OK to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants