如何优化查询?

时间:2012-10-21 20:40:26

标签: mysql optimization join

我编写了一个查询来从WordPress数据库中提取报告。查询速度令人难以置信,所以我想优化它。我做了一些研究,我很难理解什么是最好的加速查询的方法(即使我写了,我几乎不明白我做了什么。)有人能指出我正确的方向来优化这个慢查询。

SELECT
wp1.meta_value as firstName,
wp2.meta_value as middleName,
wp3.meta_value as lastName,
wp5.meta_value as email2,
wp6.meta_value as address,
wp7.meta_value as city,
wp8.meta_value as state,
wp9.meta_value as zip,
wp10.meta_value as country,
wp11.meta_value as phone,
wp12.meta_value as dateOfBirth
FROM wp99_usermeta wp1
JOIN wp99_usermeta wp2 ON wp1.user_id = wp2.user_id and wp2.meta_key = 'middle_name'
JOIN wp99_usermeta wp3 ON wp1.user_id = wp3.user_id and wp3.meta_key = 'last_name'
JOIN wp99_usermeta wp5 ON wp1.user_id = wp5.user_id and wp5.meta_key = 'email2'
JOIN wp99_usermeta wp6 ON wp1.user_id = wp6.user_id and wp6.meta_key = 'street_address'
JOIN wp99_usermeta wp7 ON wp1.user_id = wp7.user_id and wp7.meta_key = 'city'
JOIN wp99_usermeta wp8 ON wp1.user_id = wp8.user_id and wp8.meta_key = 'state'
JOIN wp99_usermeta wp9 ON wp1.user_id = wp9.user_id and wp9.meta_key = 'zip'
JOIN wp99_usermeta wp10 ON wp1.user_id = wp10.user_id and wp10.meta_key = 'country'
JOIN wp99_usermeta wp11 ON wp1.user_id = wp11.user_id and wp11.meta_key = 'phone'
JOIN wp99_usermeta wp12 ON wp1.user_id = wp12.user_id and wp12.meta_key = 'date_of_birth'
WHERE wp1.meta_key = 'first_name' 

默认的wp_usermeta表格结构:

CREATE TABLE `wp_usermeta` (
  `umeta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `meta_key` varchar(255) DEFAULT NULL,
  `meta_value` longtext,
  PRIMARY KEY (`umeta_id`),
  KEY `user_id` (`user_id`),
  KEY `meta_key` (`meta_key`)
) ENGINE=***  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;

尝试ALTER TABLE建议。这在第5行给我一个错误

ALTER TABLE wp99_usermeta     添加索引meta_key__user_id       (meta_key,user_id);

SELECT
wp1.meta_value as firstName,
wp2.meta_value as middleName,
wp3.meta_value as lastName,
wp5.meta_value as email2,
wp6.meta_value as address,
wp7.meta_value as city,
wp8.meta_value as state,
wp9.meta_value as zip,
wp10.meta_value as country
FROM wp99_usermeta wp1
JOIN wp99_usermeta wp2 ON wp1.user_id = wp2.meta_key__user_id = 'middle_name'
JOIN wp99_usermeta wp3 ON wp1.user_id = wp3.meta_key__user_id = 'last_name'
JOIN wp99_usermeta wp5 ON wp1.user_id = wp5.meta_key__user_id = 'email2'
JOIN wp99_usermeta wp6 ON wp1.user_id = wp6.meta_key__user_id = 'street_address'
JOIN wp99_usermeta wp7 ON wp1.user_id = wp7.meta_key__user_id = 'city'
JOIN wp99_usermeta wp8 ON wp1.user_id = wp8.meta_key__user_id = 'state'
JOIN wp99_usermeta wp9 ON wp1.user_id = wp9.meta_key__user_id = 'zip'
JOIN wp99_usermeta wp10 ON wp1.user_id = wp10.meta_key__user_id = 'country'
WHERE wp1.meta_key = 'first_name' 

2 个答案:

答案 0 :(得分:1)

我会在(meta_key, user_id)上添加一个化合物:

ALTER TABLE wp99_usermeta
    ADD INDEX meta_key__user_id 
      (meta_key, user_id) ;

通过上面的添加,现有的(meta_key)索引是多余的,您可以删除它。但@biziclop指出了这种干扰插件和/或WordPress自我更新(数据库)程序的可能性。所以,您可以放弃它,也许可以在WordPress开发者论坛上询问有关添加/删除索引的影响/问题的问题。

答案 1 :(得分:1)

您可以使用GROUP BY尝试一些乐趣:

SELECT
  MAX( IF( meta_key = 'first_name', meta_value , NULL ) AS firstName,
  MAX( IF( meta_key = 'last_name',  meta_value , NULL ) AS lastName,
  ...
FROM wp99_usermeta
GROUP BY user_id

如果你必须过滤实体,你可以做类似的事情:

SELECT
  MAX( IF( b.meta_key = 'first_name', b.meta_value , NULL ) AS firstName,
  MAX( IF( b.meta_key = 'last_name',  b.meta_value , NULL ) AS lastName,
  ...
FROM wp99_usermeta a
JOIN wp99_usermeta b
WHERE a.meta_key = 'first_name'
  AND a.meta_value = 'Joe'
GROUP BY user_id