SQLProfilePlugin.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET')) {
  20. exit(1);
  21. }
  22. /**
  23. * Check DB queries for filesorts and such and log em.
  24. *
  25. * @package SQLProfilePlugin
  26. * @maintainer Brion Vibber <brion@status.net>
  27. */
  28. class SQLProfilePlugin extends Plugin
  29. {
  30. private $recursionGuard = false;
  31. function onPluginVersion(array &$versions)
  32. {
  33. $versions[] = array('name' => 'SQLProfile',
  34. 'version' => GNUSOCIAL_VERSION,
  35. 'author' => 'Brion Vibber',
  36. 'homepage' => 'http://status.net/wiki/Plugin:SQLProfile',
  37. 'rawdescription' =>
  38. // TRANS: Plugin description.
  39. _m('Debug tool to watch for poorly indexed DB queries.'));
  40. return true;
  41. }
  42. function onStartDBQuery($obj, $query, &$result)
  43. {
  44. if (!$this->recursionGuard && preg_match('/\bselect\b/i', $query)) {
  45. $this->recursionGuard = true;
  46. $xobj = clone($obj);
  47. $explain = $xobj->query('EXPLAIN ' . $query);
  48. $this->recursionGuard = false;
  49. while ($xobj->fetch()) {
  50. $extra = $xobj->Extra;
  51. $evil = (strpos($extra, 'Using filesort') !== false) ||
  52. (strpos($extra, 'Using temporary') !== false);
  53. if ($evil) {
  54. $xquery = $xobj->sanitizeQuery($query);
  55. common_log(LOG_DEBUG, "$extra | $xquery");
  56. }
  57. }
  58. }
  59. return true;
  60. }
  61. }