我的查询运行超过4秒,帮助我在这里获得不到1秒的结果

时间:2013-03-07 08:50:30

标签: mysql performance query-performance

  1. 我的查询运行时间超过4秒。
  2. 我希望结果不到1秒
  3. 帮助我。
  4. 查询:

    SELECT a.index_id,
           title
    FROM fb_indices a,
         fb_catalog_indices b FORCE INDEX (idx_1986)
    WHERE a.index_id = b.index_id
      AND type = 'international'
      AND title IS NOT NULL
    

    解释计划:

    mysql> explain select a.index_id,title from fb_indices a, fb_catalog_indices b force index (idx_1986) where a.index_id = b.index_id and type='international' and title is not NULL;
    +----+-------------+-------+------+-----------------+----------+---------+----------------+------+-------------+
    | id | select_type | table | type | possible_keys   | key      | key_len | ref            | rows | Extra       |
    +----+-------------+-------+------+-----------------+----------+---------+----------------+------+-------------+
    |  1 | SIMPLE      | a     | ref  | idx_836,idx_450 | idx_450  | 53      | const          |   79 | Using where |
    |  1 | SIMPLE      | b     | ref  | idx_1986        | idx_1986 | 103     | cms.a.index_id |  590 | Using where |
    +----+-------------+-------+------+-----------------+----------+---------+----------------+------+-------------+
    2 rows in set (0.00 sec)
    

    结构:

    mysql> show create table fb_indices\G
    *************************** 1. row ***************************
           Table: fb_indices
    Create Table: CREATE TABLE `fb_indices` (
      `row_mod` datetime DEFAULT NULL,
      `row_create` datetime DEFAULT NULL,
      `index_id` varchar(100) COLLATE latin1_bin DEFAULT NULL,
      `description` varchar(255) COLLATE latin1_bin DEFAULT NULL,
      `country_id` varchar(2) COLLATE latin1_bin DEFAULT NULL,
      `index_level` int(11) DEFAULT NULL,
      `type` varchar(50) COLLATE latin1_bin DEFAULT NULL,
      `approval_flag` int(11) DEFAULT NULL,
      `username` varchar(255) COLLATE latin1_bin DEFAULT NULL,
      `search_product_type` int(11) DEFAULT NULL,
      `search_color` int(11) DEFAULT NULL,
      `search_price` int(11) DEFAULT NULL,
      UNIQUE KEY `idx_836` (`index_id`),
      KEY `idx_450` (`type`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin
    1 row in set (0.00 sec)
    
    mysql> show create table fb_catalog_indices\G
    *************************** 1. row ***************************
           Table: fb_catalog_indices
    Create Table: CREATE TABLE `fb_catalog_indices` (
      `row_mod` datetime DEFAULT NULL,
      `row_create` datetime DEFAULT NULL,
      `index_id` varchar(100) COLLATE latin1_bin DEFAULT NULL,
      `website_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
      `title` varchar(100) COLLATE latin1_bin DEFAULT NULL,
      `subheading` longtext COLLATE latin1_bin,
      `product_ids` longtext COLLATE latin1_bin,
      `sub_index_ids` longtext COLLATE latin1_bin,
      `mapped_index_ids` longtext COLLATE latin1_bin,
      `username` varchar(100) COLLATE latin1_bin DEFAULT NULL,
      `content` longtext COLLATE latin1_bin,
      `meta_description` longtext COLLATE latin1_bin,
      `meta_tag_keyword` longtext COLLATE latin1_bin,
      `mapped_link_text` longtext COLLATE latin1_bin,
      `mapped_alt_tags` longtext COLLATE latin1_bin,
      `long_title` varchar(255) COLLATE latin1_bin DEFAULT NULL,
      `html_content` longtext COLLATE latin1_bin,
      `use_html_content` int(11) DEFAULT '0',
      `flash_size` longtext COLLATE latin1_bin,
      `custom_index_ids` longtext COLLATE latin1_bin,
      `html_header_1` longtext COLLATE latin1_bin,
      `html_header_2` longtext COLLATE latin1_bin,
      `search_product_type` int(11) DEFAULT NULL,
      `search_color` int(11) DEFAULT NULL,
      `search_price` int(11) DEFAULT NULL,
      `disable_rankings` int(11) DEFAULT NULL,
      `html_footer_1` longtext COLLATE latin1_bin,
      `dramashot_html` longtext COLLATE latin1_bin,
      `quickshop_image` longtext COLLATE latin1_bin,
      `sort_by_price_css` longtext COLLATE latin1_bin,
      `pagination_css` longtext COLLATE latin1_bin,
      `suppress_navigation` int(11) DEFAULT NULL,
      `suppress_quickshop_image` int(11) DEFAULT NULL,
      `mvt_control_script` longtext COLLATE latin1_bin,
      `mvt_tracking_script` longtext COLLATE latin1_bin,
      `enable_gbb_mouse` int(11) DEFAULT NULL,
      `enable_gbb_click` int(11) DEFAULT NULL,
      `last_modified_time` datetime DEFAULT NULL,
      `seo_footer_content` longtext COLLATE latin1_bin,
      `alternate_gfi` int(11) DEFAULT NULL,
      `zip_finder_html_content` longtext COLLATE latin1_bin,
      `disable_quick_view_widget` int(11) DEFAULT NULL,
      `indexpromo_title` varchar(255) COLLATE latin1_bin DEFAULT NULL,
      `indexpromo_style` longtext COLLATE latin1_bin,
      `background_color` text COLLATE latin1_bin,
      `sameday_text` text COLLATE latin1_bin,
      `productdesc_text` text COLLATE latin1_bin,
      `productprice_text` text COLLATE latin1_bin,
      `gbbcatalog_text` text COLLATE latin1_bin,
      `max_products_display` int(11) DEFAULT NULL,
      UNIQUE KEY `idx_1986` (`index_id`,`website_id`),
      KEY `idx_1055` (`website_id`),
      KEY `idx_836` (`index_id`),
      KEY `idx_1812` (`last_modified_time`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin
    1 row in set (0.00 sec)
    

2 个答案:

答案 0 :(得分:1)

创建以下索引:

CREATE INDEX idx_999 ON fb_indices (index_id, type);

之后,此查询与您的查询等效,并且应该快速运行:

SELECT a.index_id,
       b.title
FROM fb_indices a
JOIN fb_catalog_indices b ON (a.index_id = b.index_id)
WHERE a.type = 'international'
  AND b.title IS NOT NULL

提示:尽量不要强制使用索引 - 在您的情况下,它会受到伤害。而是创建一个合适的索引。

答案 1 :(得分:0)

SELECT a.index_id,
       title
FROM fb_indices a,
     (
        SELECT 
            index_id 
        FROM 
            fb_catalog_indices 
        WHERE  
            type = 'international'
     ) as b 
WHERE a.index_id = b.index_id
  AND title IS NOT NULL