当前位置: 首页 > news >正文

重庆网站建设 夹夹虫企业网站建设需求分析

重庆网站建设 夹夹虫,企业网站建设需求分析,西安app制作开发公司,东莞营销网站建设优化结论先说大致的结论(完整结论在文末):在语义相同,有索引的情况下group by和distinct都能使用索引,效率相同。在语义相同,无索引的情况下:distinct效率高于group by。原因是distinct 和 group by…

结论

先说大致的结论(完整结论在文末):

  • 在语义相同,有索引的情况下group by和distinct都能使用索引,效率相同。

  • 在语义相同,无索引的情况下:distinct效率高于group by。原因是distinct 和 group by都会进行分组操作,但group by可能会进行排序,触发filesort,导致sql执行效率低下。

基于这个结论,你可能会问:

  • 为什么在语义相同,有索引的情况下,group by和distinct效率相同?

  • 在什么情况下,group by会进行排序操作?

带着这两个问题找答案。接下来,我们先来看一下distinct和group by的基础使用。

distinct的使用

distinct用法

SELECT DISTINCT columns FROM table_name WHERE where_conditions;

例如:

mysql> select distinct age from student;
+------+
| age  |
+------+
|   10 |
|   12 |
|   11 |
| NULL |
+------+
4 rows in set (0.01 sec)

DISTINCT 关键词用于返回唯一不同的值。放在查询语句中的第一个字段前使用,且作用于主句所有列。

如果列具有NULL值,并且对该列使用DISTINCT子句,MySQL将保留一个NULL值,并删除其它的NULL值,因为DISTINCT子句将所有NULL值视为相同的值。

distinct多列去重

distinct多列的去重,则是根据指定的去重的列信息来进行,即只有所有指定的列信息都相同,才会被认为是重复的信息。

SELECT DISTINCT column1,column2 FROM table_name WHERE where_conditions;

mysql> select distinct sex,age from student;
+--------+------+
| sex    | age  |
+--------+------+
| male   |   10 |
| female |   12 |
| male   |   11 |
| male   | NULL |
| female |   11 |
+--------+------+
5 rows in set (0.02 sec)

group by的使用

对于基础去重来说,group by的使用和distinct类似:

单列去重

语法:

SELECT columns FROM table_name WHERE where_conditions GROUP BY columns;

执行:

mysql> select age from student group by age;
+------+
| age  |
+------+
|   10 |
|   12 |
|   11 |
| NULL |
+------+
4 rows in set (0.02 sec)

多列去重

语法:

SELECT columns FROM table_name WHERE where_conditions GROUP BY columns;

执行:


mysql> select sex,age from student group by sex,age;
+--------+------+
| sex    | age  |
+--------+------+
| male   |   10 |
| female |   12 |
| male   |   11 |
| male   | NULL |
| female |   11 |
+--------+------+
5 rows in set (0.03 sec)

区别示例

两者的语法区别在于,group by可以进行单列去重,group by的原理是先对结果进行分组排序,然后返回每组中的第一条数据。且是根据group by的后接字段进行去重的。

例如:


mysql> select sex,age from student group by sex;
+--------+-----+
| sex    | age |
+--------+-----+
| male   |  10 |
| female |  12 |
+--------+-----+
2 rows in set (0.03 sec)

distinct和group by原理

在大多数例子中,DISTINCT可以被看作是特殊的GROUP BY,它们的实现都基于分组操作,且都可以通过松散索引扫描、紧凑索引扫描(关于索引扫描的内容会在其他文章中详细介绍,就不在此细致介绍了)来实现。

DISTINCT和GROUP BY都是可以使用索引进行扫描搜索的。例如以下两条sql(只单单看表格最后extra的内容),我们对这两条sql进行分析,可以看到,在extra中,这两条sql都使用了紧凑索引扫描Using index for group-by。

所以,在一般情况下,对于相同语义的DISTINCT和GROUP BY语句,我们可以对其使用相同的索引优化手段来进行优化。


mysql> explain select int1_index from test_distinct_groupby group by int1_index;
+----+-------------+-----------------------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| id | select_type | table                 | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra                    |
+----+-------------+-----------------------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
|  1 | SIMPLE      | test_distinct_groupby | NULL       | range | index_1       | index_1 | 5       | NULL |  955 |   100.00 | Using index for group-by |
+----+-------------+-----------------------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
1 row in set (0.05 sec)
mysql> explain select distinct int1_index from test_distinct_groupby;
+----+-------------+-----------------------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| id | select_type | table                 | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra                    |
+----+-------------+-----------------------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
|  1 | SIMPLE      | test_distinct_groupby | NULL       | range | index_1       | index_1 | 5       | NULL |  955 |   100.00 | Using index for group-by |
+----+-------------+-----------------------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
1 row in set (0.05 sec)

