- CREATE TABLE `shop` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '记录ID',
- `shop_id` int(11) NOT NULL COMMENT '商店ID',
- `goods_id` int(11) NOT NULL COMMENT '物品ID',
- `pay_type` tinyint(1) NOT NULL COMMENT '支付方式',
- `price` decimal(10,2) NOT NULL COMMENT '物品价格',
- `comment` varchar(200) NOT NULL COMMENT '备注',
- PRIMARY KEY (`id`),
- UNIQUE KEY `shop_id` (`shop_id`,`goods_id`),
- KEY `price` (`price`),
- KEY `pay_type` (`pay_type`)
- ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='商店物品表'
- INSERT INTO `shop` (`id`, `shop_id`, `goods_id`, `pay_type`, `price`, `comment`) VALUES
- (1, 1, 1, 0, '1.00', ''),
- (2, 2, 1, 0, '24.00', ''),
- (3, 2, 3, 1, '5.99', ''),
- (4, 3, 1, 0, '1.99', ''),
- (5, 3, 2, 1, '81.00', ''),
- (6, 4, 2, 0, '15.00', ''),
- (7, 4, 3, 0, '22.00', '');
- SELECT * FROM t1 ORDER BY key_part1,key_part2,... ;
- SELECT * FROM t1 WHERE key_part1=constant ORDER BY key_part2;
- SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 DESC;
- SELECT * FROM t1 WHERE key_part1=1 ORDER BY key_part1 DESC, key_part2 DESC;
- SELECT * FROM t1 ORDER BY key_part1,key_part2,...;
- mysql> explain select * from shop order by shop_id,goods_id;
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | 1 | SIMPLE | shop | ALL | NULL | NULL | NULL | NULL | 7 | Using filesort |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- 1 row in set (0.00 sec)
- mysql> explain select id,shop_id,goods_id from shop order by shop_id,goods_id;
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
- | 1 | SIMPLE | shop | index | NULL | shop_id | 8 | NULL | 7 | Using index |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
- 1 row in set (0.00 sec)
- mysql> explain select * from shop force index(shop_id) order by shop_id,goods_id;
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- | 1 | SIMPLE | shop | index | NULL | shop_id | 8 | NULL | 7 | |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- 1 row in set (0.03 sec)
- SELECT * FROM t1 WHERE key_part1=constant ORDER BY key_part2;
- mysql> explain select * from shop where shop_id=2 order by goods_id;
- +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
- | 1 | SIMPLE | shop | ref | shop_id | shop_id | 4 | const | 2 | Using where |
- +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
- 1 row in set (0.00 sec)
- SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 DESC;
- mysql> explain select * from shop order by shop_id desc,goods_id desc;
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | 1 | SIMPLE | shop | ALL | NULL | NULL | NULL | NULL | 7 | Using filesort |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- 1 row in set (0.00 sec)
- mysql> explain select id,shop_id,goods_id from shop order by shop_id desc,goods_id desc;
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
- | 1 | SIMPLE | shop | index | NULL | shop_id | 8 | NULL | 7 | Using index |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
- 1 row in set (0.00 sec)
- mysql> explain select * from shop force index(shop_id) order by shop_id desc,goods_id desc;
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- | 1 | SIMPLE | shop | index | NULL | shop_id | 8 | NULL | 7 | |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- 1 row in set (0.00 sec)
- SELECT * FROM t1 WHERE key_part1=1 ORDER BY key_part1 DESC, key_part2 DESC;
- mysql> explain select * from shop where shop_id=2 order by shop_id desc,goods_id desc;
- +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
- | 1 | SIMPLE | shop | ref | shop_id | shop_id | 4 | const | 2 | Using where |
- +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
- 1 row in set (0.00 sec)
- SELECT * FROM t1 WHERE key_part1=constant ORDER BY key_part2;
- SELECT * FROM t1 ORDER BY key1, key2;
- mysql> explain select * from shop order by id,price;
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | 1 | SIMPLE | shop | ALL | NULL | NULL | NULL | NULL | 7 | Using filesort |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- 1 row in set (0.00 sec)
- SELECT * FROM t1 WHERE key2=constant ORDER BY key_part2;
- mysql> explain select * from shop where price=15 order by goods_id;
- +----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------+
- | 1 | SIMPLE | shop | ref | price | price | 5 | const | 1 | Using where; Using filesort |
- +----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------+
- 1 row in set (0.00 sec)
- SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 ASC;
- mysql> explain select * from shop order by shop_id asc,goods_id desc;
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | 1 | SIMPLE | shop | ALL | NULL | NULL | NULL | NULL | 7 | Using filesort |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- 1 row in set (0.00 sec)
- SELECT * FROM t1 WHERE key2=constant ORDER BY key1;
- mysql> explain select * from shop where pay_type=1 order by price;
- +----+-------------+-------+------+---------------+----------+---------+-------+------+-----------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+----------+---------+-------+------+-----------------------------+
- | 1 | SIMPLE | shop | ref | pay_type | pay_type | 1 | const | 2 | Using where; Using filesort |
- +----+-------------+-------+------+---------------+----------+---------+-------+------+-----------------------------+
- 1 row in set (0.00 sec)
- mysql> explain select * from shop where pay_type=1 order by id;
- +----+-------------+-------+------+---------------+----------+---------+-------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+----------+---------+-------+------+-------------+
- | 1 | SIMPLE | shop | ref | pay_type | pay_type | 1 | const | 2 | Using where |
- +----+-------------+-------+------+---------------+----------+---------+-------+------+-------------+
- 1 row in set (0.00 sec)
- SELECT * FROM t1 ORDER BY ABS(key);
- SELECT * FROM t1 ORDER BY -key;
- mysql> explain select * from shop order by id;
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- | 1 | SIMPLE | shop | index | NULL | PRIMARY | 4 | NULL | 7 | |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- 1 row in set (0.00 sec)
- mysql> explain select * from shop order by ABS(id);
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | 1 | SIMPLE | shop | ALL | NULL | NULL | NULL | NULL | 7 | Using filesort |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- 1 row in set (0.00 sec)
- mysql> explain select * from shop order by -id;
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- | 1 | SIMPLE | shop | ALL | NULL | NULL | NULL | NULL | 7 | Using filesort |
- +----+-------------+-------+------+---------------+------+---------+------+------+----------------+
- 1 row in set (0.28 sec)
- CREATE TABLE `pay_type` (
- `type_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
- `rate` decimal(6,2) NOT NULL COMMENT '费率',
- `name` varchar(20) NOT NULL COMMENT '名称',
- PRIMARY KEY (`type_id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='支付方式表'
- INSERT INTO `pay_type` (`type_id`, `rate`, `name`) VALUES
- (1, '0.01', '手机'),
- (2, '0.02', '网银'),
- (3, '0.00', '货到付款');
- mysql> explain select * from shop a left join pay_type b on a.pay_type=b.type_id where a.id>2 order by a.goods_id,b.type_id;
- +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+----------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+----------------------------------------------+
- | 1 | SIMPLE | a | range | PRIMARY | PRIMARY | 4 | NULL | 5 | Using where; Using temporary; Using filesort |
- | 1 | SIMPLE | b | eq_ref | PRIMARY | PRIMARY | 4 | test.a.pay_type | 1 | |
- +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+----------------------------------------------+
- 2 rows in set (0.28 sec)
- mysql> explain select shop_id, max(price) max_price from shop group by shop_id order by max_price desc;
- +----+-------------+-------+-------+---------------+---------+---------+------+------+---------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+---------------------------------+
- | 1 | SIMPLE | shop | index | NULL | shop_id | 8 | NULL | 7 | Using temporary; Using filesort |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+---------------------------------+
- 1 row in set (0.04 sec)
- mysql> explain select shop_id, max(price) max_price from shop group by shop_id order by shop_id desc;
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- | 1 | SIMPLE | shop | index | NULL | shop_id | 8 | NULL | 7 | |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
- 1 row in set (0.01 sec)
- ALTER TABLE `test`.`pay_type` ADD INDEX `name` ( `name` ( 1 ) )
- mysql> explain select * from pay_type force index(name) order by name;
- +----+-------------+----------+------+---------------+------+---------+------+------+----------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+----------+------+---------------+------+---------+------+------+----------------+
- | 1 | SIMPLE | pay_type | ALL | NULL | NULL | NULL | NULL | 3 | Using filesort |
- +----+-------------+----------+------+---------------+------+---------+------+------+----------------+
- 1 row in set (0.00 sec)
- ALTER TABLE `test`.`pay_type` DROP INDEX `name` ,
- ADD INDEX `name` ( `name` )
- mysql> explain select * from pay_type force index(name) order by name;
- +----+-------------+----------+-------+---------------+------+---------+------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+----------+-------+---------------+------+---------+------+------+-------+
- | 1 | SIMPLE | pay_type | index | NULL | name | 62 | NULL | 3 | |
- +----+-------------+----------+-------+---------------+------+---------+------+------+-------+
- 1 row in set (0.00 sec)
- CREATE TABLE `heap_test` (
- `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
- `name` varchar(20) NOT NULL COMMENT '名称',
- PRIMARY KEY (`id`),
- KEY `name` (`name`)
- ) ENGINE=MEMORY AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='测试heap表'
- INSERT INTO `heap_test` (`id`, `name`) VALUES
- (1, '张三'),
- (2, '李四');
- mysql> explain select * from heap_test force index(name) order by name;
- +----+-------------+-----------+------+---------------+------+---------+------+------+----------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-----------+------+---------------+------+---------+------+------+----------------+
- | 1 | SIMPLE | heap_test | ALL | NULL | NULL | NULL | NULL | 2 | Using filesort |
- +----+-------------+-----------+------+---------------+------+---------+------+------+----------------+
- 1 row in set (0.00 sec)
- mysql> explain select abs(shop_id) shop_id from shop force index(shop_id) order by shop_id;
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-----------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-----------------------------+
- | 1 | SIMPLE | shop | index | NULL | shop_id | 8 | NULL | 7 | Using index; Using filesort |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-----------------------------+
- 1 row in set (0.00 sec)
- mysql> explain select abs(shop_id) shop_new_id from shop force index(shop_id) order by shop_id;
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
- | 1 | SIMPLE | shop | index | NULL | shop_id | 8 | NULL | 7 | Using index |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
- 1 row in set (0.00 sec)
- mysql> explain select goods_id,count(1) from shop force index(shop_id) group by goods_id;
- +----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
- | 1 | SIMPLE | shop | index | NULL | shop_id | 8 | NULL | 7 | Using index; Using temporary; Using filesort |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+----------------------------------------------+
- 1 row in set (0.00 sec)
- mysql> explain select goods_id,count(1) from shop force index(shop_id) group by goods_id order by null;
- +----+-------------+-------+-------+---------------+---------+---------+------+------+------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+------------------------------+
- | 1 | SIMPLE | shop | index | NULL | shop_id | 8 | NULL | 7 | Using index; Using temporary |
- +----+-------------+-------+-------+---------------+---------+---------+------+------+------------------------------+
- 1 row in set (0.00 sec)
- 增加sort_buffer_size变量的大小。
- 增加read_rnd_buffer_size变量的大小。
- 更改tmpdir指向具有大量空闲空间的专用文件系统。
分类:
数据库
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构