设为首页 加入收藏

TOP

table_rows查询优化(二)
2018-04-13 06:06:32 】 浏览:260
Tags:table_rows 查询 优化
,经过几次度娘和FQ后,排除错误答案 ,终于可以很遗憾的告诉你主键索引确实是最快的,只是主键索引查询是有前提条件的,至于什么条件烦请查看我下一篇关于count(*)怎么走索引,走那种索引分析。


2、上面的方式对单次查询,在足够配置的物理机上,显然我们还是可以接受的。然而很多次的类似sql出现,对数据库的性能也是一种不必要的损耗,因为这对业务发展并没有很深的意义。我们知道对于select count(*) from table_name这样的sql是没有办法通过索引优化的,那么只能通过改写sql进行优化了,这也是一个精通sql优化高手必备的技能。


如果你也想精确查询表中的行数,又想查询的时间能尽可能短,这个时候我们就要想到max()和min()函数了,通常我们统计最大值和最小值都是很快返回结果的。


mysql> select ifnull(max(id),0)-ifnull(min(id),0)+1 as rows from rule_ceshi.operation_log;
+----------+
| rows    |
+----------+
| 21124162 |
+----------+
1 row in set (0.02 sec)


mysql> explain select ifnull(max(id),0)-ifnull(min(id),0)+1 as rows from rule_ceshi.operation_log;
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                        |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Select tables optimized away |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
1 row in set (0.01 sec)


当然使用这种优化改写的前提是你的上产中表中有主键且是整数类型的,主键还需是连续的,也就是你的上产中没有进行过delete from table where xxx=xxx的删除行记录操作,否则这样统计还是不精准的。


3、我们知道MySQL自带一个统计信息,平时我们的show命令之类的都来源数据库中的统计表。如果我们的Dev告诉我们,只需要模糊查询知晓表中数据行数呢?这个时候,你就可以通过MySQL自带的information_schema.tables表的统计信息,初步判断表的数据行大小。


mysql> select table_schema,table_name,table_type,table_rows from information_schema.tables where table_schema='rule_ceshi' and table_name='operation_log';
+--------------+---------------+------------+------------+
| table_schema | table_name    | table_type | table_rows |
+--------------+---------------+------------+------------+
| rule_ceshi  | operation_log | BASE TABLE |  20660338 |
+--------------+---------------+------------+------------+
row in set (0.01 sec)


mysql> explain select table_schema,table_name,table_type,table_rows from information_schema.tables where table_schema='rule_ceshi' and table_name='operation_log';
+----+-------------+--------+------+---------------+-------------------------+---------+------+------+---------------------------------------------------+
| id | select_type | table  | type | possible_keys | key                    | key_len | ref  | rows | Extra                                            |
+----+-------------+--------+------+---------------+-------------------------+---------+------+------+---------------------------------------------------+
|  1 | SIMPLE      | tables | ALL  | NULL          | TABLE_SCHEMA,TABLE_NAME | NULL    | NULL | NULL | Using where; Open_full_table; Scanned 0 databases |
+----+-------------+--------+------+---------------+-------------------------+---------+------+------+---------------------

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇pt-table-checksum工具主从一致性.. 下一篇如何在Ubuntu上安装MySQL/MariaDB

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目