mysql查询最大和最小_MySQL 查询最大最小值优化

1. 假设你使用了Innodb存储引擎

2. 假设你在innodb设定了主键(聚集索引)

3. 因为聚集索引页面之间是通过双向链表链接,页按照主键的顺序排序

每个页中的记录也是通过双向链表维护。聚集索引上存储了主键的值

由于B+树的特性,最左端的叶子节点存储最小的值,最右端的叶子节点存储最大的值。

4. 最小值的一般方法:我们可以看到没有使用key,设计的行299600行

root:employees 11:00 > select min(emp_no) from employees where gender=‘M‘;

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

| min(emp_no) |

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

| 10001 |

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

1 row in set (0.11 sec)

root:employees 11:07 > explain select min(emp_no) from employees where gender=‘M‘;

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

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

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

| 1 | SIMPLE | employees | ALL | NULL | NULL | NULL | NULL | 299600 | Using where |

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

5. 利用上面的说明,取出最左端的叶子节点即可。此时我们看到执行时间很短,虽然explain结果比较困惑!

root:employees 11:12 > select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ limit 1;

+--------+

| emp_no |

+--------+

| 10001 |

+--------+

1 row in set (0.00 sec)

root:employees 11:13 > explain select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ limit 1;

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

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

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

| 1 | SIMPLE | employees | ALL | NULL | NULL | NULL | NULL | 299600 | Using where |

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

6. 同样我们执行max最大值的时候,可以先倒排在取出第一个数据。因为页之间通过双向链表链接。

root:employees 11:18 > select max(emp_no) from employees where gender=‘M‘;

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

| max(emp_no) |

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

| 499999 |

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

1 row in set (0.22 sec)

root:employees 11:18 > select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ order by emp_no desc limit 1;

+--------+

| emp_no |

+--------+

| 499999 |

+--------+

1 row in set (0.00 sec)

root:employees 11:18 > explain select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ order by emp_no desc limit 1;

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

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

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

| 1 | SIMPLE | employees | index | NULL | PRIMARY | 4 | NULL | 1 | Using where |

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

7.我们在查询范围的使用,也可以利用B+树的特性来迅速查询到我们想要的信息。因为B+树的索引页存储了主键的范围;

root:employees 11:22 > explain select emp_no from employees USE INDEX(PRIMARY) where gender=‘M‘ order by emp_no desc limit 1;

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

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

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

| 1 | SIMPLE | employees | index | NULL | PRIMARY | 4 | NULL | 1 | Using where |

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

原文:http://www.cnblogs.com/wxl-dede/p/5351624.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 MFC 的数据库类以及 MySQL 的 ODBC 驱动来实现查询最大最小值的功能。下面是一个简单的示例: 1. 首先,您需要在您的对话框类添加一个 CDatabase 对象: ``` class CMyDialog : public CDialogEx { public: CDatabase m_db; // 添加数据库对象 // ... }; ``` 2. 在 OnInitDialog 打开数据库连接: ``` BOOL CMyDialog::OnInitDialog() { CDialogEx::OnInitDialog(); // 打开数据库连接 if (!m_db.OpenEx(_T("DRIVER={MySQL ODBC 8.0 Unicode Driver};SERVER=localhost;DATABASE=your_database_name;UID=your_username;PWD=your_password"), CDatabase::noOdbcDialog)) { AfxMessageBox(_T("无法打开数据库连接!")); EndDialog(IDCANCEL); } // ... } ``` 注意:上述代码的 "localhost"、"your_database_name"、"your_username" 和 "your_password" 需要替换为您自己的 MySQL 数据库连接信息。 3. 在需要查询最大最小值的地方,使用 CRecordset 对象执行 SQL 语句并获取结果: ``` void CMyDialog::OnButtonGetMinMax() { CString strSQL; strSQL.Format(_T("SELECT MIN(column_name), MAX(column_name) FROM table_name")); CRecordset rs(&m_db); // 创建记录集对象 rs.Open(CRecordset::forwardOnly, strSQL); // 执行 SQL 语句 if (!rs.IsEOF()) // 如果记录集不为空 { CString strMin, strMax; rs.GetFieldValue(0, strMin); // 获取第一个字段的值(即 MIN(column_name)) rs.GetFieldValue(1, strMax); // 获取第二个字段的值(即 MAX(column_name)) // 显示结果 // ... } rs.Close(); // 关闭记录集 } ``` 注意:上述示例的 SQL 语句是针对 MySQL 数据库的,如果您使用的是其他类型的数据库,语法可能会有所不同。此外,您需要将代码的 "your_database_name"、"your_username" 和 "your_password" 替换为您自己的数据库连接信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值