MDL-42882 upgrade: improved SQL query

Kudos to Tim Hunt who came up with this, on mysql with 4 milion
records (and a fast SSD) the performance difference is:

Original: 36.83 sec
New query: 8.63 sec
Merge branch 'MDL-41647_26' of git://github.com/apsdehal/moodle into MOODLE_26_STABLE
Merge branch 'm26_MDL-37714' of git://github.com/greg-or/moodle into MOODLE_26_STABLE
Merge branch 'wip-MDL-39942-MOODLE_26_STABLE' of https://github.com/kwiliarty/moodle into MOODLE_26_STABLE
Merge branch 'MDL-43884-26' of git://github.com/ankitagarwal/moodle into MOODLE_26_STABLE
MDL-40410 remove ampersands required for PHP strict standards lib/tablelib.php
Merge branch 'MDL-43936_26' of git://github.com/timhunt/moodle into MOODLE_26_STABLE
Merge branch 'wip-MDL-33920-m26' of git://github.com/marinaglancy/moodle into MOODLE_26_STABLE
Merge branch 'MDL-43892_26' of git://github.com/timhunt/moodle into MOODLE_26_STABLE
Merge branch 'wip-MDL-43828-m26' of git://github.com/marinaglancy/moodle into MOODLE_26_STABLE
MDL-43821 completion: fix bug in section completion progress

Thanks to Shane Dye for the provided patch
Merge branch 'MDL-43885-26' of git://github.com/andrewnicols/moodle into MOODLE_26_STABLE
MDL-42456: add Mercurial vcs support to plugin_external_source
MDL-43911 Behat: Save dumps of the page content to disk on scenario failure
Merge branch 'MDL-43369-26' of git://github.com/jamiepratt/moodle into MOODLE_26_STABLE
MDL-43941 Course: Allow AJAX show/hide of activities in hidden and orphaned sections
MDL-43805 assign: Prevent behat fail on ci - editpdf is not available there

Ghostscript is not setup on the ci servers, so the editpdf feature will be automatically
disabled. This means that I cannot click on "Hide Annotate PDF".
MDL-43641 Javascript: Correct default dialogue centering
Merge branch 'wip-MDL-41384-m26' of git://github.com/marinaglancy/moodle into MOODLE_26_STABLE
Merge branch 'MDL-43911-26' of git://github.com/andrewnicols/moodle into MOODLE_26_STABLE