zoukankan      html  css  js  c++  java
  • 建立created_time和item_name上的联合索引对比测试

    - 建立created_time和item_name字段的联合索引
    create index idx_sort on t_source(created_time,item_name,item_id);  
    analyze table t_source;
    (1)使用相关子查询
    truncate t_target;  (http://www.amjmh.com/v/BIBRGZ_558768/)
    insert into t_target  
    select distinct t1.* from t_source t1 where item_id in   
    (select min(item_id) from t_source t2 where t1.created_time=t2.created_time and t1.item_name=t2.item_name);
            本次用时19秒,查询计划如下:

    mysql> explain select distinct t1.* from t_source t1 where item_id in   
        -> (select min(item_id) from t_source t2 where t1.created_time=t2.created_time and t1.item_name=t2.item_name);  
    +----+--------------------+-------+------------+------+---------------+----------+---------+----------------------------------------+--------+----------+------------------------------+
    | id | select_type        | table | partitions | type | possible_keys | key      | key_len | ref                                    | rows   | filtered | Extra                        |
    +----+--------------------+-------+------------+------+---------------+----------+---------+----------------------------------------+--------+----------+------------------------------+
    |  1 | PRIMARY            | t1    | NULL       | ALL  | NULL          | NULL     | NULL    | NULL                                   | 997281 |   100.00 | Using where; Using temporary |
    |  2 | DEPENDENT SUBQUERY | t2    | NULL       | ref  | idx_sort      | idx_sort | 89      | test.t1.created_time,test.t1.item_name |      2 |   100.00 | Using index                  |
    +----+--------------------+-------+------------+------+---------------+----------+---------+----------------------------------------+--------+----------+------------------------------+
    2 rows in set, 3 warnings (0.00 sec)
    外层查询的t_source表是驱动表,需要扫描100万行。
    对于驱动表每行的item_id,通过idx_sort索引查询出两行数据。

  • 相关阅读:
    mysql 主从架构搭建
    tomcat+nginx 反向代理 动静分离
    ELK(Elasticsearch + Logstash + Kibana)安装部署对nginx的日志收集
    Kibana server is not ready yet出现的原因
    apache+tomcat+单机双实例+动静分离+负载均衡
    docker的基本安装和简单使用+Dockerfile常用指令
    LVS+keepalived+DR 负载均衡高可用
    md5sum摘要
    python爬虫
    python和数据库
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11311213.html
Copyright © 2011-2022 走看看