zoukankan      html  css  js  c++  java
  • 索引与like优化

    未建索引

    mysql> alter table modulestatus drop index imei;
    Query OK, 457922 rows affected (4.29 sec)
    Records: 457922 Duplicates: 0 Warnings: 0

    mysql> SELECT count(*) from modulestatus where imei like '1%';
    +----------+
    | count(*) |
    +----------+
    | 270 |
    +----------+
    1 row in set (0.53 sec)

    mysql> SELECT count(*) from modulestatus where imei like '%1';
    +----------+
    | count(*) |
    +----------+
    | 35499 |
    +----------+
    1 row in set (0.66 sec)

    mysql> SELECT count(*) from modulestatus where imei like '1%' and '9%';
    +----------+
    | count(*) |
    +----------+
    | 270 |
    +----------+
    1 row in set, 1 warning (0.49 sec)

    建立索引

    mysql> alter table modulestatus add index (imei);
    Query OK, 457922 rows affected (31.67 sec)
    Records: 457922 Duplicates: 0 Warnings: 0

    mysql> SELECT count(*) from modulestatus where imei like '1%';
    +----------+
    | count(*) |
    +----------+
    | 270 |
    +----------+
    1 row in set (0.04 sec)

    mysql> SELECT count(*) from modulestatus where imei like '%1';
    +----------+
    | count(*) |
    +----------+
    | 35499 |
    +----------+
    1 row in set (0.80 sec)

    mysql> SELECT count(*) from modulestatus where imei like '1%' and '9%';
    +----------+
    | count(*) |
    +----------+
    | 270 |
    +----------+
    1 row in set, 1 warning (0.00 sec)

    结论

    1、like %keyword:索引失效,使用全表扫描。但可以通过翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全表扫描。

    2、like keyword%:索引有效。

    3、like %keyword% 索引失效,也无法使用反向索引。

    未建索引

    mysql> SELECT count(*) from modulestatus where imei like '%1%';
    +----------+
    | count(*) |
    +----------+
    | 326019 |
    +----------+
    1 row in set (0.73 sec)

    mysql> SELECT count(*) from modulestatus where locate('1',imei);
    +----------+
    | count(*) |
    +----------+
    | 326019 |
    +----------+
    1 row in set (0.76 sec)

    建立索引

    mysql> alter table modulestatus add index (imei);
    Query OK, 457922 rows affected (11.06 sec)
    Records: 457922 Duplicates: 0 Warnings: 0

    mysql> SELECT count(*) from modulestatus where imei like '%1%';
    +----------+
    | count(*) |
    +----------+
    | 326019 |
    +----------+
    1 row in set (0.93 sec)

    mysql> SELECT count(*) from modulestatus where locate('1',imei);
    +----------+
    | count(*) |
    +----------+
    | 326019 |
    +----------+
    1 row in set (0.95 sec)

    结论

    LOCATE(str,colum)函数,可以代替column like '%str%',但效率并没有明显的差别。

    建立索引后,都会减慢like '%str%' 与locate(str,colum)函数查询的速度。

    未建立索引

    mysql> SELECT count(*) from modulestatus where imei like '%1%';
    +----------+
    | count(*) |
    +----------+
    | 326019 |
    +----------+
    1 row in set (0.73 sec)

    mysql> SELECT count(*) from modulestatus where instr(imei,'1');
    +----------+
    | count(*) |
    +----------+
    | 326019 |
    +----------+
    1 row in set (0.79 sec)

    建立索引

    mysql> SELECT count(*) from modulestatus where imei like '%1%';
    +----------+
    | count(*) |
    +----------+
    | 326019 |
    +----------+
    1 row in set (1.00 sec)

    mysql> SELECT count(*) from modulestatus where instr(imei,'1');
    +----------+
    | count(*) |
    +----------+
    | 326019 |
    +----------+
    1 row in set (0.84 sec)

    结论

    instr(colum,str)函数,可以代替column like '%str%',但效率并没有明显的差别。

    建立索引后,都会减慢like '%str%' 与instr(colum,str)函数查询的速度

    未建立索引

    mysql> SELECT count(*) from modulestatus where imei like '%1';
    +----------+
    | count(*) |
    +----------+
    | 35499 |
    +----------+
    1 row in set (0.90 sec)

    mysql> SELECT count(*) from modulestatus where reverse(imei) like reverse('%1');

    +----------+
    | count(*) |
    +----------+
    | 35499 |
    +----------+
    1 row in set (0.70 sec)

    建立索引

    mysql> alter table modulestatus add index (imei);
    Query OK, 457922 rows affected (10.04 sec)
    Records: 457922 Duplicates: 0 Warnings: 0

    mysql> SELECT count(*) from modulestatus where imei like '%1';
    +----------+
    | count(*) |
    +----------+
    | 35499 |
    +----------+
    1 row in set (1.06 sec)

    mysql> SELECT count(*) from modulestatus where reverse(imei) like reverse('%1')

    +----------+
    | count(*) |
    +----------+
    | 35499 |
    +----------+
    1 row in set (0.84 sec)

    结论

    1、建立索引,会降低like '%str'和reverse(column) like reverse('%str′)的查询速度。

    2、不管是建立索引,还是没有建立索引,reverse(column) like reverse('%str′)都要比column like '%str'要快。

    注:在执行column like '%str'的时候,执行计划显示,消耗值,io值,cpu值均非常大,原因是like后面前模糊查询导致索引失效,进行全表扫描。

    使用翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全扫描。有效降低消耗值,io值,cpu值这三个指标,尤其是io值的降低。

  • 相关阅读:
    HDU 1556 Color the ball【树状数组】
    HDU 3015 Disharmony Trees 【 树状数组 】
    POJ 1990 MooFest【 树状数组 】
    codeforces 493 C Vasya and Basketball
    12、Decorator 装饰器 模式 装饰起来美美哒 结构型设计模式
    11、Composite 组合模式 容器与内容的一致性(抽象化) 结构型设计模式
    10、Strategy 策略模式 整体地替换算法 行为型模式
    9、Bridge 桥梁模式 将类的功能层次结构与实现层结构分离 结构型设计模式
    读源码从简单的集合类之ArrayList源码分析。正确认识ArrayList
    8、Builder 建造者模式 组装复杂的实例 创造型模式
  • 原文地址:https://www.cnblogs.com/shixm/p/5496535.html
Copyright © 2011-2022 走看看