iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >SQL优化案例分享--联合索引
  • 518
分享到

SQL优化案例分享--联合索引

2024-04-02 19:04:59 518人浏览 独家记忆
摘要

下面这个sql如何优化:desc select count(*) as total from Art_Person a, Art_Works b where a.PersonCode=b

下面这个sql如何优化

desc select count(*) as total from Art_Person a, Art_Works b where a.PersonCode=b.PersonCode;

+----+-------------+-------+-------+---------------+------------+---------+---------------------+--------+-------------+

| id | select_type | table | type  | possible_keys | key        | key_len | ref                 | rows   | Extra       |

+----+-------------+-------+-------+---------------+------------+---------+---------------------+--------+-------------+

|  1 | SIMPLE      | b     | index | PersonCode    | PersonCode | 25      | NULL                | 166904 | Using index |

|  1 | SIMPLE      | a     | ref   | PersonCode    | PersonCode | 24      | newart.b.PersonCode |      1 | Using index |

+----+-------------+-------+-------+---------------+------------+---------+---------------------+--------+-------------+

2 rows in set (0.00 sec)


Mysql> show profile for query 2;

+----------------------+----------+

| Status               | Duration |

+----------------------+----------+

| starting             | 0.000149 |

| checking permissions | 0.000015 |

| checking permissions | 0.000015 |

| Opening tables       | 0.000049 |

| System lock          | 0.000032 |

| init                 | 0.000065 |

| optimizing           | 0.000032 |

| statistics           | 0.000053 |

| preparing            | 0.000039 |

| executing            | 0.000019 |

| Sending data         | 2.244108 |

| end                  | 0.000042 |

| query end            | 0.000008 |

| closing tables       | 0.000023 |

| freeing items        | 0.000038 |

| logging slow query   | 0.000007 |

| logging slow query   | 0.000008 |

| cleaning up          | 0.000008 |

+----------------------+----------+

18 rows in set (0.00 sec)


mysql> show create table Art_Works\G

*************************** 1. row ***************************

Table: Art_Works

Create Table: CREATE TABLE `Art_Works` (

`PID` int(11) NOT NULL AUTO_INCREMENT,

PRIMARY KEY (`PID`),

KEY `ViewCount` (`ViewCount`),

KEY `PersonCode` (`PersonCode`) USING BTREE,

KEY `GoodsStatus` (`GoodsStatus`) USING BTREE,

KEY `CreateTime` (`CreateTime`) USING BTREE,

KEY `RelWorkID` (`RelWorkID`) USING BTREE

) ENGINE=MyISAM AUTO_INCREMENT=210549 DEFAULT CHARSET=utf8


mysql> show create table Art_Person\G

*************************** 1. row ***************************

Table: Art_Person

Create Table: CREATE TABLE `Art_Person` (

`PID` int(11) NOT NULL AUTO_INCREMENT,

PRIMARY KEY (`PID`),

UNIQUE KEY `MemberID` (`MemberID`),

KEY `PersonCode` (`PersonCode`) USING BTREE

) ENGINE=MyISAM AUTO_INCREMENT=8699 DEFAULT CHARSET=utf8

1 row in set (0.00 sec)


解决办法(索引的问题):带着主键,改成联合索引。count() 的时候 带上 主键 就ok了 不然不会走的。其实这个索引就是为了小表驱动大表,只是大表的索引 对count()而言 没用。加上 主键 就可以了。

mysql> alter table Art_Person add index idx_PU(PersonCode,PID);带着主键,改成联合索引。

Query OK, 8666 rows affected (0.49 sec)

Records: 8666  Duplicates: 0  Warnings: 0


mysql> alter table Art_Works add index idx_PU(PersonCode,PID); 带着主键,改成联合索引。

Query OK, 166904 rows affected (6.02 sec)

Records: 166904  Duplicates: 0  Warnings: 0


mysql> desc  select sql_no_cache count(*) as total from Art_Works b,Art_Person a force index (PersonCode) where b.PersonCode=a.PersonCode;

+----+-------------+-------+-------+-------------------+------------+---------+---------------------+------+--------------------------+

