SQL优化案例分享--联合索引
admin
2023-05-19 06:22:23
0

下面这个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)



相关内容

热门资讯

苏州能模维申请服务器液冷散热器... 国家知识产权局信息显示,苏州能模维系统有限公司申请一项名为“一种服务器液冷散热器”的专利,公开号CN...
华为申请通信方法、设备和可读存... 国家知识产权局信息显示,华为技术有限公司申请一项名为“通信方法、设备和可读存储介质”的专利,公开号C...
批赖清德当局误判“台美关系”,... 海峡导报综合报道 美国总统特朗普结束访华行程后,外媒陆续报道其涉台相关说法。中国国民党17日批评民进...
家用冰箱温度调多少度合适 冰箱温度调节根据季节不同、需求不同来设定。通常来说夏季室外温度较高,控温可以调节在3-4档位,同时温...
智能冰箱温度调到多少合适 一般的食物适合的温度,新鲜食品区域推荐的温度是2.7-4.4℃,冷冻室的温度是-15℃。如果有单独的...
热水器平时调到什么温度合适 在热水器温度的调节上,要根据自己的实际情况来考虑,主要两个方面,第一,自己认为最合适的水温,第二,节...
冰箱调节温度1234567调哪... 一档的制冷效果是最低的,而七档是最高的,但一般都不会把温度调到一档或七档,不然会让冰箱超负荷运行。如...
多位省级政府“一把手”为本省份... 澎湃新闻记者 蒋子文近期,新疆、广西、青海、江西等省级政府“一把手”相继亮相本地足球赛事,并为比赛开...
兰石集团工业母机装备亮相越南国... 5月14日,第23届“越南国际机械、设备、技术及工业产品展”在越南河内国际展览中心开幕。作为国内工业...
外墙腻子粉一平方需要多少公斤 外墙刮腻子也是必要的一项工艺,要将外面的墙体全部清理干净以后,再刮上腻子找平,然后再刷上防水漆或者是...