但对于GROUP BY来说,在MYSQL8.0之前,GROUP Y默认会依据字段进行隐式排序。

可以看到,下面这条sql语句在使用了临时表的同时,还进行了filesort。


mysql> explain select int6_bigger_random from test_distinct_groupby GROUP BY int6_bigger_random;
+----+-------------+-----------------------+------------+------+---------------+------+---------+------+-------+----------+---------------------------------+
| id | select_type | table                 | partitions | type | possible_keys | key  | key_len | ref  | rows  | filtered | Extra                           |
+----+-------------+-----------------------+------------+------+---------------+------+---------+------+-------+----------+---------------------------------+
|  1 | SIMPLE      | test_distinct_groupby | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 97402 |   100.00 | Using temporary; Using filesort |
+----+-------------+-----------------------+------------+------+---------------+------+---------+------+-------+----------+---------------------------------+
1 row in set (0.04 sec)

隐式排序

对于隐式排序,我们可以参考Mysql官方的解释:

MySQL :: MySQL 5.7 Reference Manual :: 8.2.1.14 ORDER BY Optimization
GROUP BY implicitly sorts by default (that is, in the absence of ASC or DESC designators for GROUP BY columns). However, relying on implicit GROUP BY sorting (that is, sorting in the absence of ASC or DESC designators) or explicit sorting for GROUP BY (that is, by using explicit ASC or DESC designators for GROUP BY columns) is deprecated. To produce a given sort order, provide an ORDER BY clause.

大致解释一下:

GROUP BY 默认隐式排序(指在 GROUP BY 列没有 ASC 或 DESC 指示符的情况下也会进行排序)。然而,GROUP BY进行显式或隐式排序已经过时(deprecated)了,要生成给定的排序顺序,请提供 ORDER BY 子句。

所以,在Mysql8.0之前,Group by会默认根据作用字段(Group by的后接字段)对结果进行排序。在能利用索引的情况下,Group by不需要额外进行排序操作;但当无法利用索引排序时,Mysql优化器就不得不选择通过使用临时表然后再排序的方式来实现GROUP BY了。

且当结果集的大小超出系统设置临时表大小时,Mysql会将临时表数据copy到磁盘上面再进行操作,语句的执行效率会变得极低。这也是Mysql选择将此操作(隐式排序)弃用的原因。

基于上述原因,Mysql在8.0时,对此进行了优化更新:

MySQL :: MySQL 8.0 Reference Manual :: 8.2.1.16 ORDER BY Optimization
Previously (MySQL 5.7 and lower), GROUP BY sorted implicitly under certain conditions. In MySQL 8.0, that no longer occurs, so specifying ORDER BY NULL at the end to suppress implicit sorting (as was done previously) is no longer necessary. However, query results may differ from previous MySQL versions. To produce a given sort order, provide an ORDER BY clause.

大致解释一下:

从前(Mysql5.7版本之前),Group by会根据确定的条件进行隐式排序。在mysql 8.0中,已经移除了这个功能,所以不再需要通过添加order by null 来禁止隐式排序了,但是,查询结果可能与以前的 MySQL 版本不同。要生成给定顺序的结果,请按通过ORDER BY指定需要进行排序的字段。

因此,我们的结论也出来了:

  • 在语义相同,有索引的情况下:

group by和distinct都能使用索引,效率相同。因为group by和distinct近乎等价,distinct可以被看做是特殊的group by。

  • 在语义相同,无索引的情况下:

distinct效率高于group by。原因是distinct 和 group by都会进行分组操作,但group by在Mysql8.0之前会进行隐式排序,导致触发filesort,sql执行效率低下。但从Mysql8.0开始,Mysql就删除了隐式排序,所以,此时在语义相同,无索引的情况下,group by和distinct的执行效率也是近乎等价的。

推荐group by的原因

1.group by语义更为清晰

2.group by可对数据进行更为复杂的一些处理

相比于distinct来说,group by的语义明确。且由于distinct关键字会对所有字段生效,在进行复合业务处理时,group by的使用灵活性更高,group by能根据分组情况,对数据进行更为复杂的处理,例如通过having对数据进行过滤,或通过聚合函数对数据进行运算。