| id | select_type | table | type  | possible_keys     | key        | key_len | ref                 | rows | Extra                    |

+----+-------------+-------+-------+-------------------+------------+---------+---------------------+------+--------------------------+

|  1 | SIMPLE      | a     | index | PersonCode        | PersonCode | 24      | NULL                | 8666 | Using index              |

|  1 | SIMPLE      | b     | ref   | PersonCode,idx_PU | idx_PU     | 25      | newart.a.PersonCode |    1 | Using where; Using index |

+----+-------------+-------+-------+-------------------+------------+---------+---------------------+------+--------------------------+

2 rows in set (0.00 sec)


下面是删除索引,看看count(1)这么走。

mysql> alter table Art_Person drop index idx_PU ;

Query OK, 8666 rows affected (0.45 sec)

Records: 8666  Duplicates: 0  Warnings: 0


mysql> alter table Art_Works drop index idx_PU ;

Query OK, 166904 rows affected (3.90 sec)

Records: 166904  Duplicates: 0  Warnings: 0


mysql>  select sql_no_cache count(1) as total from Art_Works b,Art_Person a force index (PersonCode) where b.PersonCode=a.PersonCode;

+--------+

| total  |

+--------+

| 166657 |

+--------+

1 row in set (2.38 sec)


mysql> alter table Art_Works add index idx_PU(PersonCode,PID);

Query OK, 166904 rows affected (4.32 sec)

Records: 166904  Duplicates: 0  Warnings: 0


mysql>  select sql_no_cache count(1) as total from Art_Works b,Art_Person a force index (PersonCode) where b.PersonCode=a.PersonCode;

+--------+

| total  |

+--------+

| 166657 |

+--------+

1 row in set (0.44 sec)


mysql> desc select sql_no_cache count(1) as total from Art_Works b,Art_Person a force index (PersonCode) where b.PersonCode=a.PersonCode;

+----+-------------+-------+-------+-------------------+------------+---------+---------------------+------+--------------------------+

| id | select_type | table | type  | possible_keys     | key        | key_len | ref                 | rows | Extra                    |

+----+-------------+-------+-------+-------------------+------------+---------+---------------------+------+--------------------------+

|  1 | SIMPLE      | a     | index | PersonCode        | PersonCode | 24      | NULL                | 8666 | Using index              |

|  1 | SIMPLE      | b     | ref   | PersonCode,idx_PU | idx_PU     | 25      | newart.a.PersonCode |    1 | Using where; Using index |

+----+-------------+-------+-------+-------------------+------------+---------+---------------------+------+--------------------------+

2 rows in set (0.00 sec)


下面是去掉大表的索引:把大表的索引去掉  count(PersonCode) 也没用,还是不走索引

mysql> alter table Art_Works drop index idx_PU ;

Query OK, 166904 rows affected (3.82 sec)

Records: 166904  Duplicates: 0  Warnings: 0



mysql> desc select sql_no_cache count(b.PersonCode) as total from Art_Works b,Art_Person a force index (PersonCode) where b.PersonCode=a.PersonCode;

+----+-------------+-------+-------+---------------+------------+---------+---------------------+--------+-------------+

| id | select_type | table | type  | possible_keys | key        | key_len | ref                 | rows   | Extra       |

+----+-------------+-------+-------+---------------+------------+---------+---------------------+--------+-------------+

|  1 | SIMPLE      | b     | index | PersonCode    | PersonCode | 25      | NULL                | 166904 | Using index |

|  1 | SIMPLE      | a     | ref   | PersonCode    | PersonCode | 24      | newart.b.PersonCode |     13 | Using index |

+----+-------------+-------+-------+---------------+------------+---------+---------------------+--------+-------------+

2 rows in set (0.00 sec)


mysql>  select sql_no_cache count(b.PersonCode) as total from Art_Works b,Art_Person a force index (PersonCode) where b.PersonCode=a.PersonCode;

+--------+

| total  |

+--------+

| 166657 |

+--------+

1 row in set (2.47 sec)


