减少mysql连接大小的步骤是什么?

时间:2011-08-23 11:32:37

标签: mysql

问题是例如: table1(id,name)=> 100行& table2(id,name,ref_id)=> 2000& table3 ...

联接在(table1.id = table2.ref_id)

上完成

我实际上只想要table2中的100行参与,因为它们需要一行而不是2000. table1中的100行是动态的&在运行时,它们与table2&结果是100行,但当2000行参与时,它们超过了连接大小&抛出错误。

其他方法是从table1&获取ID。然后使用它通过使用IN子句&获取table2中匹配的100行。这种出路并不好,因为ids不会太少。

因此,来自table1&的100行2000行table2参与笛卡尔积&它们会增加连接大小。

我可以通过SET SQL_BIG_SELECTS =1;来解决这个问题但是我想优化它。

如何通过索引或其他优化来解决这个问题。

信息:

表wp_postmeta

CREATE TABLE `wp_postmeta` (
  `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `post_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `meta_key` varchar(255) DEFAULT NULL,
  `meta_value` longtext,
  PRIMARY KEY (`meta_id`),
  KEY `post_id` (`post_id`),
  KEY `meta_key` (`meta_key`)
) ENGINE=MyISAM AUTO_INCREMENT=74135 DEFAULT CHARSET=utf8;

表wp_posts

CREATE TABLE `wp_posts` (
  `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `post_author` bigint(20) unsigned NOT NULL DEFAULT '0',
  `post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_content` longtext NOT NULL,
  `post_title` text NOT NULL,
  `post_excerpt` text NOT NULL,
  `post_status` varchar(20) NOT NULL DEFAULT 'publish',
  `comment_status` varchar(20) NOT NULL DEFAULT 'open',
  `ping_status` varchar(20) NOT NULL DEFAULT 'open',
  `post_password` varchar(20) NOT NULL DEFAULT '',
  `post_name` varchar(200) NOT NULL DEFAULT '',
  `to_ping` text NOT NULL,
  `pinged` text NOT NULL,
  `post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_content_filtered` text NOT NULL,
  `post_parent` bigint(20) unsigned NOT NULL DEFAULT '0',
  `guid` varchar(255) NOT NULL DEFAULT '',
  `menu_order` int(11) NOT NULL DEFAULT '0',
  `post_type` varchar(20) NOT NULL DEFAULT 'post',
  `post_mime_type` varchar(100) NOT NULL DEFAULT '',
  `comment_count` bigint(20) NOT NULL DEFAULT '0',
  PRIMARY KEY (`ID`),
  KEY `post_name` (`post_name`),
  KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
  KEY `post_parent` (`post_parent`),
  KEY `post_author` (`post_author`)
) ENGINE=MyISAM AUTO_INCREMENT=8526 DEFAULT CHARSET=utf8;

表wp_term_relationships

CREATE TABLE `wp_term_relationships` (
  `object_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `term_taxonomy_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `term_order` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`object_id`,`term_taxonomy_id`),
  KEY `term_taxonomy_id` (`term_taxonomy_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

表wp_term_taxonomy

CREATE TABLE `wp_term_taxonomy` (
  `term_taxonomy_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `term_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `taxonomy` varchar(32) NOT NULL DEFAULT '',
  `description` longtext NOT NULL,
  `parent` bigint(20) unsigned NOT NULL DEFAULT '0',
  `count` bigint(20) NOT NULL DEFAULT '0',
  PRIMARY KEY (`term_taxonomy_id`),
  UNIQUE KEY `term_id_taxonomy` (`term_id`,`taxonomy`),
  KEY `taxonomy` (`taxonomy`)
) ENGINE=MyISAM AUTO_INCREMENT=313 DEFAULT CHARSET=utf8;

表wp_terms

CREATE TABLE `wp_terms` (
  `term_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL DEFAULT '',
  `slug` varchar(200) NOT NULL DEFAULT '',
  `term_group` bigint(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`term_id`),
  UNIQUE KEY `slug` (`slug`),
  KEY `name` (`name`)
) ENGINE=MyISAM AUTO_INCREMENT=310 DEFAULT CHARSET=utf8;

QUERY:

SELECT products.*,
       prod_meta_key,
       prod_meta_value,
       GROUP_CONCAT( prod_categories.name ) AS category
  FROM ( SELECT `id`, post_title, post_content,
                post_excerpt, post_status, post_parent
           FROM wp_posts as products
          WHERE post_status IN  ('publish', 'draft', 'pending')
            AND post_type    = 'wpsc-product'
          ORDER BY products.id desc  
          LIMIT 0,100) AS products
  JOIN (SELECT id
          FROM wp_posts
         WHERE post_status in ('publish','draft')) AS products_parents 
    ON (if(products.post_parent != 0,products.post_parent,products.id) = products_parents.id) 
  LEFT JOIN
      (SELECT pm.post_id,
              GROUP_CONCAT(meta_key order by meta_id SEPARATOR '#') AS prod_meta_key,
              GROUP_CONCAT(meta_value order by meta_id SEPARATOR '#') AS prod_meta_value
         FROM `wp_postmeta` pm
        WHERE meta_key IN ('_wpsc_price', '_wpsc_special_price', '_wpsc_sku', '_wpsc_stock')
        GROUP BY post_id) AS products_meta ON products_meta.post_id = products.id
  LEFT JOIN 
 (SELECT wt.name, wtr.object_id
    FROM wp_term_relationships AS wtr  
    JOIN wp_term_taxonomy AS wtt
          ON (wtr.term_taxonomy_id = wtt.term_taxonomy_id AND taxonomy = 'wpsc_product_category')
    JOIN wp_terms AS wt ON (wtt.term_id = wt.term_id)
  ) AS prod_categories 
  ON (products.id = prod_categories.object_id)

Error: SELECT将检查多于MAX_JOIN_SIZE行;检查你的WHERE并使用SET SQL_BIG_SELECTS = 1或SET SQL_MAX_JOIN_SIZE =#如果SELECT没问题

ExPLAIN O/P with SQL_BIG_SELECTS =1 else it gives err: ![解释输出图像] [1] [1]:http://i.stack.imgur.com/DT7Tn.png

0 个答案:

没有答案