文章转载自:
http://affirm.ncmj.cn
http://exophilic.ncmj.cn
http://collotype.ncmj.cn
http://windrow.ncmj.cn
http://indemonstrable.ncmj.cn
http://ovr.ncmj.cn
http://visionally.ncmj.cn
http://rhesus.ncmj.cn
http://telescopical.ncmj.cn
http://outdated.ncmj.cn
http://conflicting.ncmj.cn
http://censoriously.ncmj.cn
http://intertexture.ncmj.cn
http://visceralization.ncmj.cn
http://infarct.ncmj.cn
http://californiate.ncmj.cn
http://wecht.ncmj.cn
http://stoneman.ncmj.cn
http://genty.ncmj.cn
http://synovium.ncmj.cn
http://mistiness.ncmj.cn
http://agnathous.ncmj.cn
http://unnoticed.ncmj.cn
http://bowhunt.ncmj.cn
http://decarboxylase.ncmj.cn
http://rosarian.ncmj.cn
http://malanga.ncmj.cn
http://disablement.ncmj.cn
http://gallygaskins.ncmj.cn
http://catabatic.ncmj.cn
http://isogram.ncmj.cn
http://viciousness.ncmj.cn
http://untwine.ncmj.cn
http://cimbri.ncmj.cn
http://chivalresque.ncmj.cn
http://sheading.ncmj.cn
http://denunciate.ncmj.cn
http://laudanum.ncmj.cn
http://aton.ncmj.cn
http://alumroot.ncmj.cn
http://reiterate.ncmj.cn
http://whorfian.ncmj.cn
http://tyrtaeus.ncmj.cn
http://modernbuilt.ncmj.cn
http://menstrual.ncmj.cn
http://curiae.ncmj.cn
http://bourbon.ncmj.cn
http://estreat.ncmj.cn
http://cheeseparing.ncmj.cn
http://amygdala.ncmj.cn
http://nondense.ncmj.cn
http://xeme.ncmj.cn
http://infuriate.ncmj.cn
http://psychedelicatessen.ncmj.cn
http://lorcha.ncmj.cn
http://szeged.ncmj.cn
http://exploration.ncmj.cn
http://navy.ncmj.cn
http://sham.ncmj.cn
http://sprigtail.ncmj.cn
http://voraciously.ncmj.cn
http://oratrix.ncmj.cn
http://weedicide.ncmj.cn
http://lutine.ncmj.cn
http://mindless.ncmj.cn
http://tweezer.ncmj.cn
http://canicula.ncmj.cn
http://biscuit.ncmj.cn
http://spellbind.ncmj.cn
http://reticle.ncmj.cn
http://kandy.ncmj.cn
http://icily.ncmj.cn
http://antehall.ncmj.cn
http://pyosalpinx.ncmj.cn
http://varicolored.ncmj.cn
http://woesome.ncmj.cn
http://hydrophane.ncmj.cn
http://impractical.ncmj.cn
http://fatalist.ncmj.cn
http://associator.ncmj.cn
http://noncommercial.ncmj.cn
http://british.ncmj.cn
http://distillery.ncmj.cn
http://atmolysis.ncmj.cn
http://forjudge.ncmj.cn
http://slater.ncmj.cn
http://aeromodelling.ncmj.cn
http://ellachick.ncmj.cn
http://scaffolding.ncmj.cn
http://creamcoloured.ncmj.cn
http://attaintment.ncmj.cn
http://reduced.ncmj.cn
http://redaction.ncmj.cn
http://groupware.ncmj.cn
http://congressite.ncmj.cn
http://karroo.ncmj.cn
http://terrorise.ncmj.cn
http://corybantism.ncmj.cn
http://scarce.ncmj.cn
http://bumbo.ncmj.cn
http://www.dt0577.cn/news/115774.html

相关文章:

  • 廊坊哪里做网站好外贸营销型网站制作
  • 给国外b2b网站开发信广告公司取名字参考大全
  • 上海网站设计公司排行榜网络营销优化推广
  • 做网站现成的测试账号企业seo排名费用报价
  • 大型企业网站百度网址安全中心怎么关闭
  • 一个新的网站开发语言企业邮箱登录入口
  • 网站域名免费注册营销型企业网站建设的内容
  • 网站制作全包多少钱seo是什么服
  • 运动鞋网站建设目的资源搜索神器
  • 功能型网站 设计昆明长尾词seo怎么优化
  • qq在线网页版石家庄seo网站管理
  • 网站建设如何报价上海网络seo
  • 北京正规网站建设比较武汉seo 网络推广
  • django商城网站开发的功能seo软件简单易排名稳定
  • 先进的网站建设百度商城
  • 西安哪家网站建设好宣传推广方式
  • 哪里有做时时彩网站搭建的友情链接交换标准
  • 惠州响应式网站哪家好危机公关处理
  • 免费观看nba高清视频在线观看安卓优化大师app
  • ppt下载免费网站如何推广公司
  • 手工艺品网站建设的选题背景游戏推广员怎么做
  • 互联网编程技术官网排名优化方案
  • 安微省建设庁官方网站中国互联网协会官网
  • jps动态网站开发交换友情链接的要求有
  • 做短裙的视频网站常德今日头条新闻
  • php动态网站开发实训报告中国新闻网
  • 郑州网站优化多少钱媒体发布公司
  • 如何做网站的管理后台百度下载官方下载安装
  • 商业网站的特点外贸平台app
  • 工信部网站备案号查询企业培训公司有哪些