mysql> alter table Art_Works add index idx_PU(PersonCode,PID);

Query OK, 166904 rows affected (4.23 sec)

Records: 166904  Duplicates: 0  Warnings: 0


mysql>  select sql_no_cache count(b.PersonCode) as total from Art_Works b,Art_Person a force index (PersonCode) where b.PersonCode=a.PersonCode;

+--------+

| total  |

+--------+

| 166657 |

+--------+

1 row in set (0.44 sec)



=====================下面是线上实验结果========================================

mysql> desc select sql_no_cache count(*) as total from Art_Works b,Art_Person a force index (PersonCode) where b.PersonCode=a.PersonCode;          

+----+-------------+-------+-------+---------------+------------+---------+---------------------+--------+-------------+

| id | select_type | table | type  | possible_keys | key        | key_len | ref                 | rows   | Extra       |

+----+-------------+-------+-------+---------------+------------+---------+---------------------+--------+-------------+

|  1 | SIMPLE      | b     | index | PersonCode    | PersonCode | 25      | NULL                | 173223 | Using index | 

|  1 | SIMPLE      | a     | ref   | PersonCode    | PersonCode | 24      | newart.b.PersonCode |     13 | Using index | 

+----+-------------+-------+-------+---------------+------------+---------+---------------------+--------+-------------+

2 rows in set (0.00 sec)


mysql>  alter table Art_Works add index idx_PU(PersonCode,PID);  

Query OK, 173223 rows affected (5.73 sec)

Records: 173223  Duplicates: 0  Warnings: 0


mysql> desc select sql_no_cache count(*) as total from Art_Works b,Art_Person a force index (PersonCode) where b.PersonCode=a.PersonCode;

+----+-------------+-------+-------+-------------------+------------+---------+---------------------+------+--------------------------+

| id | select_type | table | type  | possible_keys     | key        | key_len | ref                 | rows | Extra                    |

+----+-------------+-------+-------+-------------------+------------+---------+---------------------+------+--------------------------+

|  1 | SIMPLE      | a     | index | PersonCode        | PersonCode | 24      | NULL                | 8910 | Using index              | 

|  1 | SIMPLE      | b     | ref   | PersonCode,idx_PU | idx_PU     | 25      | newart.a.PersonCode |    1 | Using where; Using index | 

+----+-------------+-------+-------+-------------------+------------+---------+---------------------+------+--------------------------+

2 rows in set (0.00 sec)



您可能感兴趣的文档:

--结束END--

本文标题: SQL优化案例分享--联合索引

本文链接: https://www.lsjlt.com/news/48344.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SQL优化案例分享--联合索引
    下面这个SQL如何优化:desc select count(*) as total from Art_Person a, Art_Works b where a.PersonCode=b...
    99+
    2022-10-18
  • mysql多个联合索引的案例分析
    小编给大家分享一下mysql多个联合索引的案例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!MySQL中索引规则:1、需要加...
    99+
    2022-10-18
  • MySQL优化之联合索引
    1.表结构 (root@localhost) [test]> show create table t_demo\G; *************************** 1. row ******...
    99+
    2022-10-18
  • MySQL索引优化分享
    2,explain的作⽤ 查看表的读取顺序,读取操作类型,有哪些索引可用,表之间关联,每张表中有哪些索引被优化器执⾏ 3,索引命中策略略分析     最左匹配原则 在索引字段上加入函数(不匹配索引)     is null/is not n...
    99+
    2016-09-28
    MySQL索引优化分享
  • MySQL 索引优化案例
    目录数据准备联合索引的首字段用范围查询强制走索引覆盖索引优化in和or什么时候会走索引like xx% 一般都会走索引,和数据量无关索引下推为什么范围查找没有用索引下推优化?如何选择索引Trace 工具深入优化order...
    99+
    2022-08-19
    MySQL索引优化 MySQL索引
  • SQL优化案例-自定义函数索引(五)
    SQL 文本如下,表本身很小,走全表扫描也很快,但因业务重要性,要求尽可能缩短查询时间(为保证客户隐私,已经将注释和文字部分去掉): SELECT MERCHCODE&nbs...
    99+
    2022-10-18
  • MySQL组合索引(多列索引)使用与优化案例详解
    目录1、多列索引2、测试案例及过程2.1 创建一个测试数据库和数据表2.2 添加两个单列索引2.3 查询一条数据利用到两个列的索引2.4 查看执行计划2.5 然后删除以上索引,添加多列索引2.6 再次查询3、多列索引的使...
    99+
    2022-07-04
    mysql组合索引使用 mysql索引
  • mysql中如何进行联合索引优化
    今天就跟大家聊聊有关mysql中如何进行联合索引优化,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。  exp...
    99+
    2022-10-19
  • MySQL索引优化实例分析
    目录1.数据准备2.实例一3.mysql如何选择合适的索引?4.常见 SQL 深入优化4.1.Order by与Group by优化4.2.分页查询优化4.3.join关联查询优化4.3.1.数据准备4.3.2.MySQ...
    99+
    2022-07-29
    MySQL索引优化 MySQL索引
  • SQL优化之多表关联查询-案例一
    慢SQL日志里看到一个三张表的关联查询,如下: SELECT COUNT(1)  FROM refund_order_item i, artis...
    99+
    2022-10-18
  • 高效的SQL(函数索引优化VIEW一例)
    高效的SQL(函数索引优化VIEW一例) 原创                     O...
    99+
    2022-10-18
  • 通过案例学调优之--分区表索引
    通过案例学调优之--分区表索引分区表索引Just like partitioned tables, partitioned indexes improve manageability, availabili...
    99+
    2022-10-18
  • MySQL中索引与优化的示例分析
    这篇文章主要介绍MySQL中索引与优化的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!索引与优化1、选择索引的数据类型MySQL支持很多数据类型,选择合适的数据类型存储数据对...
    99+
    2022-10-19
  • 大数据索引在ASP接口中的应用案例分享。
    随着大数据时代的到来,数据量越来越大,如何高效地存储和检索数据成为了亟待解决的问题。大数据索引技术就应运而生,它能够快速地找到数据,提高了数据检索的效率。在ASP接口中,大数据索引技术也有着广泛的应用。本文将分享一个大数据索引在ASP接口中...
    99+
    2023-10-04
    接口 大数据 索引
  • 一个案例彻底弄懂如何正确使用mysql inndb联合索引
    有一个业务是查询最新审核的5条数据 SELECT `id`, `title` FROM `th_content` WHERE `audit_time` < 1541984478 ...
    99+
    2022-10-18
  • SQL使用复合索引实现数据库查询的优化
    目录一 问题二 分析三 解决方案一 问题 程序再在一次查询时出现查询时间过长,每次查询要1-2分钟业务反馈用户操作体验很差,sql如下: select * FROM edi...
    99+
    2022-11-13
  • 索引优化系列十三--分区表各类聚合优化玄机
    -- 范围分区示例drop table range_part_tab purge;--注意,此分区为范围分区--例子1create table range_part_tab (id number,...
    99+
    2022-10-18
  • 如何通过索引优化PHP与MySQL的联合查询和子查询?
    在开发中,经常会遇到需要在PHP中执行联合查询和子查询的情况,而这些查询的性能往往非常关键。在处理大规模数据时,不优化的查询可能会导致严重的性能问题。因此,通过合适的索引优化MySQL查询是非常必要的。下面我们将详细介绍如何通过索引优化PH...
    99+
    2023-10-21
    MySQL PHP 索引优化
  • 分析SQL给出索引优化建议的工具(美团开源)
    分析SQL给出索引优化建议的工具(美团开源) ...
    99+
    2022-10-18
  • Windows操作系统下的ASP网站索引功能优化技巧分享!
    在Windows操作系统下,ASP网站索引是一个非常重要的功能,它可以帮助用户快速地查找到所需的内容。但是,如果ASP网站的索引功能不够优化,可能会导致用户的搜索体验变得很糟糕。因此,在本文中,我们将分享一些关于Windows操作系统下A...
    99+
    2023-06-28
    windows 索引 http
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作